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

// StarsGiveawayOption represents TL type `starsGiveawayOption#94ce852a`.
// Contains info about a Telegram Star giveaway¹ option.
//
// Links:
//  1. https://core.telegram.org/api/giveaways#star-giveaways
//
// See https://core.telegram.org/constructor/starsGiveawayOption for reference.
type StarsGiveawayOption struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, this option must only be shown in the full list of giveaway options (i.e. they
	// must be added to the list only when the user clicks on the expand button).
	Extended bool
	// If set, this option must be pre-selected by default in the option list.
	Default bool
	// The number of Telegram Stars that will be distributed among winners
	Stars int64
	// Number of times the chat will be boosted for one year if the
	// inputStorePaymentStarsGiveaway¹.boost_peer flag is populated
	//
	// Links:
	//  1) https://core.telegram.org/constructor/inputStorePaymentStarsGiveaway
	YearlyBoosts int
	// Identifier of the store product associated with the option, official apps only.
	//
	// Use SetStoreProduct and GetStoreProduct helpers.
	StoreProduct string
	// Three-letter ISO 4217 currency¹ code
	//
	// Links:
	//  1) https://core.telegram.org/bots/payments#supported-currencies
	Currency string
	// Total price in the smallest units of the currency (integer, not float/double). For
	// example, for a price of US$ 1.45 pass amount = 145. See the exp parameter in
	// currencies.json¹, it shows the number of digits past the decimal point for each
	// currency (2 for the majority of currencies).
	//
	// Links:
	//  1) https://core.telegram.org/bots/payments/currencies.json
	Amount int64
	// Allowed options for the number of giveaway winners.
	Winners []StarsGiveawayWinnersOption
}

// StarsGiveawayOptionTypeID is TL type id of StarsGiveawayOption.
const StarsGiveawayOptionTypeID = 0x94ce852a

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

func (s *StarsGiveawayOption) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Extended == false) {
		return false
	}
	if !(s.Default == false) {
		return false
	}
	if !(s.Stars == 0) {
		return false
	}
	if !(s.YearlyBoosts == 0) {
		return false
	}
	if !(s.StoreProduct == "") {
		return false
	}
	if !(s.Currency == "") {
		return false
	}
	if !(s.Amount == 0) {
		return false
	}
	if !(s.Winners == nil) {
		return false
	}

	return true
}

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

// FillFrom fills StarsGiveawayOption from given interface.
func (s *StarsGiveawayOption) FillFrom(from interface {
	GetExtended() (value bool)
	GetDefault() (value bool)
	GetStars() (value int64)
	GetYearlyBoosts() (value int)
	GetStoreProduct() (value string, ok bool)
	GetCurrency() (value string)
	GetAmount() (value int64)
	GetWinners() (value []StarsGiveawayWinnersOption)
}) {
	s.Extended = from.GetExtended()
	s.Default = from.GetDefault()
	s.Stars = from.GetStars()
	s.YearlyBoosts = from.GetYearlyBoosts()
	if val, ok := from.GetStoreProduct(); ok {
		s.StoreProduct = val
	}

	s.Currency = from.GetCurrency()
	s.Amount = from.GetAmount()
	s.Winners = from.GetWinners()
}

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

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

