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

// PaymentsReorderStarGiftCollectionsRequest represents TL type `payments.reorderStarGiftCollections#c32af4cc`.
// Reorder the star gift collections »¹ on an owned peer's profile.
//
// Links:
//  1. https://core.telegram.org/api/gifts#gift-collections
//
// See https://core.telegram.org/method/payments.reorderStarGiftCollections for reference.
type PaymentsReorderStarGiftCollectionsRequest struct {
	// The owned peer.
	Peer InputPeerClass
	// New collection order.
	Order []int
}

// PaymentsReorderStarGiftCollectionsRequestTypeID is TL type id of PaymentsReorderStarGiftCollectionsRequest.
const PaymentsReorderStarGiftCollectionsRequestTypeID = 0xc32af4cc

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

func (r *PaymentsReorderStarGiftCollectionsRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Peer == nil) {
		return false
	}
	if !(r.Order == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsReorderStarGiftCollectionsRequest from given interface.
func (r *PaymentsReorderStarGiftCollectionsRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetOrder() (value []int)
}) {
	r.Peer = from.GetPeer()
	r.Order = from.GetOrder()
}

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

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

// TypeInfo returns info about TL type.
func (r *PaymentsReorderStarGiftCollectionsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.reorderStarGiftCollections",
		ID:   PaymentsReorderStarGiftCollectionsRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Order",
			SchemaName: "order",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *PaymentsReorderStarGiftCollectionsRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode payments.reorderStarGiftCollections#c32af4cc as nil")
	}
	b.PutID(PaymentsReorderStarGiftCollectionsRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *PaymentsReorderStarGiftCollectionsRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode payments.reorderStarGiftCollections#c32af4cc as nil")
	}
	if r.Peer == nil {
		return fmt.Errorf("unable to encode payments.reorderStarGiftCollections#c32af4cc: field peer is nil")
	}
	if err := r.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.reorderStarGiftCollections#c32af4cc: field peer: %w", err)
	}
	b.PutVectorHeader(len(r.Order))
	for _, v := range r.Order {
		b.PutInt(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *PaymentsReorderStarGiftCollectionsRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode payments.reorderStarGiftCollections#c32af4cc to nil")
	}
	if err := b.ConsumeID(PaymentsReorderStarGiftCollectionsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.reorderStarGiftCollections#c32af4cc: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *PaymentsReorderStarGiftCollectionsRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode payments.reorderStarGiftCollections#c32af4cc to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.reorderStarGiftCollections#c32af4cc: field peer: %w", err)
		}
		r.Peer = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.reorderStarGiftCollections#c32af4cc: field order: %w", err)
		}

		if headerLen > 0 {
			r.Order = make([]int, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int()
			if err != nil {
				return fmt.Errorf("unable to decode payments.reorderStarGiftCollections#c32af4cc: field order: %w", err)
			}
			r.Order = append(r.Order, value)
		}
	}
	return nil
}

// GetPeer returns value of Peer field.
func (r *PaymentsReorderStarGiftCollectionsRequest) GetPeer() (value InputPeerClass) {
	if r == nil {
		return
	}
	return r.Peer
}

// GetOrder returns value of Order field.
func (r *PaymentsReorderStarGiftCollectionsRequest) GetOrder() (value []int) {
	if r == nil {
		return
	}
	return r.Order
}

// PaymentsReorderStarGiftCollections invokes method payments.reorderStarGiftCollections#c32af4cc returning error if any.
// Reorder the star gift collections »¹ on an owned peer's profile.
//
// Links:
//  1. https://core.telegram.org/api/gifts#gift-collections
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/payments.reorderStarGiftCollections for reference.
func (c *Client) PaymentsReorderStarGiftCollections(ctx context.Context, request *PaymentsReorderStarGiftCollectionsRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
