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

// UpgradedGiftModel represents TL type `upgradedGiftModel#5112584f`.
type UpgradedGiftModel struct {
	// Name of the model
	Name string
	// The sticker representing the upgraded gift
	Sticker Sticker
	// The number of upgraded gifts that receive this model for each 1000 gifts upgraded
	RarityPerMille int32
}

// UpgradedGiftModelTypeID is TL type id of UpgradedGiftModel.
const UpgradedGiftModelTypeID = 0x5112584f

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

func (u *UpgradedGiftModel) 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 *UpgradedGiftModel) String() string {
	if u == nil {
		return "UpgradedGiftModel(nil)"
	}
	type Alias UpgradedGiftModel
	return fmt.Sprintf("UpgradedGiftModel%+v", Alias(*u))
}

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

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

// TypeInfo returns info about TL type.
func (u *UpgradedGiftModel) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "upgradedGiftModel",
		ID:   UpgradedGiftModelTypeID,
	}
	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 *UpgradedGiftModel) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftModel#5112584f as nil")
	}
	b.PutID(UpgradedGiftModelTypeID)
	return u.EncodeBare(b)
}

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

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

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (u *UpgradedGiftModel) EncodeTDLibJSON(b tdjson.Encoder) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftModel#5112584f as nil")
	}
	b.ObjStart()
	b.PutID("upgradedGiftModel")
	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 upgradedGiftModel#5112584f: 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 *UpgradedGiftModel) DecodeTDLibJSON(b tdjson.Decoder) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGiftModel#5112584f to nil")
	}

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

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

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

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