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

// SendPaymentFormRequest represents TL type `sendPaymentForm#c66e388a`.
type SendPaymentFormRequest struct {
	// The invoice
	InputInvoice InputInvoiceClass
	// Payment form identifier returned by getPaymentForm
	PaymentFormID int64
	// Identifier returned by validateOrderInfo, or an empty string
	OrderInfoID string
	// Identifier of a chosen shipping option, if applicable
	ShippingOptionID string
	// The credentials chosen by user for payment; pass null for a payment in Telegram Stars
	Credentials InputCredentialsClass
	// Chosen by the user amount of tip in the smallest units of the currency
	TipAmount int64
}

// SendPaymentFormRequestTypeID is TL type id of SendPaymentFormRequest.
const SendPaymentFormRequestTypeID = 0xc66e388a

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

func (s *SendPaymentFormRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.InputInvoice == nil) {
		return false
	}
	if !(s.PaymentFormID == 0) {
		return false
	}
	if !(s.OrderInfoID == "") {
		return false
	}
	if !(s.ShippingOptionID == "") {
		return false
	}
	if !(s.Credentials == nil) {
		return false
	}
	if !(s.TipAmount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SendPaymentFormRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sendPaymentForm",
		ID:   SendPaymentFormRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "InputInvoice",
			SchemaName: "input_invoice",
		},
		{
			Name:       "PaymentFormID",
			SchemaName: "payment_form_id",
		},
		{
			Name:       "OrderInfoID",
			SchemaName: "order_info_id",
		},
		{
			Name:       "ShippingOptionID",
			SchemaName: "shipping_option_id",
		},
		{
			Name:       "Credentials",
			SchemaName: "credentials",
		},
		{
			Name:       "TipAmount",
			SchemaName: "tip_amount",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SendPaymentFormRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendPaymentForm#c66e388a as nil")
	}
	b.PutID(SendPaymentFormRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SendPaymentFormRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendPaymentForm#c66e388a as nil")
	}
	if s.InputInvoice == nil {
		return fmt.Errorf("unable to encode sendPaymentForm#c66e388a: field input_invoice is nil")
	}
	if err := s.InputInvoice.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sendPaymentForm#c66e388a: field input_invoice: %w", err)
	}
	b.PutLong(s.PaymentFormID)
	b.PutString(s.OrderInfoID)
	b.PutString(s.ShippingOptionID)
	if s.Credentials == nil {
		return fmt.Errorf("unable to encode sendPaymentForm#c66e388a: field credentials is nil")
	}
	if err := s.Credentials.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sendPaymentForm#c66e388a: field credentials: %w", err)
	}
	b.PutInt53(s.TipAmount)
	return nil
}

// Decode implements bin.Decoder.
func (s *SendPaymentFormRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sendPaymentForm#c66e388a to nil")
	}
	if err := b.ConsumeID(SendPaymentFormRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SendPaymentFormRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sendPaymentForm#c66e388a to nil")
	}
	{
		value, err := DecodeInputInvoice(b)
		if err != nil {
			return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field input_invoice: %w", err)
		}
		s.InputInvoice = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field payment_form_id: %w", err)
		}
		s.PaymentFormID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field order_info_id: %w", err)
		}
		s.OrderInfoID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field shipping_option_id: %w", err)
		}
		s.ShippingOptionID = value
	}
	{
		value, err := DecodeInputCredentials(b)
		if err != nil {
			return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field credentials: %w", err)
		}
		s.Credentials = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field tip_amount: %w", err)
		}
		s.TipAmount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SendPaymentFormRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode sendPaymentForm#c66e388a as nil")
	}
	b.ObjStart()
	b.PutID("sendPaymentForm")
	b.Comma()
	b.FieldStart("input_invoice")
	if s.InputInvoice == nil {
		return fmt.Errorf("unable to encode sendPaymentForm#c66e388a: field input_invoice is nil")
	}
	if err := s.InputInvoice.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode sendPaymentForm#c66e388a: field input_invoice: %w", err)
	}
	b.Comma()
	b.FieldStart("payment_form_id")
	b.PutLong(s.PaymentFormID)
	b.Comma()
	b.FieldStart("order_info_id")
	b.PutString(s.OrderInfoID)
	b.Comma()
	b.FieldStart("shipping_option_id")
	b.PutString(s.ShippingOptionID)
	b.Comma()
	b.FieldStart("credentials")
	if s.Credentials == nil {
		return fmt.Errorf("unable to encode sendPaymentForm#c66e388a: field credentials is nil")
	}
	if err := s.Credentials.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode sendPaymentForm#c66e388a: field credentials: %w", err)
	}
	b.Comma()
	b.FieldStart("tip_amount")
	b.PutInt53(s.TipAmount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SendPaymentFormRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode sendPaymentForm#c66e388a to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("sendPaymentForm"); err != nil {
				return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: %w", err)
			}
		case "input_invoice":
			value, err := DecodeTDLibJSONInputInvoice(b)
			if err != nil {
				return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field input_invoice: %w", err)
			}
			s.InputInvoice = value
		case "payment_form_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field payment_form_id: %w", err)
			}
			s.PaymentFormID = value
		case "order_info_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field order_info_id: %w", err)
			}
			s.OrderInfoID = value
		case "shipping_option_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field shipping_option_id: %w", err)
			}
			s.ShippingOptionID = value
		case "credentials":
			value, err := DecodeTDLibJSONInputCredentials(b)
			if err != nil {
				return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field credentials: %w", err)
			}
			s.Credentials = value
		case "tip_amount":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode sendPaymentForm#c66e388a: field tip_amount: %w", err)
			}
			s.TipAmount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetInputInvoice returns value of InputInvoice field.
func (s *SendPaymentFormRequest) GetInputInvoice() (value InputInvoiceClass) {
	if s == nil {
		return
	}
	return s.InputInvoice
}

// GetPaymentFormID returns value of PaymentFormID field.
func (s *SendPaymentFormRequest) GetPaymentFormID() (value int64) {
	if s == nil {
		return
	}
	return s.PaymentFormID
}

// GetOrderInfoID returns value of OrderInfoID field.
func (s *SendPaymentFormRequest) GetOrderInfoID() (value string) {
	if s == nil {
		return
	}
	return s.OrderInfoID
}

// GetShippingOptionID returns value of ShippingOptionID field.
func (s *SendPaymentFormRequest) GetShippingOptionID() (value string) {
	if s == nil {
		return
	}
	return s.ShippingOptionID
}

// GetCredentials returns value of Credentials field.
func (s *SendPaymentFormRequest) GetCredentials() (value InputCredentialsClass) {
	if s == nil {
		return
	}
	return s.Credentials
}

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

// SendPaymentForm invokes method sendPaymentForm#c66e388a returning error if any.
func (c *Client) SendPaymentForm(ctx context.Context, request *SendPaymentFormRequest) (*PaymentResult, error) {
	var result PaymentResult

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
