// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// PaymentsStarGiftUpgradePreview represents TL type `payments.starGiftUpgradePreview#3de1dfed`.
// A preview of the possible attributes (chosen randomly) a gift »¹ can receive after
// upgrading it to a collectible gift »², see here »³ for more info.
//
// Links:
//  1. https://core.telegram.org/api/gifts
//  2. https://core.telegram.org/api/gifts#collectible-gifts
//  3. https://core.telegram.org/api/gifts#collectible-gifts
//
// See https://core.telegram.org/constructor/payments.starGiftUpgradePreview for reference.
type PaymentsStarGiftUpgradePreview struct {
	// Possible gift attributes
	SampleAttributes []StarGiftAttributeClass
	// Prices field of PaymentsStarGiftUpgradePreview.
	Prices []StarGiftUpgradePrice
	// NextPrices field of PaymentsStarGiftUpgradePreview.
	NextPrices []StarGiftUpgradePrice
}

// PaymentsStarGiftUpgradePreviewTypeID is TL type id of PaymentsStarGiftUpgradePreview.
const PaymentsStarGiftUpgradePreviewTypeID = 0x3de1dfed

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

func (s *PaymentsStarGiftUpgradePreview) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.SampleAttributes == nil) {
		return false
	}
	if !(s.Prices == nil) {
		return false
	}
	if !(s.NextPrices == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsStarGiftUpgradePreview from given interface.
func (s *PaymentsStarGiftUpgradePreview) FillFrom(from interface {
	GetSampleAttributes() (value []StarGiftAttributeClass)
	GetPrices() (value []StarGiftUpgradePrice)
	GetNextPrices() (value []StarGiftUpgradePrice)
}) {
	s.SampleAttributes = from.GetSampleAttributes()
	s.Prices = from.GetPrices()
	s.NextPrices = from.GetNextPrices()
}

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

// TypeName returns name of type in TL schema.
func (*PaymentsStarGiftUpgradePreview) TypeName() string {
	return "payments.starGiftUpgradePreview"
}

// TypeInfo returns info about TL type.
func (s *PaymentsStarGiftUpgradePreview) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.starGiftUpgradePreview",
		ID:   PaymentsStarGiftUpgradePreviewTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SampleAttributes",
			SchemaName: "sample_attributes",
		},
		{
			Name:       "Prices",
			SchemaName: "prices",
		},
		{
			Name:       "NextPrices",
			SchemaName: "next_prices",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *PaymentsStarGiftUpgradePreview) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftUpgradePreview#3de1dfed as nil")
	}
	b.PutID(PaymentsStarGiftUpgradePreviewTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsStarGiftUpgradePreview) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftUpgradePreview#3de1dfed as nil")
	}
	b.PutVectorHeader(len(s.SampleAttributes))
	for idx, v := range s.SampleAttributes {
		if v == nil {
			return fmt.Errorf("unable to encode payments.starGiftUpgradePreview#3de1dfed: field sample_attributes element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starGiftUpgradePreview#3de1dfed: field sample_attributes element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Prices))
	for idx, v := range s.Prices {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starGiftUpgradePreview#3de1dfed: field prices element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.NextPrices))
	for idx, v := range s.NextPrices {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starGiftUpgradePreview#3de1dfed: field next_prices element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *PaymentsStarGiftUpgradePreview) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.starGiftUpgradePreview#3de1dfed to nil")
	}
	if err := b.ConsumeID(PaymentsStarGiftUpgradePreviewTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.starGiftUpgradePreview#3de1dfed: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *PaymentsStarGiftUpgradePreview) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.starGiftUpgradePreview#3de1dfed to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starGiftUpgradePreview#3de1dfed: field sample_attributes: %w", err)
		}

		if headerLen > 0 {
			s.SampleAttributes = make([]StarGiftAttributeClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeStarGiftAttribute(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.starGiftUpgradePreview#3de1dfed: field sample_attributes: %w", err)
			}
			s.SampleAttributes = append(s.SampleAttributes, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starGiftUpgradePreview#3de1dfed: field prices: %w", err)
		}

		if headerLen > 0 {
			s.Prices = make([]StarGiftUpgradePrice, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarGiftUpgradePrice
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode payments.starGiftUpgradePreview#3de1dfed: field prices: %w", err)
			}
			s.Prices = append(s.Prices, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starGiftUpgradePreview#3de1dfed: field next_prices: %w", err)
		}

		if headerLen > 0 {
			s.NextPrices = make([]StarGiftUpgradePrice, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarGiftUpgradePrice
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode payments.starGiftUpgradePreview#3de1dfed: field next_prices: %w", err)
			}
			s.NextPrices = append(s.NextPrices, value)
		}
	}
	return nil
}

// GetSampleAttributes returns value of SampleAttributes field.
func (s *PaymentsStarGiftUpgradePreview) GetSampleAttributes() (value []StarGiftAttributeClass) {
	if s == nil {
		return
	}
	return s.SampleAttributes
}

// GetPrices returns value of Prices field.
func (s *PaymentsStarGiftUpgradePreview) GetPrices() (value []StarGiftUpgradePrice) {
	if s == nil {
		return
	}
	return s.Prices
}

// GetNextPrices returns value of NextPrices field.
func (s *PaymentsStarGiftUpgradePreview) GetNextPrices() (value []StarGiftUpgradePrice) {
	if s == nil {
		return
	}
	return s.NextPrices
}

// MapSampleAttributes returns field SampleAttributes wrapped in StarGiftAttributeClassArray helper.
func (s *PaymentsStarGiftUpgradePreview) MapSampleAttributes() (value StarGiftAttributeClassArray) {
	return StarGiftAttributeClassArray(s.SampleAttributes)
}
