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

// StarGiveawayPaymentOption represents TL type `starGiveawayPaymentOption#bca2d5e5`.
type StarGiveawayPaymentOption struct {
	// ISO 4217 currency code for the payment
	Currency string
	// The amount to pay, in the smallest units of the currency
	Amount int64
	// Number of Telegram Stars that will be distributed among winners
	StarCount int64
	// Identifier of the store product associated with the option; may be empty if none
	StoreProductID string
	// Number of times the chat will be boosted for one year if the option is chosen
	YearlyBoostCount int32
	// Allowed options for the number of giveaway winners
	WinnerOptions []StarGiveawayWinnerOption
	// True, if the option must be chosen by default
	IsDefault bool
	// True, if the option must be shown only in the full list of payment options
	IsAdditional bool
}

// StarGiveawayPaymentOptionTypeID is TL type id of StarGiveawayPaymentOption.
const StarGiveawayPaymentOptionTypeID = 0xbca2d5e5

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

func (s *StarGiveawayPaymentOption) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Currency == "") {
		return false
	}
	if !(s.Amount == 0) {
		return false
	}
	if !(s.StarCount == 0) {
		return false
	}
	if !(s.StoreProductID == "") {
		return false
	}
	if !(s.YearlyBoostCount == 0) {
		return false
	}
	if !(s.WinnerOptions == nil) {
		return false
	}
	if !(s.IsDefault == false) {
		return false
	}
	if !(s.IsAdditional == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StarGiveawayPaymentOption) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starGiveawayPaymentOption",
		ID:   StarGiveawayPaymentOptionTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Currency",
			SchemaName: "currency",
		},
		{
			Name:       "Amount",
			SchemaName: "amount",
		},
		{
			Name:       "StarCount",
			SchemaName: "star_count",
		},
		{
			Name:       "StoreProductID",
			SchemaName: "store_product_id",
		},
		{
			Name:       "YearlyBoostCount",
			SchemaName: "yearly_boost_count",
		},
		{
			Name:       "WinnerOptions",
			SchemaName: "winner_options",
		},
		{
			Name:       "IsDefault",
			SchemaName: "is_default",
		},
		{
			Name:       "IsAdditional",
			SchemaName: "is_additional",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StarGiveawayPaymentOption) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starGiveawayPaymentOption#bca2d5e5 as nil")
	}
	b.PutID(StarGiveawayPaymentOptionTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarGiveawayPaymentOption) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starGiveawayPaymentOption#bca2d5e5 as nil")
	}
	b.PutString(s.Currency)
	b.PutInt53(s.Amount)
	b.PutInt53(s.StarCount)
	b.PutString(s.StoreProductID)
	b.PutInt32(s.YearlyBoostCount)
	b.PutInt(len(s.WinnerOptions))
	for idx, v := range s.WinnerOptions {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare starGiveawayPaymentOption#bca2d5e5: field winner_options element with index %d: %w", idx, err)
		}
	}
	b.PutBool(s.IsDefault)
	b.PutBool(s.IsAdditional)
	return nil
}

