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

// PremiumPaymentOption represents TL type `premiumPaymentOption#8c0c5fb2`.
type PremiumPaymentOption struct {
	// ISO 4217 currency code for Telegram Premium subscription payment
	Currency string
	// The amount to pay, in the smallest units of the currency
	Amount int64
	// The discount associated with this option, as a percentage
	DiscountPercentage int32
	// Number of months the Telegram Premium subscription will be active. Use
	// getPremiumInfoSticker to get the sticker to be used as representation of the Telegram
	// Premium subscription
	MonthCount int32
	// Identifier of the store product associated with the option
	StoreProductID string
	// An internal link to be opened for buying Telegram Premium to the user if store payment
	// isn't possible; may be null if direct payment isn't available
	PaymentLink InternalLinkTypeClass
}

// PremiumPaymentOptionTypeID is TL type id of PremiumPaymentOption.
const PremiumPaymentOptionTypeID = 0x8c0c5fb2

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PremiumPaymentOption) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "premiumPaymentOption",
		ID:   PremiumPaymentOptionTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Currency",
			SchemaName: "currency",
		},
		{
			Name:       "Amount",
			SchemaName: "amount",
		},
		{
			Name:       "DiscountPercentage",
			SchemaName: "discount_percentage",
		},
		{
			Name:       "MonthCount",
			SchemaName: "month_count",
		},
		{
			Name:       "StoreProductID",
			SchemaName: "store_product_id",
		},
		{
			Name:       "PaymentLink",
			SchemaName: "payment_link",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PremiumPaymentOption) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumPaymentOption#8c0c5fb2 as nil")
	}
	b.PutID(PremiumPaymentOptionTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PremiumPaymentOption) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumPaymentOption#8c0c5fb2 as nil")
	}
	b.PutString(p.Currency)
	b.PutInt53(p.Amount)
	b.PutInt32(p.DiscountPercentage)
	b.PutInt32(p.MonthCount)
	b.PutString(p.StoreProductID)
	if p.PaymentLink == nil {
		return fmt.Errorf("unable to encode premiumPaymentOption#8c0c5fb2: field payment_link is nil")
	}
	if err := p.PaymentLink.Encode(b); err != nil {
		return fmt.Errorf("unable to encode premiumPaymentOption#8c0c5fb2: field payment_link: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *PremiumPaymentOption) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumPaymentOption#8c0c5fb2 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: field currency: %w", err)
		}
		p.Currency = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: field amount: %w", err)
		}
		p.Amount = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: field discount_percentage: %w", err)
		}
		p.DiscountPercentage = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: field month_count: %w", err)
		}
		p.MonthCount = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: field store_product_id: %w", err)
		}
		p.StoreProductID = value
	}
	{
		value, err := DecodeInternalLinkType(b)
		if err != nil {
			return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: field payment_link: %w", err)
		}
		p.PaymentLink = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PremiumPaymentOption) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumPaymentOption#8c0c5fb2 as nil")
	}
	b.ObjStart()
	b.PutID("premiumPaymentOption")
	b.Comma()
	b.FieldStart("currency")
	b.PutString(p.Currency)
	b.Comma()
	b.FieldStart("amount")
	b.PutInt53(p.Amount)
	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("payment_link")
	if p.PaymentLink == nil {
		return fmt.Errorf("unable to encode premiumPaymentOption#8c0c5fb2: field payment_link is nil")
	}
	if err := p.PaymentLink.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode premiumPaymentOption#8c0c5fb2: field payment_link: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("premiumPaymentOption"); err != nil {
				return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: %w", err)
			}
		case "currency":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: field currency: %w", err)
			}
			p.Currency = value
		case "amount":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: field amount: %w", err)
			}
			p.Amount = value
		case "discount_percentage":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: field discount_percentage: %w", err)
			}
			p.DiscountPercentage = value
		case "month_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: 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 premiumPaymentOption#8c0c5fb2: field store_product_id: %w", err)
			}
			p.StoreProductID = value
		case "payment_link":
			value, err := DecodeTDLibJSONInternalLinkType(b)
			if err != nil {
				return fmt.Errorf("unable to decode premiumPaymentOption#8c0c5fb2: field payment_link: %w", err)
			}
			p.PaymentLink = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

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

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

// GetPaymentLink returns value of PaymentLink field.
func (p *PremiumPaymentOption) GetPaymentLink() (value InternalLinkTypeClass) {
	if p == nil {
		return
	}
	return p.PaymentLink
}
