// 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{}
)

// LoadChatsRequest represents TL type `loadChats#8f9b7d7b`.
type LoadChatsRequest struct {
	// The chat list in which to load chats; pass null to load chats from the main chat list
	ChatList ChatListClass
	// The maximum number of chats to be loaded. For optimal performance, the number of
	// loaded chats is chosen by TDLib and can be smaller than the specified limit, even if
	// the end of the list is not reached
	Limit int32
}

// LoadChatsRequestTypeID is TL type id of LoadChatsRequest.
const LoadChatsRequestTypeID = 0x8f9b7d7b

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (l *LoadChatsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "loadChats",
		ID:   LoadChatsRequestTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatList",
			SchemaName: "chat_list",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (l *LoadChatsRequest) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode loadChats#8f9b7d7b as nil")
	}
	b.PutID(LoadChatsRequestTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *LoadChatsRequest) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode loadChats#8f9b7d7b as nil")
	}
	if l.ChatList == nil {
		return fmt.Errorf("unable to encode loadChats#8f9b7d7b: field chat_list is nil")
	}
	if err := l.ChatList.Encode(b); err != nil {
		return fmt.Errorf("unable to encode loadChats#8f9b7d7b: field chat_list: %w", err)
	}
	b.PutInt32(l.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (l *LoadChatsRequest) Decode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode loadChats#8f9b7d7b to nil")
	}
	if err := b.ConsumeID(LoadChatsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode loadChats#8f9b7d7b: %w", err)
	}
	return l.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (l *LoadChatsRequest) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode loadChats#8f9b7d7b to nil")
	}
	{
		value, err := DecodeChatList(b)
		if err != nil {
			return fmt.Errorf("unable to decode loadChats#8f9b7d7b: field chat_list: %w", err)
		}
		l.ChatList = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode loadChats#8f9b7d7b: field limit: %w", err)
		}
		l.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (l *LoadChatsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if l == nil {
		return fmt.Errorf("can't encode loadChats#8f9b7d7b as nil")
	}
	b.ObjStart()
	b.PutID("loadChats")
	b.Comma()
	b.FieldStart("chat_list")
	if l.ChatList == nil {
		return fmt.Errorf("unable to encode loadChats#8f9b7d7b: field chat_list is nil")
	}
	if err := l.ChatList.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode loadChats#8f9b7d7b: field chat_list: %w", err)
	}
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(l.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (l *LoadChatsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if l == nil {
		return fmt.Errorf("can't decode loadChats#8f9b7d7b to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("loadChats"); err != nil {
				return fmt.Errorf("unable to decode loadChats#8f9b7d7b: %w", err)
			}
		case "chat_list":
			value, err := DecodeTDLibJSONChatList(b)
			if err != nil {
				return fmt.Errorf("unable to decode loadChats#8f9b7d7b: field chat_list: %w", err)
			}
			l.ChatList = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode loadChats#8f9b7d7b: field limit: %w", err)
			}
			l.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatList returns value of ChatList field.
func (l *LoadChatsRequest) GetChatList() (value ChatListClass) {
	if l == nil {
		return
	}
	return l.ChatList
}

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

// LoadChats invokes method loadChats#8f9b7d7b returning error if any.
func (c *Client) LoadChats(ctx context.Context, request *LoadChatsRequest) error {
	var ok Ok

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