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

// UpgradedGift represents TL type `upgradedGift#3b2d6b9a`.
type UpgradedGift struct {
	// Unique identifier of the gift
	ID int64
	// The title of the upgraded gift
	Title string
	// Unique name of the upgraded gift that can be used with internalLinkTypeUpgradedGift or
	// sendResoldGift
	Name string
	// Unique number of the upgraded gift among gifts upgraded from the same gift
	Number int32
	// Total number of gifts that were upgraded from the same gift
	TotalUpgradedCount int32
	// The maximum number of gifts that can be upgraded from the same gift
	MaxUpgradedCount int32
	// Identifier of the user or the chat that owns the upgraded gift; may be null if none or
	// unknown
	OwnerID MessageSenderClass
	// Address of the gift NFT owner in TON blockchain; may be empty if none. Append the
	// address to getOption("ton_blockchain_explorer_url") to get a link with information
	// about the address
	OwnerAddress string
	// Name of the owner for the case when owner identifier and address aren't known
	OwnerName string
	// Address of the gift NFT in TON blockchain; may be empty if none. Append the address to
	// getOption("ton_blockchain_explorer_url") to get a link with information about the
	// address
	GiftAddress string
	// Model of the upgraded gift
	Model UpgradedGiftModel
	// Symbol of the upgraded gift
	Symbol UpgradedGiftSymbol
	// Backdrop of the upgraded gift
	Backdrop UpgradedGiftBackdrop
	// Information about the originally sent gift; may be null if unknown
	OriginalDetails UpgradedGiftOriginalDetails
	// Number of Telegram Stars that must be paid to buy the gift and send it to someone
	// else; 0 if resale isn't possible
	ResaleStarCount int64
}

// UpgradedGiftTypeID is TL type id of UpgradedGift.
const UpgradedGiftTypeID = 0x3b2d6b9a

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

