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

// PremiumSubscriptionOption represents TL type `premiumSubscriptionOption#5f2d1df2`.
// Describes a Telegram Premium subscription option
//
// See https://core.telegram.org/constructor/premiumSubscriptionOption for reference.
type PremiumSubscriptionOption struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether this subscription option is currently in use.
	Current bool
	// Whether this subscription option can be used to upgrade the existing Telegram Premium
	// subscription. When upgrading Telegram Premium subscriptions bought through stores,
	// make sure that the store transaction ID is equal to transaction, to avoid upgrading
	// someone else's account, if the client is currently logged into multiple accounts.
	CanPurchaseUpgrade bool
	// Identifier of the last in-store transaction for the currently used subscription on the
	// current account.
	//
	// Use SetTransaction and GetTransaction helpers.
	Transaction string
	// Duration of subscription in months
	Months int
	// 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
	// Deep link¹ used to initiate payment
	//
	// Links:
	//  1) https://core.telegram.org/api/links
	BotURL string
	// Store product ID, only for official apps
	//
	// Use SetStoreProduct and GetStoreProduct helpers.
	StoreProduct string
}

// PremiumSubscriptionOptionTypeID is TL type id of PremiumSubscriptionOption.
const PremiumSubscriptionOptionTypeID = 0x5f2d1df2

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

func (p *PremiumSubscriptionOption) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Flags.Zero()) {
		return false
	}
	if !(p.Current == false) {
		return false
	}
	if !(p.CanPurchaseUpgrade == false) {
		return false
	}
	if !(p.Transaction == "") {
		return false
	}
	if !(p.Months == 0) {
		return false
	}
	if !(p.Currency == "") {
		return false
	}
	if !(p.Amount == 0) {
		return false
	}
	if !(p.BotURL == "") {
		return false
	}
	if !(p.StoreProduct == "") {
		return false
	}

	return true
}

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

// FillFrom fills PremiumSubscriptionOption from given interface.
func (p *PremiumSubscriptionOption) FillFrom(from interface {
	GetCurrent() (value bool)
	GetCanPurchaseUpgrade() (value bool)
	GetTransaction() (value string, ok bool)
	GetMonths() (value int)
	GetCurrency() (value string)
	GetAmount() (value int64)
	GetBotURL() (value string)
	GetStoreProduct() (value string, ok bool)
}) {
	p.Current = from.GetCurrent()
	p.CanPurchaseUpgrade = from.GetCanPurchaseUpgrade()
	if val, ok := from.GetTransaction(); ok {
		p.Transaction = val
	}

	p.Months = from.GetMonths()
	p.Currency = from.GetCurrency()
	p.Amount = from.GetAmount()
	p.BotURL = from.GetBotURL()
	if val, ok := from.GetStoreProduct(); ok {
		p.StoreProduct = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (p *PremiumSubscriptionOption) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "premiumSubscriptionOption",
		ID:   PremiumSubscriptionOptionTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Current",
			SchemaName: "current",
			Null:       !p.Flags.Has(1),
		},
		{
			Name:       "CanPurchaseUpgrade",
			SchemaName: "can_purchase_upgrade",
			Null:       !p.Flags.Has(2),
		},
		{
			Name:       "Transaction",
			SchemaName: "transaction",
			Null:       !p.Flags.Has(3),
		},
		{
			Name:       "Months",
			SchemaName: "months",
		},
		{
			Name:       "Currency",
			SchemaName: "currency",
		},
		{
			Name:       "Amount",
			SchemaName: "amount",
		},
		{
			Name:       "BotURL",
			SchemaName: "bot_url",
		},
		{
			Name:       "StoreProduct",
			SchemaName: "store_product",
			Null:       !p.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (p *PremiumSubscriptionOption) SetFlags() {
	if !(p.Current == false) {
		p.Flags.Set(1)
	}
	if !(p.CanPurchaseUpgrade == false) {
		p.Flags.Set(2)
	}
	if !(p.Transaction == "") {
		p.Flags.Set(3)
	}
	if !(p.StoreProduct == "") {
		p.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (p *PremiumSubscriptionOption) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumSubscriptionOption#5f2d1df2 as nil")
	}
	b.PutID(PremiumSubscriptionOptionTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PremiumSubscriptionOption) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumSubscriptionOption#5f2d1df2 as nil")
	}
	p.SetFlags()
	if err := p.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode premiumSubscriptionOption#5f2d1df2: field flags: %w", err)
	}
	if p.Flags.Has(3) {
		b.PutString(p.Transaction)
	}
	b.PutInt(p.Months)
	b.PutString(p.Currency)
	b.PutLong(p.Amount)
	b.PutString(p.BotURL)
	if p.Flags.Has(0) {
		b.PutString(p.StoreProduct)
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PremiumSubscriptionOption) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumSubscriptionOption#5f2d1df2 to nil")
	}
	if err := b.ConsumeID(PremiumSubscriptionOptionTypeID); err != nil {
		return fmt.Errorf("unable to decode premiumSubscriptionOption#5f2d1df2: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PremiumSubscriptionOption) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumSubscriptionOption#5f2d1df2 to nil")
	}
	{
		if err := p.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode premiumSubscriptionOption#5f2d1df2: field flags: %w", err)
		}
	}
	p.Current = p.Flags.Has(1)
	p.CanPurchaseUpgrade = p.Flags.Has(2)
	if p.Flags.Has(3) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode premiumSubscriptionOption#5f2d1df2: field transaction: %w", err)
		}
		p.Transaction = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode premiumSubscriptionOption#5f2d1df2: field months: %w", err)
		}
		p.Months = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode premiumSubscriptionOption#5f2d1df2: field currency: %w", err)
		}
		p.Currency = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode premiumSubscriptionOption#5f2d1df2: field amount: %w", err)
		}
		p.Amount = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode premiumSubscriptionOption#5f2d1df2: field bot_url: %w", err)
		}
		p.BotURL = value
	}
	if p.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode premiumSubscriptionOption#5f2d1df2: field store_product: %w", err)
		}
		p.StoreProduct = value
	}
	return nil
}

