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

// AssignStoreTransactionRequest represents TL type `assignStoreTransaction#86096bec`.
type AssignStoreTransactionRequest struct {
	// Information about the transaction
	Transaction StoreTransactionClass
	// Transaction purpose
	Purpose StorePaymentPurposeClass
}

// AssignStoreTransactionRequestTypeID is TL type id of AssignStoreTransactionRequest.
const AssignStoreTransactionRequestTypeID = 0x86096bec

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

func (a *AssignStoreTransactionRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Transaction == nil) {
		return false
	}
	if !(a.Purpose == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AssignStoreTransactionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "assignStoreTransaction",
		ID:   AssignStoreTransactionRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Transaction",
			SchemaName: "transaction",
		},
		{
			Name:       "Purpose",
			SchemaName: "purpose",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AssignStoreTransactionRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode assignStoreTransaction#86096bec as nil")
	}
	b.PutID(AssignStoreTransactionRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AssignStoreTransactionRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode assignStoreTransaction#86096bec as nil")
	}
	if a.Transaction == nil {
		return fmt.Errorf("unable to encode assignStoreTransaction#86096bec: field transaction is nil")
	}
	if err := a.Transaction.Encode(b); err != nil {
		return fmt.Errorf("unable to encode assignStoreTransaction#86096bec: field transaction: %w", err)
	}
	if a.Purpose == nil {
		return fmt.Errorf("unable to encode assignStoreTransaction#86096bec: field purpose is nil")
	}
	if err := a.Purpose.Encode(b); err != nil {
		return fmt.Errorf("unable to encode assignStoreTransaction#86096bec: field purpose: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AssignStoreTransactionRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode assignStoreTransaction#86096bec to nil")
	}
	if err := b.ConsumeID(AssignStoreTransactionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode assignStoreTransaction#86096bec: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AssignStoreTransactionRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode assignStoreTransaction#86096bec to nil")
	}
	{
		value, err := DecodeStoreTransaction(b)
		if err != nil {
			return fmt.Errorf("unable to decode assignStoreTransaction#86096bec: field transaction: %w", err)
		}
		a.Transaction = value
	}
	{
		value, err := DecodeStorePaymentPurpose(b)
		if err != nil {
			return fmt.Errorf("unable to decode assignStoreTransaction#86096bec: field purpose: %w", err)
		}
		a.Purpose = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AssignStoreTransactionRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode assignStoreTransaction#86096bec as nil")
	}
	b.ObjStart()
	b.PutID("assignStoreTransaction")
	b.Comma()
	b.FieldStart("transaction")
	if a.Transaction == nil {
		return fmt.Errorf("unable to encode assignStoreTransaction#86096bec: field transaction is nil")
	}
	if err := a.Transaction.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode assignStoreTransaction#86096bec: field transaction: %w", err)
	}
	b.Comma()
	b.FieldStart("purpose")
	if a.Purpose == nil {
		return fmt.Errorf("unable to encode assignStoreTransaction#86096bec: field purpose is nil")
	}
	if err := a.Purpose.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode assignStoreTransaction#86096bec: field purpose: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AssignStoreTransactionRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode assignStoreTransaction#86096bec to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("assignStoreTransaction"); err != nil {
				return fmt.Errorf("unable to decode assignStoreTransaction#86096bec: %w", err)
			}
		case "transaction":
			value, err := DecodeTDLibJSONStoreTransaction(b)
			if err != nil {
				return fmt.Errorf("unable to decode assignStoreTransaction#86096bec: field transaction: %w", err)
			}
			a.Transaction = value
		case "purpose":
			value, err := DecodeTDLibJSONStorePaymentPurpose(b)
			if err != nil {
				return fmt.Errorf("unable to decode assignStoreTransaction#86096bec: field purpose: %w", err)
			}
			a.Purpose = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetPurpose returns value of Purpose field.
func (a *AssignStoreTransactionRequest) GetPurpose() (value StorePaymentPurposeClass) {
	if a == nil {
		return
	}
	return a.Purpose
}

// AssignStoreTransaction invokes method assignStoreTransaction#86096bec returning error if any.
func (c *Client) AssignStoreTransaction(ctx context.Context, request *AssignStoreTransactionRequest) error {
	var ok Ok

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