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

// StarsGiftOption represents TL type `starsGiftOption#5e0589f1`.
// Telegram Stars gift option¹.
//
// Links:
//  1. https://core.telegram.org/api/stars#buying-or-gifting-stars
//
// See https://core.telegram.org/constructor/starsGiftOption for reference.
type StarsGiftOption struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, the option must only be shown in the full list of topup options.
	Extended bool
	// Amount of Telegram stars.
	Stars int64
	// 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
	// Price of the product 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
}

// StarsGiftOptionTypeID is TL type id of StarsGiftOption.
const StarsGiftOptionTypeID = 0x5e0589f1

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

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

	return true
}

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

// FillFrom fills StarsGiftOption from given interface.
func (s *StarsGiftOption) FillFrom(from interface {
	GetExtended() (value bool)
	GetStars() (value int64)
	GetStoreProduct() (value string, ok bool)
	GetCurrency() (value string)
	GetAmount() (value int64)
}) {
	s.Extended = from.GetExtended()
	s.Stars = from.GetStars()
	if val, ok := from.GetStoreProduct(); ok {
		s.StoreProduct = val
	}

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

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

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

// TypeInfo returns info about TL type.
func (s *StarsGiftOption) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starsGiftOption",
		ID:   StarsGiftOptionTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Extended",
			SchemaName: "extended",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "Stars",
			SchemaName: "stars",
		},
		{
			Name:       "StoreProduct",
			SchemaName: "store_product",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Currency",
			SchemaName: "currency",
		},
		{
			Name:       "Amount",
			SchemaName: "amount",
		},
	}
	return typ
}

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

// Encode implements bin.Encoder.
func (s *StarsGiftOption) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starsGiftOption#5e0589f1 as nil")
	}
	b.PutID(StarsGiftOptionTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarsGiftOption) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starsGiftOption#5e0589f1 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starsGiftOption#5e0589f1: field flags: %w", err)
	}
	b.PutLong(s.Stars)
	if s.Flags.Has(0) {
		b.PutString(s.StoreProduct)
	}
	b.PutString(s.Currency)
	b.PutLong(s.Amount)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StarsGiftOption) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starsGiftOption#5e0589f1 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starsGiftOption#5e0589f1: field flags: %w", err)
		}
	}
	s.Extended = s.Flags.Has(1)
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starsGiftOption#5e0589f1: field stars: %w", err)
		}
		s.Stars = value
	}
	if s.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starsGiftOption#5e0589f1: field store_product: %w", err)
		}
		s.StoreProduct = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starsGiftOption#5e0589f1: field currency: %w", err)
		}
		s.Currency = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starsGiftOption#5e0589f1: field amount: %w", err)
		}
		s.Amount = value
	}
	return nil
}

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

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

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

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

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

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

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