func (u *UpgradedGift) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.ID == 0) {
		return false
	}
	if !(u.Title == "") {
		return false
	}
	if !(u.Name == "") {
		return false
	}
	if !(u.Number == 0) {
		return false
	}
	if !(u.TotalUpgradedCount == 0) {
		return false
	}
	if !(u.MaxUpgradedCount == 0) {
		return false
	}
	if !(u.OwnerID == nil) {
		return false
	}
	if !(u.OwnerAddress == "") {
		return false
	}
	if !(u.OwnerName == "") {
		return false
	}
	if !(u.GiftAddress == "") {
		return false
	}
	if !(u.Model.Zero()) {
		return false
	}
	if !(u.Symbol.Zero()) {
		return false
	}
	if !(u.Backdrop.Zero()) {
		return false
	}
	if !(u.OriginalDetails.Zero()) {
		return false
	}
	if !(u.ResaleStarCount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (u *UpgradedGift) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "upgradedGift",
		ID:   UpgradedGiftTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "Number",
			SchemaName: "number",
		},
		{
			Name:       "TotalUpgradedCount",
			SchemaName: "total_upgraded_count",
		},
		{
			Name:       "MaxUpgradedCount",
			SchemaName: "max_upgraded_count",
		},
		{
			Name:       "OwnerID",
			SchemaName: "owner_id",
		},
		{
			Name:       "OwnerAddress",
			SchemaName: "owner_address",
		},
		{
			Name:       "OwnerName",
			SchemaName: "owner_name",
		},
		{
			Name:       "GiftAddress",
			SchemaName: "gift_address",
		},
		{
			Name:       "Model",
			SchemaName: "model",
		},
		{
			Name:       "Symbol",
			SchemaName: "symbol",
		},
		{
			Name:       "Backdrop",
			SchemaName: "backdrop",
		},
		{
			Name:       "OriginalDetails",
			SchemaName: "original_details",
		},
		{
			Name:       "ResaleStarCount",
			SchemaName: "resale_star_count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *UpgradedGift) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGift#3b2d6b9a as nil")
	}
	b.PutID(UpgradedGiftTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *UpgradedGift) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGift#3b2d6b9a as nil")
	}
	b.PutLong(u.ID)
	b.PutString(u.Title)
	b.PutString(u.Name)
	b.PutInt32(u.Number)
	b.PutInt32(u.TotalUpgradedCount)
	b.PutInt32(u.MaxUpgradedCount)
	if u.OwnerID == nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field owner_id is nil")
	}
	if err := u.OwnerID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field owner_id: %w", err)
	}
	b.PutString(u.OwnerAddress)
	b.PutString(u.OwnerName)
	b.PutString(u.GiftAddress)
	if err := u.Model.Encode(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field model: %w", err)
	}
	if err := u.Symbol.Encode(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field symbol: %w", err)
	}
	if err := u.Backdrop.Encode(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field backdrop: %w", err)
	}
	if err := u.OriginalDetails.Encode(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field original_details: %w", err)
	}
	b.PutInt53(u.ResaleStarCount)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (u *UpgradedGift) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGift#3b2d6b9a to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field id: %w", err)
		}
		u.ID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field title: %w", err)
		}
		u.Title = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field name: %w", err)
		}
		u.Name = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field number: %w", err)
		}
		u.Number = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field total_upgraded_count: %w", err)
		}
		u.TotalUpgradedCount = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field max_upgraded_count: %w", err)
		}
		u.MaxUpgradedCount = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field owner_id: %w", err)
		}
		u.OwnerID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field owner_address: %w", err)
		}
		u.OwnerAddress = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field owner_name: %w", err)
		}
		u.OwnerName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field gift_address: %w", err)
		}
		u.GiftAddress = value
	}
	{
		if err := u.Model.Decode(b); err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field model: %w", err)
		}
	}
	{
		if err := u.Symbol.Decode(b); err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field symbol: %w", err)
		}
	}
	{
		if err := u.Backdrop.Decode(b); err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field backdrop: %w", err)
		}
	}
	{
		if err := u.OriginalDetails.Decode(b); err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field original_details: %w", err)
		}
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field resale_star_count: %w", err)
		}
		u.ResaleStarCount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (u *UpgradedGift) EncodeTDLibJSON(b tdjson.Encoder) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGift#3b2d6b9a as nil")
	}
	b.ObjStart()
	b.PutID("upgradedGift")
	b.Comma()
	b.FieldStart("id")
	b.PutLong(u.ID)
	b.Comma()
	b.FieldStart("title")
	b.PutString(u.Title)
	b.Comma()
	b.FieldStart("name")
	b.PutString(u.Name)
	b.Comma()
	b.FieldStart("number")
	b.PutInt32(u.Number)
	b.Comma()
	b.FieldStart("total_upgraded_count")
	b.PutInt32(u.TotalUpgradedCount)
	b.Comma()
	b.FieldStart("max_upgraded_count")
	b.PutInt32(u.MaxUpgradedCount)
	b.Comma()
	b.FieldStart("owner_id")
	if u.OwnerID == nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field owner_id is nil")
	}
	if err := u.OwnerID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field owner_id: %w", err)
	}
	b.Comma()
	b.FieldStart("owner_address")
	b.PutString(u.OwnerAddress)
	b.Comma()
	b.FieldStart("owner_name")
	b.PutString(u.OwnerName)
	b.Comma()
	b.FieldStart("gift_address")
	b.PutString(u.GiftAddress)
	b.Comma()
	b.FieldStart("model")
	if err := u.Model.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field model: %w", err)
	}
	b.Comma()
	b.FieldStart("symbol")
	if err := u.Symbol.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field symbol: %w", err)
	}
	b.Comma()
	b.FieldStart("backdrop")
	if err := u.Backdrop.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field backdrop: %w", err)
	}
	b.Comma()
	b.FieldStart("original_details")
	if err := u.OriginalDetails.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGift#3b2d6b9a: field original_details: %w", err)
	}
	b.Comma()
	b.FieldStart("resale_star_count")
	b.PutInt53(u.ResaleStarCount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (u *UpgradedGift) DecodeTDLibJSON(b tdjson.Decoder) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGift#3b2d6b9a to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("upgradedGift"); err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: %w", err)
			}
		case "id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field id: %w", err)
			}
			u.ID = value
		case "title":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field title: %w", err)
			}
			u.Title = value
		case "name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field name: %w", err)
			}
			u.Name = value
		case "number":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field number: %w", err)
			}
			u.Number = value
		case "total_upgraded_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field total_upgraded_count: %w", err)
			}
			u.TotalUpgradedCount = value
		case "max_upgraded_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field max_upgraded_count: %w", err)
			}
			u.MaxUpgradedCount = value
		case "owner_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field owner_id: %w", err)
			}
			u.OwnerID = value
		case "owner_address":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field owner_address: %w", err)
			}
			u.OwnerAddress = value
		case "owner_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field owner_name: %w", err)
			}
			u.OwnerName = value
		case "gift_address":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field gift_address: %w", err)
			}
			u.GiftAddress = value
		case "model":
			if err := u.Model.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field model: %w", err)
			}
		case "symbol":
			if err := u.Symbol.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field symbol: %w", err)
			}
		case "backdrop":
			if err := u.Backdrop.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field backdrop: %w", err)
			}
		case "original_details":
			if err := u.OriginalDetails.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field original_details: %w", err)
			}
		case "resale_star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGift#3b2d6b9a: field resale_star_count: %w", err)
			}
			u.ResaleStarCount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

