// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// StarGiftCollection represents TL type `starGiftCollection#9d6b13b0`.
// Represents a star gift collection »¹.
//
// Links:
//  1. https://core.telegram.org/api/gifts#gift-collections
//
// See https://core.telegram.org/constructor/starGiftCollection for reference.
type StarGiftCollection struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// The ID of the collection.
	CollectionID int
	// Title of the collection.
	Title string
	// Optional icon for the collection, taken from the first gift in the collection.
	//
	// Use SetIcon and GetIcon helpers.
	Icon DocumentClass
	// Number of gifts in the collection.
	GiftsCount int
	// Field to use instead of collection_id when generating the hash to pass to payments
	// getStarGiftCollections¹.
	//
	// Links:
	//  1) https://core.telegram.org/method/payments.getStarGiftCollections
	Hash int64
}

// StarGiftCollectionTypeID is TL type id of StarGiftCollection.
const StarGiftCollectionTypeID = 0x9d6b13b0

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

func (s *StarGiftCollection) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.CollectionID == 0) {
		return false
	}
	if !(s.Title == "") {
		return false
	}
	if !(s.Icon == nil) {
		return false
	}
	if !(s.GiftsCount == 0) {
		return false
	}
	if !(s.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StarGiftCollection from given interface.
func (s *StarGiftCollection) FillFrom(from interface {
	GetCollectionID() (value int)
	GetTitle() (value string)
	GetIcon() (value DocumentClass, ok bool)
	GetGiftsCount() (value int)
	GetHash() (value int64)
}) {
	s.CollectionID = from.GetCollectionID()
	s.Title = from.GetTitle()
	if val, ok := from.GetIcon(); ok {
		s.Icon = val
	}

	s.GiftsCount = from.GetGiftsCount()
	s.Hash = from.GetHash()
}

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

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

// TypeInfo returns info about TL type.
func (s *StarGiftCollection) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starGiftCollection",
		ID:   StarGiftCollectionTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "CollectionID",
			SchemaName: "collection_id",
		},
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "Icon",
			SchemaName: "icon",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "GiftsCount",
			SchemaName: "gifts_count",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StarGiftCollection) SetFlags() {
	if !(s.Icon == nil) {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *StarGiftCollection) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starGiftCollection#9d6b13b0 as nil")
	}
	b.PutID(StarGiftCollectionTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarGiftCollection) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starGiftCollection#9d6b13b0 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starGiftCollection#9d6b13b0: field flags: %w", err)
	}
	b.PutInt(s.CollectionID)
	b.PutString(s.Title)
	if s.Flags.Has(0) {
		if s.Icon == nil {
			return fmt.Errorf("unable to encode starGiftCollection#9d6b13b0: field icon is nil")
		}
		if err := s.Icon.Encode(b); err != nil {
			return fmt.Errorf("unable to encode starGiftCollection#9d6b13b0: field icon: %w", err)
		}
	}
	b.PutInt(s.GiftsCount)
	b.PutLong(s.Hash)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StarGiftCollection) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starGiftCollection#9d6b13b0 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starGiftCollection#9d6b13b0: field flags: %w", err)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftCollection#9d6b13b0: field collection_id: %w", err)
		}
		s.CollectionID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftCollection#9d6b13b0: field title: %w", err)
		}
		s.Title = value
	}
	if s.Flags.Has(0) {
		value, err := DecodeDocument(b)
		if err != nil {
			return fmt.Errorf("unable to decode starGiftCollection#9d6b13b0: field icon: %w", err)
		}
		s.Icon = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftCollection#9d6b13b0: field gifts_count: %w", err)
		}
		s.GiftsCount = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftCollection#9d6b13b0: field hash: %w", err)
		}
		s.Hash = value
	}
	return nil
}

// GetCollectionID returns value of CollectionID field.
func (s *StarGiftCollection) GetCollectionID() (value int) {
	if s == nil {
		return
	}
	return s.CollectionID
}

// GetTitle returns value of Title field.
func (s *StarGiftCollection) GetTitle() (value string) {
	if s == nil {
		return
	}
	return s.Title
}

// SetIcon sets value of Icon conditional field.
func (s *StarGiftCollection) SetIcon(value DocumentClass) {
	s.Flags.Set(0)
	s.Icon = value
}

// GetIcon returns value of Icon conditional field and
// boolean which is true if field was set.
func (s *StarGiftCollection) GetIcon() (value DocumentClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.Icon, true
}

// GetGiftsCount returns value of GiftsCount field.
func (s *StarGiftCollection) GetGiftsCount() (value int) {
	if s == nil {
		return
	}
	return s.GiftsCount
}

// GetHash returns value of Hash field.
func (s *StarGiftCollection) GetHash() (value int64) {
	if s == nil {
		return
	}
	return s.Hash
}

// GetIconAsNotEmpty returns mapped value of Icon conditional field and
// boolean which is true if field was set.
func (s *StarGiftCollection) GetIconAsNotEmpty() (*Document, bool) {
	if value, ok := s.GetIcon(); ok {
		return value.AsNotEmpty()
	}
	return nil, false
}
