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

// GetTrendingStickerSetsRequest represents TL type `getTrendingStickerSets#e058455e`.
type GetTrendingStickerSetsRequest struct {
	// Type of the sticker sets to return
	StickerType StickerTypeClass
	// The offset from which to return the sticker sets; must be non-negative
	Offset int32
	// The maximum number of sticker sets to be returned; up to 100. For optimal performance,
	// the number of returned sticker sets is chosen by TDLib and can be smaller than the
	// specified limit, even if the end of the list has not been reached
	Limit int32
}

// GetTrendingStickerSetsRequestTypeID is TL type id of GetTrendingStickerSetsRequest.
const GetTrendingStickerSetsRequestTypeID = 0xe058455e

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

func (g *GetTrendingStickerSetsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.StickerType == nil) {
		return false
	}
	if !(g.Offset == 0) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetTrendingStickerSetsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getTrendingStickerSets",
		ID:   GetTrendingStickerSetsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StickerType",
			SchemaName: "sticker_type",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetTrendingStickerSetsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getTrendingStickerSets#e058455e as nil")
	}
	b.PutID(GetTrendingStickerSetsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetTrendingStickerSetsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getTrendingStickerSets#e058455e as nil")
	}
	if g.StickerType == nil {
		return fmt.Errorf("unable to encode getTrendingStickerSets#e058455e: field sticker_type is nil")
	}
	if err := g.StickerType.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getTrendingStickerSets#e058455e: field sticker_type: %w", err)
	}
	b.PutInt32(g.Offset)
	b.PutInt32(g.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetTrendingStickerSetsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getTrendingStickerSets#e058455e to nil")
	}
	{
		value, err := DecodeStickerType(b)
		if err != nil {
			return fmt.Errorf("unable to decode getTrendingStickerSets#e058455e: field sticker_type: %w", err)
		}
		g.StickerType = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getTrendingStickerSets#e058455e: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getTrendingStickerSets#e058455e: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

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

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getTrendingStickerSets"); err != nil {
				return fmt.Errorf("unable to decode getTrendingStickerSets#e058455e: %w", err)
			}
		case "sticker_type":
			value, err := DecodeTDLibJSONStickerType(b)
			if err != nil {
				return fmt.Errorf("unable to decode getTrendingStickerSets#e058455e: field sticker_type: %w", err)
			}
			g.StickerType = value
		case "offset":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getTrendingStickerSets#e058455e: field offset: %w", err)
			}
			g.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getTrendingStickerSets#e058455e: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetStickerType returns value of StickerType field.
func (g *GetTrendingStickerSetsRequest) GetStickerType() (value StickerTypeClass) {
	if g == nil {
		return
	}
	return g.StickerType
}

// GetOffset returns value of Offset field.
func (g *GetTrendingStickerSetsRequest) GetOffset() (value int32) {
	if g == nil {
		return
	}
	return g.Offset
}

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

// GetTrendingStickerSets invokes method getTrendingStickerSets#e058455e returning error if any.
func (c *Client) GetTrendingStickerSets(ctx context.Context, request *GetTrendingStickerSetsRequest) (*TrendingStickerSets, error) {
	var result TrendingStickerSets

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