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

// PremiumGiftPaymentOption represents TL type `premiumGiftPaymentOption#ebd93b5d`.
type PremiumGiftPaymentOption struct {
	// ISO 4217 currency code for the payment
	Currency string
	// The amount to pay, in the smallest units of the currency
	Amount int64
	// The alternative amount of Telegram Stars to pay; 0 if payment in Telegram Stars is not
	// possible
	StarCount int64
	// The discount associated with this option, as a percentage
	DiscountPercentage int32
	// Number of months the Telegram Premium subscription will be active
	MonthCount int32
	// Identifier of the store product associated with the option
	StoreProductID string
	// A sticker to be shown along with the option; may be null if unknown
	Sticker Sticker
}

// PremiumGiftPaymentOptionTypeID is TL type id of PremiumGiftPaymentOption.
const PremiumGiftPaymentOptionTypeID = 0xebd93b5d

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

func (p *PremiumGiftPaymentOption) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Currency == "") {
		return false
	}
	if !(p.Amount == 0) {
		return false
	}
	if !(p.StarCount == 0) {
		return false
	}
	if !(p.DiscountPercentage == 0) {
		return false
	}
	if !(p.MonthCount == 0) {
		return false
	}
	if !(p.StoreProductID == "") {
		return false
	}
	if !(p.Sticker.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PremiumGiftPaymentOption) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "premiumGiftPaymentOption",
		ID:   PremiumGiftPaymentOptionTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Currency",
			SchemaName: "currency",
		},
		{
			Name:       "Amount",
			SchemaName: "amount",
		},
		{
			Name:       "StarCount",
			SchemaName: "star_count",
		},
		{
			Name:       "DiscountPercentage",
			SchemaName: "discount_percentage",
		},
		{
			Name:       "MonthCount",
			SchemaName: "month_count",
		},
		{
			Name:       "StoreProductID",
			SchemaName: "store_product_id",
		},
		{
			Name:       "Sticker",
			SchemaName: "sticker",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PremiumGiftPaymentOption) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumGiftPaymentOption#ebd93b5d as nil")
	}
	b.PutID(PremiumGiftPaymentOptionTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PremiumGiftPaymentOption) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumGiftPaymentOption#ebd93b5d as nil")
	}
	b.PutString(p.Currency)
	b.PutInt53(p.Amount)
	b.PutInt53(p.StarCount)
	b.PutInt32(p.DiscountPercentage)
	b.PutInt32(p.MonthCount)
	b.PutString(p.StoreProductID)
	if err := p.Sticker.Encode(b); err != nil {
		return fmt.Errorf("unable to encode premiumGiftPaymentOption#ebd93b5d: field sticker: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *PremiumGiftPaymentOption) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumGiftPaymentOption#ebd93b5d to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field currency: %w", err)
		}
		p.Currency = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field amount: %w", err)
		}
		p.Amount = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field star_count: %w", err)
		}
		p.StarCount = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field discount_percentage: %w", err)
		}
		p.DiscountPercentage = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field month_count: %w", err)
		}
		p.MonthCount = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field store_product_id: %w", err)
		}
		p.StoreProductID = value
	}
	{
		if err := p.Sticker.Decode(b); err != nil {
			return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field sticker: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PremiumGiftPaymentOption) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumGiftPaymentOption#ebd93b5d as nil")
	}
	b.ObjStart()
	b.PutID("premiumGiftPaymentOption")
	b.Comma()
	b.FieldStart("currency")
	b.PutString(p.Currency)
	b.Comma()
	b.FieldStart("amount")
	b.PutInt53(p.Amount)
	b.Comma()
	b.FieldStart("star_count")
	b.PutInt53(p.StarCount)
	b.Comma()
	b.FieldStart("discount_percentage")
	b.PutInt32(p.DiscountPercentage)
	b.Comma()
	b.FieldStart("month_count")
	b.PutInt32(p.MonthCount)
	b.Comma()
	b.FieldStart("store_product_id")
	b.PutString(p.StoreProductID)
	b.Comma()
	b.FieldStart("sticker")
	if err := p.Sticker.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode premiumGiftPaymentOption#ebd93b5d: field sticker: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PremiumGiftPaymentOption) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumGiftPaymentOption#ebd93b5d to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("premiumGiftPaymentOption"); err != nil {
				return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: %w", err)
			}
		case "currency":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field currency: %w", err)
			}
			p.Currency = value
		case "amount":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field amount: %w", err)
			}
			p.Amount = value
		case "star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field star_count: %w", err)
			}
			p.StarCount = value
		case "discount_percentage":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field discount_percentage: %w", err)
			}
			p.DiscountPercentage = value
		case "month_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field month_count: %w", err)
			}
			p.MonthCount = value
		case "store_product_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field store_product_id: %w", err)
			}
			p.StoreProductID = value
		case "sticker":
			if err := p.Sticker.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode premiumGiftPaymentOption#ebd93b5d: field sticker: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

// GetDiscountPercentage returns value of DiscountPercentage field.
func (p *PremiumGiftPaymentOption) GetDiscountPercentage() (value int32) {
	if p == nil {
		return
	}
	return p.DiscountPercentage
}

// GetMonthCount returns value of MonthCount field.
func (p *PremiumGiftPaymentOption) GetMonthCount() (value int32) {
	if p == nil {
		return
	}
	return p.MonthCount
}

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

// GetSticker returns value of Sticker field.
func (p *PremiumGiftPaymentOption) GetSticker() (value Sticker) {
	if p == nil {
		return
	}
	return p.Sticker
}
