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

// UpgradedGiftModelCount represents TL type `upgradedGiftModelCount#bccfb679`.
type UpgradedGiftModelCount struct {
	// The model
	Model UpgradedGiftModel
	// Total number of gifts with the model
	TotalCount int32
}

// UpgradedGiftModelCountTypeID is TL type id of UpgradedGiftModelCount.
const UpgradedGiftModelCountTypeID = 0xbccfb679

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

func (u *UpgradedGiftModelCount) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Model.Zero()) {
		return false
	}
	if !(u.TotalCount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (u *UpgradedGiftModelCount) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "upgradedGiftModelCount",
		ID:   UpgradedGiftModelCountTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Model",
			SchemaName: "model",
		},
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *UpgradedGiftModelCount) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftModelCount#bccfb679 as nil")
	}
	b.PutID(UpgradedGiftModelCountTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *UpgradedGiftModelCount) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftModelCount#bccfb679 as nil")
	}
	if err := u.Model.Encode(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftModelCount#bccfb679: field model: %w", err)
	}
	b.PutInt32(u.TotalCount)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (u *UpgradedGiftModelCount) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGiftModelCount#bccfb679 to nil")
	}
	{
		if err := u.Model.Decode(b); err != nil {
			return fmt.Errorf("unable to decode upgradedGiftModelCount#bccfb679: field model: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGiftModelCount#bccfb679: field total_count: %w", err)
		}
		u.TotalCount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (u *UpgradedGiftModelCount) EncodeTDLibJSON(b tdjson.Encoder) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftModelCount#bccfb679 as nil")
	}
	b.ObjStart()
	b.PutID("upgradedGiftModelCount")
	b.Comma()
	b.FieldStart("model")
	if err := u.Model.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftModelCount#bccfb679: field model: %w", err)
	}
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(u.TotalCount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("upgradedGiftModelCount"); err != nil {
				return fmt.Errorf("unable to decode upgradedGiftModelCount#bccfb679: %w", err)
			}
		case "model":
			if err := u.Model.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode upgradedGiftModelCount#bccfb679: field model: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGiftModelCount#bccfb679: field total_count: %w", err)
			}
			u.TotalCount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetTotalCount returns value of TotalCount field.
func (u *UpgradedGiftModelCount) GetTotalCount() (value int32) {
	if u == nil {
		return
	}
	return u.TotalCount
}
