// Code generated by gotdgen, DO NOT EDIT.

package tdapi

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)

// No-op definition for keeping imports.
var (
	_ = bin.Buffer{}
	_ = context.Background()
	_ = fmt.Stringer(nil)
	_ = strings.Builder{}
	_ = errors.Is
	_ = multierr.AppendInto
	_ = sort.Ints
	_ = tdp.Format
	_ = tgerr.Error{}
	_ = tdjson.Encoder{}
)

// LoadDirectMessagesChatTopicsRequest represents TL type `loadDirectMessagesChatTopics#58b215b7`.
type LoadDirectMessagesChatTopicsRequest struct {
	// Chat identifier of the channel direct messages chat
	ChatID int64
	// The maximum number of topics to be loaded. For optimal performance, the number of
	// loaded topics is chosen by TDLib and can be smaller than the specified limit, even if
	// the end of the list is not reached
	Limit int32
}

// LoadDirectMessagesChatTopicsRequestTypeID is TL type id of LoadDirectMessagesChatTopicsRequest.
const LoadDirectMessagesChatTopicsRequestTypeID = 0x58b215b7

// Ensuring interfaces in compile-time for LoadDirectMessagesChatTopicsRequest.
var (
	_ bin.Encoder     = &LoadDirectMessagesChatTopicsRequest{}
	_ bin.Decoder     = &LoadDirectMessagesChatTopicsRequest{}
	_ bin.BareEncoder = &LoadDirectMessagesChatTopicsRequest{}
	_ bin.BareDecoder = &LoadDirectMessagesChatTopicsRequest{}
)

func (l *LoadDirectMessagesChatTopicsRequest) Zero() bool {
	if l == nil {
		return true
	}
	if !(l.ChatID == 0) {
		return false
	}
	if !(l.Limit == 0) {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (l *LoadDirectMessagesChatTopicsRequest) String() string {
	if l == nil {
		return "LoadDirectMessagesChatTopicsRequest(nil)"
	}
	type Alias LoadDirectMessagesChatTopicsRequest
	return fmt.Sprintf("LoadDirectMessagesChatTopicsRequest%+v", Alias(*l))
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*LoadDirectMessagesChatTopicsRequest) TypeID() uint32 {
	return LoadDirectMessagesChatTopicsRequestTypeID
}

// TypeName returns name of type in TL schema.
func (*LoadDirectMessagesChatTopicsRequest) TypeName() string {
	return "loadDirectMessagesChatTopics"
}

// TypeInfo returns info about TL type.
func (l *LoadDirectMessagesChatTopicsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "loadDirectMessagesChatTopics",
		ID:   LoadDirectMessagesChatTopicsRequestTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (l *LoadDirectMessagesChatTopicsRequest) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode loadDirectMessagesChatTopics#58b215b7 as nil")
	}
	b.PutID(LoadDirectMessagesChatTopicsRequestTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *LoadDirectMessagesChatTopicsRequest) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode loadDirectMessagesChatTopics#58b215b7 as nil")
	}
	b.PutInt53(l.ChatID)
	b.PutInt32(l.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (l *LoadDirectMessagesChatTopicsRequest) Decode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode loadDirectMessagesChatTopics#58b215b7 to nil")
	}
	if err := b.ConsumeID(LoadDirectMessagesChatTopicsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode loadDirectMessagesChatTopics#58b215b7: %w", err)
	}
	return l.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (l *LoadDirectMessagesChatTopicsRequest) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode loadDirectMessagesChatTopics#58b215b7 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode loadDirectMessagesChatTopics#58b215b7: field chat_id: %w", err)
		}
		l.ChatID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode loadDirectMessagesChatTopics#58b215b7: field limit: %w", err)
		}
		l.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (l *LoadDirectMessagesChatTopicsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if l == nil {
		return fmt.Errorf("can't encode loadDirectMessagesChatTopics#58b215b7 as nil")
	}
	b.ObjStart()
	b.PutID("loadDirectMessagesChatTopics")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(l.ChatID)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(l.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (l *LoadDirectMessagesChatTopicsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if l == nil {
		return fmt.Errorf("can't decode loadDirectMessagesChatTopics#58b215b7 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("loadDirectMessagesChatTopics"); err != nil {
				return fmt.Errorf("unable to decode loadDirectMessagesChatTopics#58b215b7: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode loadDirectMessagesChatTopics#58b215b7: field chat_id: %w", err)
			}
			l.ChatID = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode loadDirectMessagesChatTopics#58b215b7: field limit: %w", err)
			}
			l.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (l *LoadDirectMessagesChatTopicsRequest) GetChatID() (value int64) {
	if l == nil {
		return
	}
	return l.ChatID
}

// GetLimit returns value of Limit field.
func (l *LoadDirectMessagesChatTopicsRequest) GetLimit() (value int32) {
	if l == nil {
		return
	}
	return l.Limit
}

// LoadDirectMessagesChatTopics invokes method loadDirectMessagesChatTopics#58b215b7 returning error if any.
func (c *Client) LoadDirectMessagesChatTopics(ctx context.Context, request *LoadDirectMessagesChatTopicsRequest) error {
	var ok Ok

	if err := c.rpc.Invoke(ctx, request, &ok); err != nil {
		return err
	}
	return nil
}
