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

// GetTopChatsRequest represents TL type `getTopChats#e8d95221`.
type GetTopChatsRequest struct {
	// Category of chats to be returned
	Category TopChatCategoryClass
	// The maximum number of chats to be returned; up to 30
	Limit int32
}

// GetTopChatsRequestTypeID is TL type id of GetTopChatsRequest.
const GetTopChatsRequestTypeID = 0xe8d95221

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetTopChatsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getTopChats",
		ID:   GetTopChatsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Category",
			SchemaName: "category",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetTopChatsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getTopChats#e8d95221 as nil")
	}
	b.PutID(GetTopChatsRequestTypeID)
	return g.EncodeBare(b)
}

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

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

// DecodeBare implements bin.BareDecoder.
func (g *GetTopChatsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getTopChats#e8d95221 to nil")
	}
	{
		value, err := DecodeTopChatCategory(b)
		if err != nil {
			return fmt.Errorf("unable to decode getTopChats#e8d95221: field category: %w", err)
		}
		g.Category = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getTopChats#e8d95221: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetTopChatsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getTopChats#e8d95221 as nil")
	}
	b.ObjStart()
	b.PutID("getTopChats")
	b.Comma()
	b.FieldStart("category")
	if g.Category == nil {
		return fmt.Errorf("unable to encode getTopChats#e8d95221: field category is nil")
	}
	if err := g.Category.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getTopChats#e8d95221: field category: %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 *GetTopChatsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getTopChats#e8d95221 to nil")
	}

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

// GetCategory returns value of Category field.
func (g *GetTopChatsRequest) GetCategory() (value TopChatCategoryClass) {
	if g == nil {
		return
	}
	return g.Category
}

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

// GetTopChats invokes method getTopChats#e8d95221 returning error if any.
func (c *Client) GetTopChats(ctx context.Context, request *GetTopChatsRequest) (*Chats, error) {
	var result Chats

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