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

// StickerSets represents TL type `stickerSets#b8a6490d`.
type StickerSets struct {
	// Approximate total number of sticker sets found
	TotalCount int32
	// List of sticker sets
	Sets []StickerSetInfo
}

// StickerSetsTypeID is TL type id of StickerSets.
const StickerSetsTypeID = 0xb8a6490d

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

func (s *StickerSets) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.TotalCount == 0) {
		return false
	}
	if !(s.Sets == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StickerSets) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stickerSets",
		ID:   StickerSetsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Sets",
			SchemaName: "sets",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StickerSets) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode stickerSets#b8a6490d as nil")
	}
	b.PutID(StickerSetsTypeID)
	return s.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (s *StickerSets) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode stickerSets#b8a6490d to nil")
	}
	if err := b.ConsumeID(StickerSetsTypeID); err != nil {
		return fmt.Errorf("unable to decode stickerSets#b8a6490d: %w", err)
	}
	return s.DecodeBare(b)
}

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

		if headerLen > 0 {
			s.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 stickerSets#b8a6490d: field sets: %w", err)
			}
			s.Sets = append(s.Sets, value)
		}
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StickerSets) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode stickerSets#b8a6490d to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("stickerSets"); err != nil {
				return fmt.Errorf("unable to decode stickerSets#b8a6490d: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode stickerSets#b8a6490d: field total_count: %w", err)
			}
			s.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 stickerSets#b8a6490d: field sets: %w", err)
				}
				s.Sets = append(s.Sets, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode stickerSets#b8a6490d: field sets: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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