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

// GetChatsRequest represents TL type `getChats#c604bac2`.
type GetChatsRequest struct {
	// The chat list in which to return chats; pass null to get chats from the main chat list
	ChatList ChatListClass
	// The maximum number of chats to be returned
	Limit int32
}

// GetChatsRequestTypeID is TL type id of GetChatsRequest.
const GetChatsRequestTypeID = 0xc604bac2

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetChatsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getChats",
		ID:   GetChatsRequestTypeID,
	}
	if g == 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 (g *GetChatsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChats#c604bac2 as nil")
	}
	b.PutID(GetChatsRequestTypeID)
	return g.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (g *GetChatsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChats#c604bac2 to nil")
	}
	if err := b.ConsumeID(GetChatsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getChats#c604bac2: %w", err)
	}
	return g.DecodeBare(b)
}

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

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetChatsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getChats#c604bac2 to nil")
	}

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

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

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

// GetChats invokes method getChats#c604bac2 returning error if any.
func (c *Client) GetChats(ctx context.Context, request *GetChatsRequest) (*Chats, error) {
	var result Chats

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