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

// UpgradedGiftBackdrop represents TL type `upgradedGiftBackdrop#e973c3a6`.
type UpgradedGiftBackdrop struct {
	// Unique identifier of the backdrop
	ID int32
	// Name of the backdrop
	Name string
	// Colors of the backdrop
	Colors UpgradedGiftBackdropColors
	// The number of upgraded gifts that receive this backdrop for each 1000 gifts upgraded
	RarityPerMille int32
}

// UpgradedGiftBackdropTypeID is TL type id of UpgradedGiftBackdrop.
const UpgradedGiftBackdropTypeID = 0xe973c3a6

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (u *UpgradedGiftBackdrop) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "upgradedGiftBackdrop",
		ID:   UpgradedGiftBackdropTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "Colors",
			SchemaName: "colors",
		},
		{
			Name:       "RarityPerMille",
			SchemaName: "rarity_per_mille",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *UpgradedGiftBackdrop) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftBackdrop#e973c3a6 as nil")
	}
	b.PutID(UpgradedGiftBackdropTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *UpgradedGiftBackdrop) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftBackdrop#e973c3a6 as nil")
	}
	b.PutInt32(u.ID)
	b.PutString(u.Name)
	if err := u.Colors.Encode(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftBackdrop#e973c3a6: field colors: %w", err)
	}
	b.PutInt32(u.RarityPerMille)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (u *UpgradedGiftBackdrop) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGiftBackdrop#e973c3a6 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGiftBackdrop#e973c3a6: field id: %w", err)
		}
		u.ID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGiftBackdrop#e973c3a6: field name: %w", err)
		}
		u.Name = value
	}
	{
		if err := u.Colors.Decode(b); err != nil {
			return fmt.Errorf("unable to decode upgradedGiftBackdrop#e973c3a6: field colors: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGiftBackdrop#e973c3a6: field rarity_per_mille: %w", err)
		}
		u.RarityPerMille = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (u *UpgradedGiftBackdrop) EncodeTDLibJSON(b tdjson.Encoder) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftBackdrop#e973c3a6 as nil")
	}
	b.ObjStart()
	b.PutID("upgradedGiftBackdrop")
	b.Comma()
	b.FieldStart("id")
	b.PutInt32(u.ID)
	b.Comma()
	b.FieldStart("name")
	b.PutString(u.Name)
	b.Comma()
	b.FieldStart("colors")
	if err := u.Colors.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftBackdrop#e973c3a6: field colors: %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 *UpgradedGiftBackdrop) DecodeTDLibJSON(b tdjson.Decoder) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGiftBackdrop#e973c3a6 to nil")
	}

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

// GetID returns value of ID field.
func (u *UpgradedGiftBackdrop) GetID() (value int32) {
	if u == nil {
		return
	}
	return u.ID
}

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

// GetColors returns value of Colors field.
func (u *UpgradedGiftBackdrop) GetColors() (value UpgradedGiftBackdropColors) {
	if u == nil {
		return
	}
	return u.Colors
}

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