// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// PaymentsGetPaymentFormRequest represents TL type `payments.getPaymentForm#37148dbb`.
// Get a payment form
//
// See https://core.telegram.org/method/payments.getPaymentForm for reference.
type PaymentsGetPaymentFormRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Invoice
	Invoice InputInvoiceClass
	// Theme parameters »¹
	//
	// Links:
	//  1) https://core.telegram.org/api/bots/webapps#theme-parameters
	//
	// Use SetThemeParams and GetThemeParams helpers.
	ThemeParams DataJSON
}

// PaymentsGetPaymentFormRequestTypeID is TL type id of PaymentsGetPaymentFormRequest.
const PaymentsGetPaymentFormRequestTypeID = 0x37148dbb

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

func (g *PaymentsGetPaymentFormRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.Invoice == nil) {
		return false
	}
	if !(g.ThemeParams.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsGetPaymentFormRequest from given interface.
func (g *PaymentsGetPaymentFormRequest) FillFrom(from interface {
	GetInvoice() (value InputInvoiceClass)
	GetThemeParams() (value DataJSON, ok bool)
}) {
	g.Invoice = from.GetInvoice()
	if val, ok := from.GetThemeParams(); ok {
		g.ThemeParams = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*PaymentsGetPaymentFormRequest) TypeName() string {
	return "payments.getPaymentForm"
}

// TypeInfo returns info about TL type.
func (g *PaymentsGetPaymentFormRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.getPaymentForm",
		ID:   PaymentsGetPaymentFormRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Invoice",
			SchemaName: "invoice",
		},
		{
			Name:       "ThemeParams",
			SchemaName: "theme_params",
			Null:       !g.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *PaymentsGetPaymentFormRequest) SetFlags() {
	if !(g.ThemeParams.Zero()) {
		g.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (g *PaymentsGetPaymentFormRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getPaymentForm#37148dbb as nil")
	}
	b.PutID(PaymentsGetPaymentFormRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *PaymentsGetPaymentFormRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getPaymentForm#37148dbb as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getPaymentForm#37148dbb: field flags: %w", err)
	}
	if g.Invoice == nil {
		return fmt.Errorf("unable to encode payments.getPaymentForm#37148dbb: field invoice is nil")
	}
	if err := g.Invoice.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getPaymentForm#37148dbb: field invoice: %w", err)
	}
	if g.Flags.Has(0) {
		if err := g.ThemeParams.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.getPaymentForm#37148dbb: field theme_params: %w", err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *PaymentsGetPaymentFormRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getPaymentForm#37148dbb to nil")
	}
	if err := b.ConsumeID(PaymentsGetPaymentFormRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.getPaymentForm#37148dbb: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *PaymentsGetPaymentFormRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getPaymentForm#37148dbb to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.getPaymentForm#37148dbb: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputInvoice(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getPaymentForm#37148dbb: field invoice: %w", err)
		}
		g.Invoice = value
	}
	if g.Flags.Has(0) {
		if err := g.ThemeParams.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.getPaymentForm#37148dbb: field theme_params: %w", err)
		}
	}
	return nil
}

// GetInvoice returns value of Invoice field.
func (g *PaymentsGetPaymentFormRequest) GetInvoice() (value InputInvoiceClass) {
	if g == nil {
		return
	}
	return g.Invoice
}

// SetThemeParams sets value of ThemeParams conditional field.
func (g *PaymentsGetPaymentFormRequest) SetThemeParams(value DataJSON) {
	g.Flags.Set(0)
	g.ThemeParams = value
}

// GetThemeParams returns value of ThemeParams conditional field and
// boolean which is true if field was set.
func (g *PaymentsGetPaymentFormRequest) GetThemeParams() (value DataJSON, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(0) {
		return value, false
	}
	return g.ThemeParams, true
}

// PaymentsGetPaymentForm invokes method payments.getPaymentForm#37148dbb returning error if any.
// Get a payment form
//
// Possible errors:
//
//	406 API_GIFT_RESTRICTED_UPDATE_APP: Please update the app to access the gift API.
//	400 BOOST_PEER_INVALID: The specified boost_peer is invalid.
//	403 BOT_ACCESS_FORBIDDEN: The specified method can be used over a business connection for some operations, but the specified query attempted an operation that is not allowed over a business connection.
//	400 BOT_INVOICE_INVALID: The specified invoice is invalid.
//	400 BUSINESS_CONNECTION_INVALID: The connection_id passed to the wrapping invokeWithBusinessConnection call is invalid.
//	400 GIFT_MONTHS_INVALID: The value passed in invoice.inputInvoicePremiumGiftStars.months is invalid.
//	400 INVOICE_INVALID: The specified invoice is invalid.
//	400 MESSAGE_ID_INVALID: The provided message id is invalid.
//	400 MONTH_INVALID: The number of months specified in inputInvoicePremiumGiftStars.months is invalid.
//	400 NO_PAYMENT_NEEDED: The upgrade/transfer of the specified gift was already paid for or is free.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//	400 SLUG_INVALID: The specified invoice slug is invalid.
//	400 STARGIFT_ALREADY_CONVERTED: The specified star gift was already converted to Stars.
//	400 STARGIFT_ALREADY_REFUNDED: The specified star gift was already refunded.
//	400 STARGIFT_ALREADY_UPGRADED: The specified gift was already upgraded to a collectible gift.
//	406 STARGIFT_EXPORT_IN_PROGRESS: A gift export is in progress, a detailed and localized description for the error will be emitted via an updateServiceNotification as specified here ».
//	400 STARGIFT_INVALID: The passed gift is invalid.
//	400 STARGIFT_NOT_FOUND: The specified gift was not found.
//	400 STARGIFT_OWNER_INVALID: You cannot transfer or sell a gift owned by another user.
//	400 STARGIFT_PEER_INVALID: The specified inputSavedStarGiftChat.peer is invalid.
//	400 STARGIFT_RESELL_CURRENCY_NOT_ALLOWED: You can't buy the gift using the specified currency (i.e. trying to pay in Stars for TON gifts).
//	400 STARGIFT_SLUG_INVALID: The specified gift slug is invalid.
//	400 STARGIFT_TRANSFER_TOO_EARLY_%d: You cannot transfer this gift yet, wait %d seconds.
//	400 STARGIFT_UPGRADE_UNAVAILABLE: A received gift can only be upgraded to a collectible gift if the messageActionStarGift/savedStarGift.can_upgrade flag is set.
//	406 STARS_FORM_AMOUNT_MISMATCH: The form amount has changed, please fetch the new form using payments.getPaymentForm and restart the process.
//	400 TO_ID_INVALID: The specified to_id of the passed inputInvoiceStarGiftResale or inputInvoiceStarGiftTransfer is invalid.
//	400 UNTIL_DATE_INVALID: Invalid until date provided.
//
// See https://core.telegram.org/method/payments.getPaymentForm for reference.
func (c *Client) PaymentsGetPaymentForm(ctx context.Context, request *PaymentsGetPaymentFormRequest) (PaymentsPaymentFormClass, error) {
	var result PaymentsPaymentFormBox

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