// GetNumber returns value of Number field.
func (u *UpgradedGift) GetNumber() (value int32) {
	if u == nil {
		return
	}
	return u.Number
}

// GetTotalUpgradedCount returns value of TotalUpgradedCount field.
func (u *UpgradedGift) GetTotalUpgradedCount() (value int32) {
	if u == nil {
		return
	}
	return u.TotalUpgradedCount
}

// GetMaxUpgradedCount returns value of MaxUpgradedCount field.
func (u *UpgradedGift) GetMaxUpgradedCount() (value int32) {
	if u == nil {
		return
	}
	return u.MaxUpgradedCount
}

// GetOwnerID returns value of OwnerID field.
func (u *UpgradedGift) GetOwnerID() (value MessageSenderClass) {
	if u == nil {
		return
	}
	return u.OwnerID
}

// GetOwnerAddress returns value of OwnerAddress field.
func (u *UpgradedGift) GetOwnerAddress() (value string) {
	if u == nil {
		return
	}
	return u.OwnerAddress
}

// GetOwnerName returns value of OwnerName field.
func (u *UpgradedGift) GetOwnerName() (value string) {
	if u == nil {
		return
	}
	return u.OwnerName
}

// GetGiftAddress returns value of GiftAddress field.
func (u *UpgradedGift) GetGiftAddress() (value string) {
	if u == nil {
		return
	}
	return u.GiftAddress
}

// GetModel returns value of Model field.
func (u *UpgradedGift) GetModel() (value UpgradedGiftModel) {
	if u == nil {
		return
	}
	return u.Model
}

// GetSymbol returns value of Symbol field.
func (u *UpgradedGift) GetSymbol() (value UpgradedGiftSymbol) {
	if u == nil {
		return
	}
	return u.Symbol
}

// GetBackdrop returns value of Backdrop field.
func (u *UpgradedGift) GetBackdrop() (value UpgradedGiftBackdrop) {
	if u == nil {
		return
	}
	return u.Backdrop
}

// GetOriginalDetails returns value of OriginalDetails field.
func (u *UpgradedGift) GetOriginalDetails() (value UpgradedGiftOriginalDetails) {
	if u == nil {
		return
	}
	return u.OriginalDetails
}

// GetResaleStarCount returns value of ResaleStarCount field.
func (u *UpgradedGift) GetResaleStarCount() (value int64) {
	if u == nil {
		return
	}
	return u.ResaleStarCount
}
