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

// PremiumGiftCodeOption represents TL type `premiumGiftCodeOption#257e962b`.
// Contains info about a giveaway/gift¹ option.
//
// Links:
//  1. https://core.telegram.org/api/giveaways
//
// See https://core.telegram.org/constructor/premiumGiftCodeOption for reference.
type PremiumGiftCodeOption struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Number of users which will be able to activate the gift codes.
	Users int
	// Duration in months of each gifted Telegram Premium¹ subscription.
	//
	// Links:
	//  1) https://core.telegram.org/api/premium
	Months int
	// Identifier of the store product associated with the option, official apps only.
	//
	// Use SetStoreProduct and GetStoreProduct helpers.
	StoreProduct string
	// Number of times the store product must be paid
	//
	// Use SetStoreQuantity and GetStoreQuantity helpers.
	StoreQuantity 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
}

// PremiumGiftCodeOptionTypeID is TL type id of PremiumGiftCodeOption.
const PremiumGiftCodeOptionTypeID = 0x257e962b

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

func (p *PremiumGiftCodeOption) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Flags.Zero()) {
		return false
	}
	if !(p.Users == 0) {
		return false
	}
	if !(p.Months == 0) {
		return false
	}
	if !(p.StoreProduct == "") {
		return false
	}
	if !(p.StoreQuantity == 0) {
		return false
	}
	if !(p.Currency == "") {
		return false
	}
	if !(p.Amount == 0) {
		return false
	}

	return true
}

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

// FillFrom fills PremiumGiftCodeOption from given interface.
func (p *PremiumGiftCodeOption) FillFrom(from interface {
	GetUsers() (value int)
	GetMonths() (value int)
	GetStoreProduct() (value string, ok bool)
	GetStoreQuantity() (value int, ok bool)
	GetCurrency() (value string)
	GetAmount() (value int64)
}) {
	p.Users = from.GetUsers()
	p.Months = from.GetMonths()
	if val, ok := from.GetStoreProduct(); ok {
		p.StoreProduct = val
	}

	if val, ok := from.GetStoreQuantity(); ok {
		p.StoreQuantity = val
	}

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

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

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

// TypeInfo returns info about TL type.
func (p *PremiumGiftCodeOption) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "premiumGiftCodeOption",
		ID:   PremiumGiftCodeOptionTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Users",
			SchemaName: "users",
		},
		{
			Name:       "Months",
			SchemaName: "months",
		},
		{
			Name:       "StoreProduct",
			SchemaName: "store_product",
			Null:       !p.Flags.Has(0),
		},
		{
			Name:       "StoreQuantity",
			SchemaName: "store_quantity",
			Null:       !p.Flags.Has(1),
		},
		{
			Name:       "Currency",
			SchemaName: "currency",
		},
		{
			Name:       "Amount",
			SchemaName: "amount",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (p *PremiumGiftCodeOption) SetFlags() {
	if !(p.StoreProduct == "") {
		p.Flags.Set(0)
	}
	if !(p.StoreQuantity == 0) {
		p.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (p *PremiumGiftCodeOption) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumGiftCodeOption#257e962b as nil")
	}
	b.PutID(PremiumGiftCodeOptionTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PremiumGiftCodeOption) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumGiftCodeOption#257e962b as nil")
	}
	p.SetFlags()
	if err := p.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode premiumGiftCodeOption#257e962b: field flags: %w", err)
	}
	b.PutInt(p.Users)
	b.PutInt(p.Months)
	if p.Flags.Has(0) {
		b.PutString(p.StoreProduct)
	}
	if p.Flags.Has(1) {
		b.PutInt(p.StoreQuantity)
	}
	b.PutString(p.Currency)
	b.PutLong(p.Amount)
	return nil
}

// Decode implements bin.Decoder.
func (p *PremiumGiftCodeOption) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumGiftCodeOption#257e962b to nil")
	}
	if err := b.ConsumeID(PremiumGiftCodeOptionTypeID); err != nil {
		return fmt.Errorf("unable to decode premiumGiftCodeOption#257e962b: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PremiumGiftCodeOption) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumGiftCodeOption#257e962b to nil")
	}
	{
		if err := p.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode premiumGiftCodeOption#257e962b: field flags: %w", err)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftCodeOption#257e962b: field users: %w", err)
		}
		p.Users = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftCodeOption#257e962b: field months: %w", err)
		}
		p.Months = value
	}
	if p.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftCodeOption#257e962b: field store_product: %w", err)
		}
		p.StoreProduct = value
	}
	if p.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftCodeOption#257e962b: field store_quantity: %w", err)
		}
		p.StoreQuantity = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftCodeOption#257e962b: field currency: %w", err)
		}
		p.Currency = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftCodeOption#257e962b: field amount: %w", err)
		}
		p.Amount = value
	}
	return nil
}

// GetUsers returns value of Users field.
func (p *PremiumGiftCodeOption) GetUsers() (value int) {
	if p == nil {
		return
	}
	return p.Users
}

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

// SetStoreProduct sets value of StoreProduct conditional field.
func (p *PremiumGiftCodeOption) 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 *PremiumGiftCodeOption) GetStoreProduct() (value string, ok bool) {
	if p == nil {
		return
	}
	if !p.Flags.Has(0) {
		return value, false
	}
	return p.StoreProduct, true
}

// SetStoreQuantity sets value of StoreQuantity conditional field.
func (p *PremiumGiftCodeOption) SetStoreQuantity(value int) {
	p.Flags.Set(1)
	p.StoreQuantity = value
}

// GetStoreQuantity returns value of StoreQuantity conditional field and
// boolean which is true if field was set.
func (p *PremiumGiftCodeOption) GetStoreQuantity() (value int, ok bool) {
	if p == nil {
		return
	}
	if !p.Flags.Has(1) {
		return value, false
	}
	return p.StoreQuantity, true
}

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

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