// Decode implements bin.Decoder.
func (s *StarGiveawayPaymentOption) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starGiveawayPaymentOption#bca2d5e5 to nil")
	}
	if err := b.ConsumeID(StarGiveawayPaymentOptionTypeID); err != nil {
		return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StarGiveawayPaymentOption) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starGiveawayPaymentOption#bca2d5e5 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field currency: %w", err)
		}
		s.Currency = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field amount: %w", err)
		}
		s.Amount = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field star_count: %w", err)
		}
		s.StarCount = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field store_product_id: %w", err)
		}
		s.StoreProductID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field yearly_boost_count: %w", err)
		}
		s.YearlyBoostCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field winner_options: %w", err)
		}

		if headerLen > 0 {
			s.WinnerOptions = make([]StarGiveawayWinnerOption, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarGiveawayWinnerOption
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare starGiveawayPaymentOption#bca2d5e5: field winner_options: %w", err)
			}
			s.WinnerOptions = append(s.WinnerOptions, value)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field is_default: %w", err)
		}
		s.IsDefault = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field is_additional: %w", err)
		}
		s.IsAdditional = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StarGiveawayPaymentOption) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode starGiveawayPaymentOption#bca2d5e5 as nil")
	}
	b.ObjStart()
	b.PutID("starGiveawayPaymentOption")
	b.Comma()
	b.FieldStart("currency")
	b.PutString(s.Currency)
	b.Comma()
	b.FieldStart("amount")
	b.PutInt53(s.Amount)
	b.Comma()
	b.FieldStart("star_count")
	b.PutInt53(s.StarCount)
	b.Comma()
	b.FieldStart("store_product_id")
	b.PutString(s.StoreProductID)
	b.Comma()
	b.FieldStart("yearly_boost_count")
	b.PutInt32(s.YearlyBoostCount)
	b.Comma()
	b.FieldStart("winner_options")
	b.ArrStart()
	for idx, v := range s.WinnerOptions {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode starGiveawayPaymentOption#bca2d5e5: field winner_options element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("is_default")
	b.PutBool(s.IsDefault)
	b.Comma()
	b.FieldStart("is_additional")
	b.PutBool(s.IsAdditional)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StarGiveawayPaymentOption) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode starGiveawayPaymentOption#bca2d5e5 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("starGiveawayPaymentOption"); err != nil {
				return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: %w", err)
			}
		case "currency":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field currency: %w", err)
			}
			s.Currency = value
		case "amount":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field amount: %w", err)
			}
			s.Amount = value
		case "star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field star_count: %w", err)
			}
			s.StarCount = value
		case "store_product_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field store_product_id: %w", err)
			}
			s.StoreProductID = value
		case "yearly_boost_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field yearly_boost_count: %w", err)
			}
			s.YearlyBoostCount = value
		case "winner_options":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value StarGiveawayWinnerOption
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field winner_options: %w", err)
				}
				s.WinnerOptions = append(s.WinnerOptions, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field winner_options: %w", err)
			}
		case "is_default":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field is_default: %w", err)
			}
			s.IsDefault = value
		case "is_additional":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode starGiveawayPaymentOption#bca2d5e5: field is_additional: %w", err)
			}
			s.IsAdditional = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetCurrency returns value of Currency field.
func (s *StarGiveawayPaymentOption) GetCurrency() (value string) {
	if s == nil {
		return
	}
	return s.Currency
}

// GetAmount returns value of Amount field.
func (s *StarGiveawayPaymentOption) GetAmount() (value int64) {
	if s == nil {
		return
	}
	return s.Amount
}

// GetStarCount returns value of StarCount field.
func (s *StarGiveawayPaymentOption) GetStarCount() (value int64) {
	if s == nil {
		return
	}
	return s.StarCount
}

// GetStoreProductID returns value of StoreProductID field.
func (s *StarGiveawayPaymentOption) GetStoreProductID() (value string) {
	if s == nil {
		return
	}
	return s.StoreProductID
}

// GetYearlyBoostCount returns value of YearlyBoostCount field.
func (s *StarGiveawayPaymentOption) GetYearlyBoostCount() (value int32) {
	if s == nil {
		return
	}
	return s.YearlyBoostCount
}

// GetWinnerOptions returns value of WinnerOptions field.
func (s *StarGiveawayPaymentOption) GetWinnerOptions() (value []StarGiveawayWinnerOption) {
	if s == nil {
		return
	}
	return s.WinnerOptions
}

// GetIsDefault returns value of IsDefault field.
func (s *StarGiveawayPaymentOption) GetIsDefault() (value bool) {
	if s == nil {
		return
	}
	return s.IsDefault
}

// GetIsAdditional returns value of IsAdditional field.
func (s *StarGiveawayPaymentOption) GetIsAdditional() (value bool) {
	if s == nil {
		return
	}
	return s.IsAdditional
}
