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

// PremiumStatePaymentOption represents TL type `premiumStatePaymentOption#7d06b579`.
type PremiumStatePaymentOption struct {
	// Information about the payment option
	PaymentOption PremiumPaymentOption
	// True, if this is the currently used Telegram Premium subscription option
	IsCurrent bool
	// True, if the payment option can be used to upgrade the existing Telegram Premium
	// subscription
	IsUpgrade bool
	// Identifier of the last in-store transaction for the currently used option
	LastTransactionID string
}

// PremiumStatePaymentOptionTypeID is TL type id of PremiumStatePaymentOption.
const PremiumStatePaymentOptionTypeID = 0x7d06b579

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

func (p *PremiumStatePaymentOption) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.PaymentOption.Zero()) {
		return false
	}
	if !(p.IsCurrent == false) {
		return false
	}
	if !(p.IsUpgrade == false) {
		return false
	}
	if !(p.LastTransactionID == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PremiumStatePaymentOption) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "premiumStatePaymentOption",
		ID:   PremiumStatePaymentOptionTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "PaymentOption",
			SchemaName: "payment_option",
		},
		{
			Name:       "IsCurrent",
			SchemaName: "is_current",
		},
		{
			Name:       "IsUpgrade",
			SchemaName: "is_upgrade",
		},
		{
			Name:       "LastTransactionID",
			SchemaName: "last_transaction_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PremiumStatePaymentOption) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumStatePaymentOption#7d06b579 as nil")
	}
	b.PutID(PremiumStatePaymentOptionTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PremiumStatePaymentOption) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumStatePaymentOption#7d06b579 as nil")
	}
	if err := p.PaymentOption.Encode(b); err != nil {
		return fmt.Errorf("unable to encode premiumStatePaymentOption#7d06b579: field payment_option: %w", err)
	}
	b.PutBool(p.IsCurrent)
	b.PutBool(p.IsUpgrade)
	b.PutString(p.LastTransactionID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *PremiumStatePaymentOption) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumStatePaymentOption#7d06b579 to nil")
	}
	{
		if err := p.PaymentOption.Decode(b); err != nil {
			return fmt.Errorf("unable to decode premiumStatePaymentOption#7d06b579: field payment_option: %w", err)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode premiumStatePaymentOption#7d06b579: field is_current: %w", err)
		}
		p.IsCurrent = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode premiumStatePaymentOption#7d06b579: field is_upgrade: %w", err)
		}
		p.IsUpgrade = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode premiumStatePaymentOption#7d06b579: field last_transaction_id: %w", err)
		}
		p.LastTransactionID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PremiumStatePaymentOption) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumStatePaymentOption#7d06b579 as nil")
	}
	b.ObjStart()
	b.PutID("premiumStatePaymentOption")
	b.Comma()
	b.FieldStart("payment_option")
	if err := p.PaymentOption.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode premiumStatePaymentOption#7d06b579: field payment_option: %w", err)
	}
	b.Comma()
	b.FieldStart("is_current")
	b.PutBool(p.IsCurrent)
	b.Comma()
	b.FieldStart("is_upgrade")
	b.PutBool(p.IsUpgrade)
	b.Comma()
	b.FieldStart("last_transaction_id")
	b.PutString(p.LastTransactionID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("premiumStatePaymentOption"); err != nil {
				return fmt.Errorf("unable to decode premiumStatePaymentOption#7d06b579: %w", err)
			}
		case "payment_option":
			if err := p.PaymentOption.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode premiumStatePaymentOption#7d06b579: field payment_option: %w", err)
			}
		case "is_current":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode premiumStatePaymentOption#7d06b579: field is_current: %w", err)
			}
			p.IsCurrent = value
		case "is_upgrade":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode premiumStatePaymentOption#7d06b579: field is_upgrade: %w", err)
			}
			p.IsUpgrade = value
		case "last_transaction_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode premiumStatePaymentOption#7d06b579: field last_transaction_id: %w", err)
			}
			p.LastTransactionID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPaymentOption returns value of PaymentOption field.
func (p *PremiumStatePaymentOption) GetPaymentOption() (value PremiumPaymentOption) {
	if p == nil {
		return
	}
	return p.PaymentOption
}

// GetIsCurrent returns value of IsCurrent field.
func (p *PremiumStatePaymentOption) GetIsCurrent() (value bool) {
	if p == nil {
		return
	}
	return p.IsCurrent
}

// GetIsUpgrade returns value of IsUpgrade field.
func (p *PremiumStatePaymentOption) GetIsUpgrade() (value bool) {
	if p == nil {
		return
	}
	return p.IsUpgrade
}

// GetLastTransactionID returns value of LastTransactionID field.
func (p *PremiumStatePaymentOption) GetLastTransactionID() (value string) {
	if p == nil {
		return
	}
	return p.LastTransactionID
}