// TypeInfo returns info about TL type.
func (s *StarsGiveawayOption) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starsGiveawayOption",
		ID:   StarsGiveawayOptionTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Extended",
			SchemaName: "extended",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Default",
			SchemaName: "default",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "Stars",
			SchemaName: "stars",
		},
		{
			Name:       "YearlyBoosts",
			SchemaName: "yearly_boosts",
		},
		{
			Name:       "StoreProduct",
			SchemaName: "store_product",
			Null:       !s.Flags.Has(2),
		},
		{
			Name:       "Currency",
			SchemaName: "currency",
		},
		{
			Name:       "Amount",
			SchemaName: "amount",
		},
		{
			Name:       "Winners",
			SchemaName: "winners",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StarsGiveawayOption) SetFlags() {
	if !(s.Extended == false) {
		s.Flags.Set(0)
	}
	if !(s.Default == false) {
		s.Flags.Set(1)
	}
	if !(s.StoreProduct == "") {
		s.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (s *StarsGiveawayOption) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starsGiveawayOption#94ce852a as nil")
	}
	b.PutID(StarsGiveawayOptionTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarsGiveawayOption) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starsGiveawayOption#94ce852a as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starsGiveawayOption#94ce852a: field flags: %w", err)
	}
	b.PutLong(s.Stars)
	b.PutInt(s.YearlyBoosts)
	if s.Flags.Has(2) {
		b.PutString(s.StoreProduct)
	}
	b.PutString(s.Currency)
	b.PutLong(s.Amount)
	b.PutVectorHeader(len(s.Winners))
	for idx, v := range s.Winners {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode starsGiveawayOption#94ce852a: field winners element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StarsGiveawayOption) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starsGiveawayOption#94ce852a to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starsGiveawayOption#94ce852a: field flags: %w", err)
		}
	}
	s.Extended = s.Flags.Has(0)
	s.Default = s.Flags.Has(1)
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starsGiveawayOption#94ce852a: field stars: %w", err)
		}
		s.Stars = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starsGiveawayOption#94ce852a: field yearly_boosts: %w", err)
		}
		s.YearlyBoosts = value
	}
	if s.Flags.Has(2) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starsGiveawayOption#94ce852a: field store_product: %w", err)
		}
		s.StoreProduct = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starsGiveawayOption#94ce852a: field currency: %w", err)
		}
		s.Currency = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starsGiveawayOption#94ce852a: field amount: %w", err)
		}
		s.Amount = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode starsGiveawayOption#94ce852a: field winners: %w", err)
		}

		if headerLen > 0 {
			s.Winners = make([]StarsGiveawayWinnersOption, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarsGiveawayWinnersOption
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode starsGiveawayOption#94ce852a: field winners: %w", err)
			}
			s.Winners = append(s.Winners, value)
		}
	}
	return nil
}

// SetExtended sets value of Extended conditional field.
func (s *StarsGiveawayOption) SetExtended(value bool) {
	if value {
		s.Flags.Set(0)
		s.Extended = true
	} else {
		s.Flags.Unset(0)
		s.Extended = false
	}
}

// GetExtended returns value of Extended conditional field.
func (s *StarsGiveawayOption) GetExtended() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(0)
}

// SetDefault sets value of Default conditional field.
func (s *StarsGiveawayOption) SetDefault(value bool) {
	if value {
		s.Flags.Set(1)
		s.Default = true
	} else {
		s.Flags.Unset(1)
		s.Default = false
	}
}

// GetDefault returns value of Default conditional field.
func (s *StarsGiveawayOption) GetDefault() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(1)
}

// GetStars returns value of Stars field.
func (s *StarsGiveawayOption) GetStars() (value int64) {
	if s == nil {
		return
	}
	return s.Stars
}

// GetYearlyBoosts returns value of YearlyBoosts field.
func (s *StarsGiveawayOption) GetYearlyBoosts() (value int) {
	if s == nil {
		return
	}
	return s.YearlyBoosts
}

// SetStoreProduct sets value of StoreProduct conditional field.
func (s *StarsGiveawayOption) SetStoreProduct(value string) {
	s.Flags.Set(2)
	s.StoreProduct = value
}

// GetStoreProduct returns value of StoreProduct conditional field and
// boolean which is true if field was set.
func (s *StarsGiveawayOption) GetStoreProduct() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(2) {
		return value, false
	}
	return s.StoreProduct, true
}

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

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

// GetWinners returns value of Winners field.
func (s *StarsGiveawayOption) GetWinners() (value []StarsGiveawayWinnersOption) {
	if s == nil {
		return
	}
	return s.Winners
}
