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

// GiftUpgradePreview represents TL type `giftUpgradePreview#4319deef`.
type GiftUpgradePreview struct {
	// Examples of possible models that can be chosen for the gift after upgrade
	Models []UpgradedGiftModel
	// Examples of possible symbols that can be chosen for the gift after upgrade
	Symbols []UpgradedGiftSymbol
	// Examples of possible backdrops that can be chosen for the gift after upgrade
	Backdrops []UpgradedGiftBackdrop
}

// GiftUpgradePreviewTypeID is TL type id of GiftUpgradePreview.
const GiftUpgradePreviewTypeID = 0x4319deef

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

func (g *GiftUpgradePreview) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Models == nil) {
		return false
	}
	if !(g.Symbols == nil) {
		return false
	}
	if !(g.Backdrops == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GiftUpgradePreview) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "giftUpgradePreview",
		ID:   GiftUpgradePreviewTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Models",
			SchemaName: "models",
		},
		{
			Name:       "Symbols",
			SchemaName: "symbols",
		},
		{
			Name:       "Backdrops",
			SchemaName: "backdrops",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GiftUpgradePreview) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode giftUpgradePreview#4319deef as nil")
	}
	b.PutID(GiftUpgradePreviewTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GiftUpgradePreview) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode giftUpgradePreview#4319deef as nil")
	}
	b.PutInt(len(g.Models))
	for idx, v := range g.Models {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare giftUpgradePreview#4319deef: field models element with index %d: %w", idx, err)
		}
	}
	b.PutInt(len(g.Symbols))
	for idx, v := range g.Symbols {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare giftUpgradePreview#4319deef: field symbols element with index %d: %w", idx, err)
		}
	}
	b.PutInt(len(g.Backdrops))
	for idx, v := range g.Backdrops {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare giftUpgradePreview#4319deef: field backdrops element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *GiftUpgradePreview) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode giftUpgradePreview#4319deef to nil")
	}
	if err := b.ConsumeID(GiftUpgradePreviewTypeID); err != nil {
		return fmt.Errorf("unable to decode giftUpgradePreview#4319deef: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GiftUpgradePreview) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode giftUpgradePreview#4319deef to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode giftUpgradePreview#4319deef: field models: %w", err)
		}

		if headerLen > 0 {
			g.Models = make([]UpgradedGiftModel, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value UpgradedGiftModel
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare giftUpgradePreview#4319deef: field models: %w", err)
			}
			g.Models = append(g.Models, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode giftUpgradePreview#4319deef: field symbols: %w", err)
		}

		if headerLen > 0 {
			g.Symbols = make([]UpgradedGiftSymbol, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value UpgradedGiftSymbol
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare giftUpgradePreview#4319deef: field symbols: %w", err)
			}
			g.Symbols = append(g.Symbols, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode giftUpgradePreview#4319deef: field backdrops: %w", err)
		}

		if headerLen > 0 {
			g.Backdrops = make([]UpgradedGiftBackdrop, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value UpgradedGiftBackdrop
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare giftUpgradePreview#4319deef: field backdrops: %w", err)
			}
			g.Backdrops = append(g.Backdrops, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GiftUpgradePreview) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode giftUpgradePreview#4319deef as nil")
	}
	b.ObjStart()
	b.PutID("giftUpgradePreview")
	b.Comma()
	b.FieldStart("models")
	b.ArrStart()
	for idx, v := range g.Models {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode giftUpgradePreview#4319deef: field models element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("symbols")
	b.ArrStart()
	for idx, v := range g.Symbols {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode giftUpgradePreview#4319deef: field symbols element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("backdrops")
	b.ArrStart()
	for idx, v := range g.Backdrops {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode giftUpgradePreview#4319deef: field backdrops element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GiftUpgradePreview) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode giftUpgradePreview#4319deef to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("giftUpgradePreview"); err != nil {
				return fmt.Errorf("unable to decode giftUpgradePreview#4319deef: %w", err)
			}
		case "models":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value UpgradedGiftModel
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode giftUpgradePreview#4319deef: field models: %w", err)
				}
				g.Models = append(g.Models, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode giftUpgradePreview#4319deef: field models: %w", err)
			}
		case "symbols":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value UpgradedGiftSymbol
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode giftUpgradePreview#4319deef: field symbols: %w", err)
				}
				g.Symbols = append(g.Symbols, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode giftUpgradePreview#4319deef: field symbols: %w", err)
			}
		case "backdrops":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value UpgradedGiftBackdrop
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode giftUpgradePreview#4319deef: field backdrops: %w", err)
				}
				g.Backdrops = append(g.Backdrops, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode giftUpgradePreview#4319deef: field backdrops: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetModels returns value of Models field.
func (g *GiftUpgradePreview) GetModels() (value []UpgradedGiftModel) {
	if g == nil {
		return
	}
	return g.Models
}

// GetSymbols returns value of Symbols field.
func (g *GiftUpgradePreview) GetSymbols() (value []UpgradedGiftSymbol) {
	if g == nil {
		return
	}
	return g.Symbols
}

// GetBackdrops returns value of Backdrops field.
func (g *GiftUpgradePreview) GetBackdrops() (value []UpgradedGiftBackdrop) {
	if g == nil {
		return
	}
	return g.Backdrops
}
