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

// PaymentForm represents TL type `paymentForm#7720ffb3`.
type PaymentForm struct {
	// The payment form identifier
	ID int64
	// Type of the payment form
	Type PaymentFormTypeClass
	// User identifier of the seller bot
	SellerBotUserID int64
	// Information about the product
	ProductInfo ProductInfo
}

// PaymentFormTypeID is TL type id of PaymentForm.
const PaymentFormTypeID = 0x7720ffb3

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (p *PaymentForm) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentForm#7720ffb3 as nil")
	}
	b.PutID(PaymentFormTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PaymentForm) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentForm#7720ffb3 as nil")
	}
	b.PutLong(p.ID)
	if p.Type == nil {
		return fmt.Errorf("unable to encode paymentForm#7720ffb3: field type is nil")
	}
	if err := p.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode paymentForm#7720ffb3: field type: %w", err)
	}
	b.PutInt53(p.SellerBotUserID)
	if err := p.ProductInfo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode paymentForm#7720ffb3: field product_info: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *PaymentForm) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode paymentForm#7720ffb3 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode paymentForm#7720ffb3: field id: %w", err)
		}
		p.ID = value
	}
	{
		value, err := DecodePaymentFormType(b)
		if err != nil {
			return fmt.Errorf("unable to decode paymentForm#7720ffb3: field type: %w", err)
		}
		p.Type = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode paymentForm#7720ffb3: field seller_bot_user_id: %w", err)
		}
		p.SellerBotUserID = value
	}
	{
		if err := p.ProductInfo.Decode(b); err != nil {
			return fmt.Errorf("unable to decode paymentForm#7720ffb3: field product_info: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PaymentForm) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode paymentForm#7720ffb3 as nil")
	}
	b.ObjStart()
	b.PutID("paymentForm")
	b.Comma()
	b.FieldStart("id")
	b.PutLong(p.ID)
	b.Comma()
	b.FieldStart("type")
	if p.Type == nil {
		return fmt.Errorf("unable to encode paymentForm#7720ffb3: field type is nil")
	}
	if err := p.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode paymentForm#7720ffb3: field type: %w", err)
	}
	b.Comma()
	b.FieldStart("seller_bot_user_id")
	b.PutInt53(p.SellerBotUserID)
	b.Comma()
	b.FieldStart("product_info")
	if err := p.ProductInfo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode paymentForm#7720ffb3: field product_info: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("paymentForm"); err != nil {
				return fmt.Errorf("unable to decode paymentForm#7720ffb3: %w", err)
			}
		case "id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode paymentForm#7720ffb3: field id: %w", err)
			}
			p.ID = value
		case "type":
			value, err := DecodeTDLibJSONPaymentFormType(b)
			if err != nil {
				return fmt.Errorf("unable to decode paymentForm#7720ffb3: field type: %w", err)
			}
			p.Type = value
		case "seller_bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode paymentForm#7720ffb3: field seller_bot_user_id: %w", err)
			}
			p.SellerBotUserID = value
		case "product_info":
			if err := p.ProductInfo.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode paymentForm#7720ffb3: field product_info: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (p *PaymentForm) GetID() (value int64) {
	if p == nil {
		return
	}
	return p.ID
}

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

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

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