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

// TrendingStickerSets represents TL type `trendingStickerSets#298f12cc`.
type TrendingStickerSets struct {
	// Approximate total number of trending sticker sets
	TotalCount int32
	// List of trending sticker sets
	Sets []StickerSetInfo
	// True, if the list contains sticker sets with premium stickers
	IsPremium bool
}

// TrendingStickerSetsTypeID is TL type id of TrendingStickerSets.
const TrendingStickerSetsTypeID = 0x298f12cc

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

func (t *TrendingStickerSets) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.TotalCount == 0) {
		return false
	}
	if !(t.Sets == nil) {
		return false
	}
	if !(t.IsPremium == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TrendingStickerSets) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "trendingStickerSets",
		ID:   TrendingStickerSetsTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Sets",
			SchemaName: "sets",
		},
		{
			Name:       "IsPremium",
			SchemaName: "is_premium",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TrendingStickerSets) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode trendingStickerSets#298f12cc as nil")
	}
	b.PutID(TrendingStickerSetsTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TrendingStickerSets) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode trendingStickerSets#298f12cc as nil")
	}
	b.PutInt32(t.TotalCount)
	b.PutInt(len(t.Sets))
	for idx, v := range t.Sets {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare trendingStickerSets#298f12cc: field sets element with index %d: %w", idx, err)
		}
	}
	b.PutBool(t.IsPremium)
	return nil
}

// Decode implements bin.Decoder.
func (t *TrendingStickerSets) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode trendingStickerSets#298f12cc to nil")
	}
	if err := b.ConsumeID(TrendingStickerSetsTypeID); err != nil {
		return fmt.Errorf("unable to decode trendingStickerSets#298f12cc: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *TrendingStickerSets) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode trendingStickerSets#298f12cc to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode trendingStickerSets#298f12cc: field total_count: %w", err)
		}
		t.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode trendingStickerSets#298f12cc: field sets: %w", err)
		}

		if headerLen > 0 {
			t.Sets = make([]StickerSetInfo, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StickerSetInfo
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare trendingStickerSets#298f12cc: field sets: %w", err)
			}
			t.Sets = append(t.Sets, value)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode trendingStickerSets#298f12cc: field is_premium: %w", err)
		}
		t.IsPremium = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *TrendingStickerSets) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode trendingStickerSets#298f12cc as nil")
	}
	b.ObjStart()
	b.PutID("trendingStickerSets")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(t.TotalCount)
	b.Comma()
	b.FieldStart("sets")
	b.ArrStart()
	for idx, v := range t.Sets {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode trendingStickerSets#298f12cc: field sets element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("is_premium")
	b.PutBool(t.IsPremium)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *TrendingStickerSets) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode trendingStickerSets#298f12cc to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("trendingStickerSets"); err != nil {
				return fmt.Errorf("unable to decode trendingStickerSets#298f12cc: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode trendingStickerSets#298f12cc: field total_count: %w", err)
			}
			t.TotalCount = value
		case "sets":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value StickerSetInfo
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode trendingStickerSets#298f12cc: field sets: %w", err)
				}
				t.Sets = append(t.Sets, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode trendingStickerSets#298f12cc: field sets: %w", err)
			}
		case "is_premium":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode trendingStickerSets#298f12cc: field is_premium: %w", err)
			}
			t.IsPremium = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTotalCount returns value of TotalCount field.
func (t *TrendingStickerSets) GetTotalCount() (value int32) {
	if t == nil {
		return
	}
	return t.TotalCount
}

// GetSets returns value of Sets field.
func (t *TrendingStickerSets) GetSets() (value []StickerSetInfo) {
	if t == nil {
		return
	}
	return t.Sets
}

// GetIsPremium returns value of IsPremium field.
func (t *TrendingStickerSets) GetIsPremium() (value bool) {
	if t == nil {
		return
	}
	return t.IsPremium
}
