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

// PaymentReceipt represents TL type `paymentReceipt#2d313392`.
type PaymentReceipt struct {
	// Information about the product
	ProductInfo ProductInfo
	// Point in time (Unix timestamp) when the payment was made
	Date int32
	// User identifier of the seller bot
	SellerBotUserID int64
	// Type of the payment receipt
	Type PaymentReceiptTypeClass
}

// PaymentReceiptTypeID is TL type id of PaymentReceipt.
const PaymentReceiptTypeID = 0x2d313392

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

func (p *PaymentReceipt) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.ProductInfo.Zero()) {
		return false
	}
	if !(p.Date == 0) {
		return false
	}
	if !(p.SellerBotUserID == 0) {
		return false
	}
	if !(p.Type == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PaymentReceipt) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "paymentReceipt",
		ID:   PaymentReceiptTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ProductInfo",
			SchemaName: "product_info",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "SellerBotUserID",
			SchemaName: "seller_bot_user_id",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PaymentReceipt) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentReceipt#2d313392 as nil")
	}
	b.PutID(PaymentReceiptTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PaymentReceipt) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentReceipt#2d313392 as nil")
	}
	if err := p.ProductInfo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode paymentReceipt#2d313392: field product_info: %w", err)
	}
	b.PutInt32(p.Date)
	b.PutInt53(p.SellerBotUserID)
	if p.Type == nil {
		return fmt.Errorf("unable to encode paymentReceipt#2d313392: field type is nil")
	}
	if err := p.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode paymentReceipt#2d313392: field type: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *PaymentReceipt) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode paymentReceipt#2d313392 to nil")
	}
	{
		if err := p.ProductInfo.Decode(b); err != nil {
			return fmt.Errorf("unable to decode paymentReceipt#2d313392: field product_info: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode paymentReceipt#2d313392: field date: %w", err)
		}
		p.Date = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode paymentReceipt#2d313392: field seller_bot_user_id: %w", err)
		}
		p.SellerBotUserID = value
	}
	{
		value, err := DecodePaymentReceiptType(b)
		if err != nil {
			return fmt.Errorf("unable to decode paymentReceipt#2d313392: field type: %w", err)
		}
		p.Type = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PaymentReceipt) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentReceipt#2d313392 as nil")
	}
	b.ObjStart()
	b.PutID("paymentReceipt")
	b.Comma()
	b.FieldStart("product_info")
	if err := p.ProductInfo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode paymentReceipt#2d313392: field product_info: %w", err)
	}
	b.Comma()
	b.FieldStart("date")
	b.PutInt32(p.Date)
	b.Comma()
	b.FieldStart("seller_bot_user_id")
	b.PutInt53(p.SellerBotUserID)
	b.Comma()
	b.FieldStart("type")
	if p.Type == nil {
		return fmt.Errorf("unable to encode paymentReceipt#2d313392: field type is nil")
	}
	if err := p.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode paymentReceipt#2d313392: field type: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("paymentReceipt"); err != nil {
				return fmt.Errorf("unable to decode paymentReceipt#2d313392: %w", err)
			}
		case "product_info":
			if err := p.ProductInfo.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode paymentReceipt#2d313392: field product_info: %w", err)
			}
		case "date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode paymentReceipt#2d313392: field date: %w", err)
			}
			p.Date = value
		case "seller_bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode paymentReceipt#2d313392: field seller_bot_user_id: %w", err)
			}
			p.SellerBotUserID = value
		case "type":
			value, err := DecodeTDLibJSONPaymentReceiptType(b)
			if err != nil {
				return fmt.Errorf("unable to decode paymentReceipt#2d313392: field type: %w", err)
			}
			p.Type = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetProductInfo returns value of ProductInfo field.
func (p *PaymentReceipt) GetProductInfo() (value ProductInfo) {
	if p == nil {
		return
	}
	return p.ProductInfo
}

// GetDate returns value of Date field.
func (p *PaymentReceipt) GetDate() (value int32) {
	if p == nil {
		return
	}
	return p.Date
}

// GetSellerBotUserID returns value of SellerBotUserID field.
func (p *PaymentReceipt) GetSellerBotUserID() (value int64) {
	if p == nil {
		return
	}
	return p.SellerBotUserID
}

// GetType returns value of Type field.
func (p *PaymentReceipt) GetType() (value PaymentReceiptTypeClass) {
	if p == nil {
		return
	}
	return p.Type
}
