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

// PaymentReceiptTypeRegular represents TL type `paymentReceiptTypeRegular#9e7715b6`.
type PaymentReceiptTypeRegular struct {
	// User identifier of the payment provider bot
	PaymentProviderUserID int64
	// Information about the invoice
	Invoice Invoice
	// Order information; may be null
	OrderInfo OrderInfo
	// Chosen shipping option; may be null
	ShippingOption ShippingOption
	// Title of the saved credentials chosen by the buyer
	CredentialsTitle string
	// The amount of tip chosen by the buyer in the smallest units of the currency
	TipAmount int64
}

// PaymentReceiptTypeRegularTypeID is TL type id of PaymentReceiptTypeRegular.
const PaymentReceiptTypeRegularTypeID = 0x9e7715b6

// construct implements constructor of PaymentReceiptTypeClass.
func (p PaymentReceiptTypeRegular) construct() PaymentReceiptTypeClass { return &p }

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

	_ PaymentReceiptTypeClass = &PaymentReceiptTypeRegular{}
)

func (p *PaymentReceiptTypeRegular) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.PaymentProviderUserID == 0) {
		return false
	}
	if !(p.Invoice.Zero()) {
		return false
	}
	if !(p.OrderInfo.Zero()) {
		return false
	}
	if !(p.ShippingOption.Zero()) {
		return false
	}
	if !(p.CredentialsTitle == "") {
		return false
	}
	if !(p.TipAmount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PaymentReceiptTypeRegular) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "paymentReceiptTypeRegular",
		ID:   PaymentReceiptTypeRegularTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "PaymentProviderUserID",
			SchemaName: "payment_provider_user_id",
		},
		{
			Name:       "Invoice",
			SchemaName: "invoice",
		},
		{
			Name:       "OrderInfo",
			SchemaName: "order_info",
		},
		{
			Name:       "ShippingOption",
			SchemaName: "shipping_option",
		},
		{
			Name:       "CredentialsTitle",
			SchemaName: "credentials_title",
		},
		{
			Name:       "TipAmount",
			SchemaName: "tip_amount",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PaymentReceiptTypeRegular) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentReceiptTypeRegular#9e7715b6 as nil")
	}
	b.PutID(PaymentReceiptTypeRegularTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PaymentReceiptTypeRegular) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentReceiptTypeRegular#9e7715b6 as nil")
	}
	b.PutInt53(p.PaymentProviderUserID)
	if err := p.Invoice.Encode(b); err != nil {
		return fmt.Errorf("unable to encode paymentReceiptTypeRegular#9e7715b6: field invoice: %w", err)
	}
	if err := p.OrderInfo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode paymentReceiptTypeRegular#9e7715b6: field order_info: %w", err)
	}
	if err := p.ShippingOption.Encode(b); err != nil {
		return fmt.Errorf("unable to encode paymentReceiptTypeRegular#9e7715b6: field shipping_option: %w", err)
	}
	b.PutString(p.CredentialsTitle)
	b.PutInt53(p.TipAmount)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *PaymentReceiptTypeRegular) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode paymentReceiptTypeRegular#9e7715b6 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field payment_provider_user_id: %w", err)
		}
		p.PaymentProviderUserID = value
	}
	{
		if err := p.Invoice.Decode(b); err != nil {
			return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field invoice: %w", err)
		}
	}
	{
		if err := p.OrderInfo.Decode(b); err != nil {
			return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field order_info: %w", err)
		}
	}
	{
		if err := p.ShippingOption.Decode(b); err != nil {
			return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field shipping_option: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field credentials_title: %w", err)
		}
		p.CredentialsTitle = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field tip_amount: %w", err)
		}
		p.TipAmount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PaymentReceiptTypeRegular) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentReceiptTypeRegular#9e7715b6 as nil")
	}
	b.ObjStart()
	b.PutID("paymentReceiptTypeRegular")
	b.Comma()
	b.FieldStart("payment_provider_user_id")
	b.PutInt53(p.PaymentProviderUserID)
	b.Comma()
	b.FieldStart("invoice")
	if err := p.Invoice.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode paymentReceiptTypeRegular#9e7715b6: field invoice: %w", err)
	}
	b.Comma()
	b.FieldStart("order_info")
	if err := p.OrderInfo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode paymentReceiptTypeRegular#9e7715b6: field order_info: %w", err)
	}
	b.Comma()
	b.FieldStart("shipping_option")
	if err := p.ShippingOption.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode paymentReceiptTypeRegular#9e7715b6: field shipping_option: %w", err)
	}
	b.Comma()
	b.FieldStart("credentials_title")
	b.PutString(p.CredentialsTitle)
	b.Comma()
	b.FieldStart("tip_amount")
	b.PutInt53(p.TipAmount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("paymentReceiptTypeRegular"); err != nil {
				return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: %w", err)
			}
		case "payment_provider_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field payment_provider_user_id: %w", err)
			}
			p.PaymentProviderUserID = value
		case "invoice":
			if err := p.Invoice.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field invoice: %w", err)
			}
		case "order_info":
			if err := p.OrderInfo.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field order_info: %w", err)
			}
		case "shipping_option":
			if err := p.ShippingOption.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field shipping_option: %w", err)
			}
		case "credentials_title":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field credentials_title: %w", err)
			}
			p.CredentialsTitle = value
		case "tip_amount":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode paymentReceiptTypeRegular#9e7715b6: field tip_amount: %w", err)
			}
			p.TipAmount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPaymentProviderUserID returns value of PaymentProviderUserID field.
