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

// SetAuthenticationPremiumPurchaseTransactionRequest represents TL type `setAuthenticationPremiumPurchaseTransaction#e51e7c79`.
type SetAuthenticationPremiumPurchaseTransactionRequest struct {
	// Information about the transaction
	Transaction StoreTransactionClass
	// Pass true if this is a restore of a Telegram Premium purchase; only for App Store
	IsRestore bool
	// ISO 4217 currency code of the payment currency
	Currency string
	// Paid amount, in the smallest units of the currency
	Amount int64
}

// SetAuthenticationPremiumPurchaseTransactionRequestTypeID is TL type id of SetAuthenticationPremiumPurchaseTransactionRequest.
const SetAuthenticationPremiumPurchaseTransactionRequestTypeID = 0xe51e7c79

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

func (s *SetAuthenticationPremiumPurchaseTransactionRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Transaction == nil) {
		return false
	}
	if !(s.IsRestore == false) {
		return false
	}
	if !(s.Currency == "") {
		return false
	}
	if !(s.Amount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetAuthenticationPremiumPurchaseTransactionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setAuthenticationPremiumPurchaseTransaction",
		ID:   SetAuthenticationPremiumPurchaseTransactionRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Transaction",
			SchemaName: "transaction",
		},
		{
			Name:       "IsRestore",
			SchemaName: "is_restore",
		},
		{
			Name:       "Currency",
			SchemaName: "currency",
		},
		{
			Name:       "Amount",
			SchemaName: "amount",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetAuthenticationPremiumPurchaseTransactionRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setAuthenticationPremiumPurchaseTransaction#e51e7c79 as nil")
	}
	b.PutID(SetAuthenticationPremiumPurchaseTransactionRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetAuthenticationPremiumPurchaseTransactionRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setAuthenticationPremiumPurchaseTransaction#e51e7c79 as nil")
	}
	if s.Transaction == nil {
		return fmt.Errorf("unable to encode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field transaction is nil")
	}
	if err := s.Transaction.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field transaction: %w", err)
	}
	b.PutBool(s.IsRestore)
	b.PutString(s.Currency)
	b.PutInt53(s.Amount)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetAuthenticationPremiumPurchaseTransactionRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setAuthenticationPremiumPurchaseTransaction#e51e7c79 to nil")
	}
	{
		value, err := DecodeStoreTransaction(b)
		if err != nil {
			return fmt.Errorf("unable to decode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field transaction: %w", err)
		}
		s.Transaction = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field is_restore: %w", err)
		}
		s.IsRestore = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field currency: %w", err)
		}
		s.Currency = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field amount: %w", err)
		}
		s.Amount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetAuthenticationPremiumPurchaseTransactionRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setAuthenticationPremiumPurchaseTransaction#e51e7c79 as nil")
	}
	b.ObjStart()
	b.PutID("setAuthenticationPremiumPurchaseTransaction")
	b.Comma()
	b.FieldStart("transaction")
	if s.Transaction == nil {
		return fmt.Errorf("unable to encode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field transaction is nil")
	}
	if err := s.Transaction.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field transaction: %w", err)
	}
	b.Comma()
	b.FieldStart("is_restore")
	b.PutBool(s.IsRestore)
	b.Comma()
	b.FieldStart("currency")
	b.PutString(s.Currency)
	b.Comma()
	b.FieldStart("amount")
	b.PutInt53(s.Amount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setAuthenticationPremiumPurchaseTransaction"); err != nil {
				return fmt.Errorf("unable to decode setAuthenticationPremiumPurchaseTransaction#e51e7c79: %w", err)
			}
		case "transaction":
			value, err := DecodeTDLibJSONStoreTransaction(b)
			if err != nil {
				return fmt.Errorf("unable to decode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field transaction: %w", err)
			}
			s.Transaction = value
		case "is_restore":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field is_restore: %w", err)
			}
			s.IsRestore = value
		case "currency":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field currency: %w", err)
			}
			s.Currency = value
		case "amount":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setAuthenticationPremiumPurchaseTransaction#e51e7c79: field amount: %w", err)
			}
			s.Amount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTransaction returns value of Transaction field.
func (s *SetAuthenticationPremiumPurchaseTransactionRequest) GetTransaction() (value StoreTransactionClass) {
	if s == nil {
		return
	}
	return s.Transaction
}

// GetIsRestore returns value of IsRestore field.
func (s *SetAuthenticationPremiumPurchaseTransactionRequest) GetIsRestore() (value bool) {
	if s == nil {
		return
	}
	return s.IsRestore
}

// GetCurrency returns value of Currency field.
func (s *SetAuthenticationPremiumPurchaseTransactionRequest) GetCurrency() (value string) {
	if s == nil {
		return
	}
	return s.Currency
}

// GetAmount returns value of Amount field.
func (s *SetAuthenticationPremiumPurchaseTransactionRequest) GetAmount() (value int64) {
	if s == nil {
		return
	}
	return s.Amount
}

// SetAuthenticationPremiumPurchaseTransaction invokes method setAuthenticationPremiumPurchaseTransaction#e51e7c79 returning error if any.
func (c *Client) SetAuthenticationPremiumPurchaseTransaction(ctx context.Context, request *SetAuthenticationPremiumPurchaseTransactionRequest) error {
	var ok Ok

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