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

// UpgradedGiftSymbol represents TL type `upgradedGiftSymbol#4340c5af`.
type UpgradedGiftSymbol struct {
	// Name of the symbol
	Name string
	// The sticker representing the symbol
	Sticker Sticker
	// The number of upgraded gifts that receive this symbol for each 1000 gifts upgraded
	RarityPerMille int32
}

// UpgradedGiftSymbolTypeID is TL type id of UpgradedGiftSymbol.
const UpgradedGiftSymbolTypeID = 0x4340c5af

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

func (u *UpgradedGiftSymbol) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Name == "") {
		return false
	}
	if !(u.Sticker.Zero()) {
		return false
	}
	if !(u.RarityPerMille == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (u *UpgradedGiftSymbol) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "upgradedGiftSymbol",
		ID:   UpgradedGiftSymbolTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "Sticker",
			SchemaName: "sticker",
		},
		{
			Name:       "RarityPerMille",
			SchemaName: "rarity_per_mille",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *UpgradedGiftSymbol) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftSymbol#4340c5af as nil")
	}
	b.PutID(UpgradedGiftSymbolTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *UpgradedGiftSymbol) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftSymbol#4340c5af as nil")
	}
	b.PutString(u.Name)
	if err := u.Sticker.Encode(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftSymbol#4340c5af: field sticker: %w", err)
	}
	b.PutInt32(u.RarityPerMille)
	return nil
}

// Decode implements bin.Decoder.
func (u *UpgradedGiftSymbol) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGiftSymbol#4340c5af to nil")
	}
	if err := b.ConsumeID(UpgradedGiftSymbolTypeID); err != nil {
		return fmt.Errorf("unable to decode upgradedGiftSymbol#4340c5af: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *UpgradedGiftSymbol) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGiftSymbol#4340c5af to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGiftSymbol#4340c5af: field name: %w", err)
		}
		u.Name = value
	}
	{
		if err := u.Sticker.Decode(b); err != nil {
			return fmt.Errorf("unable to decode upgradedGiftSymbol#4340c5af: field sticker: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGiftSymbol#4340c5af: field rarity_per_mille: %w", err)
		}
		u.RarityPerMille = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (u *UpgradedGiftSymbol) EncodeTDLibJSON(b tdjson.Encoder) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftSymbol#4340c5af as nil")
	}
	b.ObjStart()
	b.PutID("upgradedGiftSymbol")
	b.Comma()
	b.FieldStart("name")
	b.PutString(u.Name)
	b.Comma()
	b.FieldStart("sticker")
	if err := u.Sticker.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftSymbol#4340c5af: field sticker: %w", err)
	}
	b.Comma()
	b.FieldStart("rarity_per_mille")
	b.PutInt32(u.RarityPerMille)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (u *UpgradedGiftSymbol) DecodeTDLibJSON(b tdjson.Decoder) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGiftSymbol#4340c5af to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("upgradedGiftSymbol"); err != nil {
				return fmt.Errorf("unable to decode upgradedGiftSymbol#4340c5af: %w", err)
			}
		case "name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGiftSymbol#4340c5af: field name: %w", err)
			}
			u.Name = value
		case "sticker":
			if err := u.Sticker.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode upgradedGiftSymbol#4340c5af: field sticker: %w", err)
			}
		case "rarity_per_mille":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGiftSymbol#4340c5af: field rarity_per_mille: %w", err)
			}
			u.RarityPerMille = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetName returns value of Name field.
func (u *UpgradedGiftSymbol) GetName() (value string) {
	if u == nil {
		return
	}
	return u.Name
}

// GetSticker returns value of Sticker field.
func (u *UpgradedGiftSymbol) GetSticker() (value Sticker) {
	if u == nil {
		return
	}
	return u.Sticker
}

// GetRarityPerMille returns value of RarityPerMille field.
func (u *UpgradedGiftSymbol) GetRarityPerMille() (value int32) {
	if u == nil {
		return
	}
	return u.RarityPerMille
}