func (p *PaymentReceiptTypeRegular) GetPaymentProviderUserID() (value int64) {
	if p == nil {
		return
	}
	return p.PaymentProviderUserID
}

// GetInvoice returns value of Invoice field.
func (p *PaymentReceiptTypeRegular) GetInvoice() (value Invoice) {
	if p == nil {
		return
	}
	return p.Invoice
}

// GetOrderInfo returns value of OrderInfo field.
func (p *PaymentReceiptTypeRegular) GetOrderInfo() (value OrderInfo) {
	if p == nil {
		return
	}
	return p.OrderInfo
}

// GetShippingOption returns value of ShippingOption field.
func (p *PaymentReceiptTypeRegular) GetShippingOption() (value ShippingOption) {
	if p == nil {
		return
	}
	return p.ShippingOption
}

// GetCredentialsTitle returns value of CredentialsTitle field.
func (p *PaymentReceiptTypeRegular) GetCredentialsTitle() (value string) {
	if p == nil {
		return
	}
	return p.CredentialsTitle
}

// GetTipAmount returns value of TipAmount field.
func (p *PaymentReceiptTypeRegular) GetTipAmount() (value int64) {
	if p == nil {
		return
	}
	return p.TipAmount
}

// PaymentReceiptTypeStars represents TL type `paymentReceiptTypeStars#1194074c`.
type PaymentReceiptTypeStars struct {
	// Number of Telegram Stars that were paid
	StarCount int64
	// Unique identifier of the transaction that can be used to dispute it
	TransactionID string
}

// PaymentReceiptTypeStarsTypeID is TL type id of PaymentReceiptTypeStars.
const PaymentReceiptTypeStarsTypeID = 0x1194074c

// construct implements constructor of PaymentReceiptTypeClass.
func (p PaymentReceiptTypeStars) construct() PaymentReceiptTypeClass { return &p }

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

	_ PaymentReceiptTypeClass = &PaymentReceiptTypeStars{}
)

func (p *PaymentReceiptTypeStars) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.StarCount == 0) {
		return false
	}
	if !(p.TransactionID == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PaymentReceiptTypeStars) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "paymentReceiptTypeStars",
		ID:   PaymentReceiptTypeStarsTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StarCount",
			SchemaName: "star_count",
		},
		{
			Name:       "TransactionID",
			SchemaName: "transaction_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PaymentReceiptTypeStars) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentReceiptTypeStars#1194074c as nil")
	}
	b.PutID(PaymentReceiptTypeStarsTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PaymentReceiptTypeStars) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentReceiptTypeStars#1194074c as nil")
	}
	b.PutInt53(p.StarCount)
	b.PutString(p.TransactionID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *PaymentReceiptTypeStars) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode paymentReceiptTypeStars#1194074c to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode paymentReceiptTypeStars#1194074c: field star_count: %w", err)
		}
		p.StarCount = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode paymentReceiptTypeStars#1194074c: field transaction_id: %w", err)
		}
		p.TransactionID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PaymentReceiptTypeStars) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentReceiptTypeStars#1194074c as nil")
	}
	b.ObjStart()
	b.PutID("paymentReceiptTypeStars")
	b.Comma()
	b.FieldStart("star_count")
	b.PutInt53(p.StarCount)
	b.Comma()
	b.FieldStart("transaction_id")
	b.PutString(p.TransactionID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("paymentReceiptTypeStars"); err != nil {
				return fmt.Errorf("unable to decode paymentReceiptTypeStars#1194074c: %w", err)
			}
		case "star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode paymentReceiptTypeStars#1194074c: field star_count: %w", err)
			}
			p.StarCount = value
		case "transaction_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode paymentReceiptTypeStars#1194074c: field transaction_id: %w", err)
			}
			p.TransactionID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetTransactionID returns value of TransactionID field.