// SetCurrent sets value of Current conditional field.
func (p *PremiumSubscriptionOption) SetCurrent(value bool) {
	if value {
		p.Flags.Set(1)
		p.Current = true
	} else {
		p.Flags.Unset(1)
		p.Current = false
	}
}

// GetCurrent returns value of Current conditional field.
func (p *PremiumSubscriptionOption) GetCurrent() (value bool) {
	if p == nil {
		return
	}
	return p.Flags.Has(1)
}

// SetCanPurchaseUpgrade sets value of CanPurchaseUpgrade conditional field.
func (p *PremiumSubscriptionOption) SetCanPurchaseUpgrade(value bool) {
	if value {
		p.Flags.Set(2)
		p.CanPurchaseUpgrade = true
	} else {
		p.Flags.Unset(2)
		p.CanPurchaseUpgrade = false
	}
}

// GetCanPurchaseUpgrade returns value of CanPurchaseUpgrade conditional field.
func (p *PremiumSubscriptionOption) GetCanPurchaseUpgrade() (value bool) {
	if p == nil {
		return
	}
	return p.Flags.Has(2)
}

// SetTransaction sets value of Transaction conditional field.
func (p *PremiumSubscriptionOption) SetTransaction(value string) {
	p.Flags.Set(3)
	p.Transaction = value
}

// GetTransaction returns value of Transaction conditional field and
// boolean which is true if field was set.
func (p *PremiumSubscriptionOption) GetTransaction() (value string, ok bool) {
	if p == nil {
		return
	}
	if !p.Flags.Has(3) {
		return value, false
	}
	return p.Transaction, true
}

// GetMonths returns value of Months field.
func (p *PremiumSubscriptionOption) GetMonths() (value int) {
	if p == nil {
		return
	}
	return p.Months
}

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

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

// GetBotURL returns value of BotURL field.
func (p *PremiumSubscriptionOption) GetBotURL() (value string) {
	if p == nil {
		return
	}
	return p.BotURL
}

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

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