func (p *PaymentReceiptTypeStars) GetTransactionID() (value string) {
	if p == nil {
		return
	}
	return p.TransactionID
}

// PaymentReceiptTypeClassName is schema name of PaymentReceiptTypeClass.
const PaymentReceiptTypeClassName = "PaymentReceiptType"

// PaymentReceiptTypeClass represents PaymentReceiptType generic type.
//
// Example:
//
//	g, err := tdapi.DecodePaymentReceiptType(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.PaymentReceiptTypeRegular: // paymentReceiptTypeRegular#9e7715b6
//	case *tdapi.PaymentReceiptTypeStars: // paymentReceiptTypeStars#1194074c
//	default: panic(v)
//	}
type PaymentReceiptTypeClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() PaymentReceiptTypeClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodePaymentReceiptType implements binary de-serialization for PaymentReceiptTypeClass.
func DecodePaymentReceiptType(buf *bin.Buffer) (PaymentReceiptTypeClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case PaymentReceiptTypeRegularTypeID:
		// Decoding paymentReceiptTypeRegular#9e7715b6.
		v := PaymentReceiptTypeRegular{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PaymentReceiptTypeClass: %w", err)
		}
		return &v, nil
	case PaymentReceiptTypeStarsTypeID:
		// Decoding paymentReceiptTypeStars#1194074c.
		v := PaymentReceiptTypeStars{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PaymentReceiptTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode PaymentReceiptTypeClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONPaymentReceiptType implements binary de-serialization for PaymentReceiptTypeClass.
func DecodeTDLibJSONPaymentReceiptType(buf tdjson.Decoder) (PaymentReceiptTypeClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "paymentReceiptTypeRegular":
		// Decoding paymentReceiptTypeRegular#9e7715b6.
		v := PaymentReceiptTypeRegular{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PaymentReceiptTypeClass: %w", err)
		}
		return &v, nil
	case "paymentReceiptTypeStars":
		// Decoding paymentReceiptTypeStars#1194074c.
		v := PaymentReceiptTypeStars{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PaymentReceiptTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode PaymentReceiptTypeClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// PaymentReceiptType boxes the PaymentReceiptTypeClass providing a helper.
type PaymentReceiptTypeBox struct {
	PaymentReceiptType PaymentReceiptTypeClass
}

// Decode implements bin.Decoder for PaymentReceiptTypeBox.
func (b *PaymentReceiptTypeBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode PaymentReceiptTypeBox to nil")
	}
	v, err := DecodePaymentReceiptType(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.PaymentReceiptType = v
	return nil
}

// Encode implements bin.Encode for PaymentReceiptTypeBox.
func (b *PaymentReceiptTypeBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.PaymentReceiptType == nil {
		return fmt.Errorf("unable to encode PaymentReceiptTypeClass as nil")
	}
	return b.PaymentReceiptType.Encode(buf)
}

// DecodeTDLibJSON implements bin.Decoder for PaymentReceiptTypeBox.
func (b *PaymentReceiptTypeBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode PaymentReceiptTypeBox to nil")
	}
	v, err := DecodeTDLibJSONPaymentReceiptType(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.PaymentReceiptType = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for PaymentReceiptTypeBox.
func (b *PaymentReceiptTypeBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.PaymentReceiptType == nil {
		return fmt.Errorf("unable to encode PaymentReceiptTypeClass as nil")
	}
	return b.PaymentReceiptType.EncodeTDLibJSON(buf)
}
