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

// MessagesReorderQuickRepliesRequest represents TL type `messages.reorderQuickReplies#60331907`.
// Reorder quick reply shortcuts¹.
// This will emit an updateQuickReplies¹ update to other logged-in sessions.
//
// Links:
//  1. https://core.telegram.org/api/business#quick-reply-shortcuts
//  2. https://core.telegram.org/constructor/updateQuickReplies
//
// See https://core.telegram.org/method/messages.reorderQuickReplies for reference.
type MessagesReorderQuickRepliesRequest struct {
	// IDs of all created quick reply shortcuts¹, in the desired order.
	//
	// Links:
	//  1) https://core.telegram.org/api/business#quick-reply-shortcuts
	Order []int
}

// MessagesReorderQuickRepliesRequestTypeID is TL type id of MessagesReorderQuickRepliesRequest.
const MessagesReorderQuickRepliesRequestTypeID = 0x60331907

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

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

	return true
}

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

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

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

// TypeName returns name of type in TL schema.
func (*MessagesReorderQuickRepliesRequest) TypeName() string {
	return "messages.reorderQuickReplies"
}

// TypeInfo returns info about TL type.
func (r *MessagesReorderQuickRepliesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.reorderQuickReplies",
		ID:   MessagesReorderQuickRepliesRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Order",
			SchemaName: "order",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *MessagesReorderQuickRepliesRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.reorderQuickReplies#60331907 as nil")
	}
	b.PutID(MessagesReorderQuickRepliesRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *MessagesReorderQuickRepliesRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.reorderQuickReplies#60331907 as nil")
	}
	b.PutVectorHeader(len(r.Order))
	for _, v := range r.Order {
		b.PutInt(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *MessagesReorderQuickRepliesRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.reorderQuickReplies#60331907 to nil")
	}
	if err := b.ConsumeID(MessagesReorderQuickRepliesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.reorderQuickReplies#60331907: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *MessagesReorderQuickRepliesRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.reorderQuickReplies#60331907 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.reorderQuickReplies#60331907: 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 messages.reorderQuickReplies#60331907: field order: %w", err)
			}
			r.Order = append(r.Order, value)
		}
	}
	return nil
}

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

// MessagesReorderQuickReplies invokes method messages.reorderQuickReplies#60331907 returning error if any.
// Reorder quick reply shortcuts¹.
// This will emit an updateQuickReplies¹ update to other logged-in sessions.
//
// Links:
//  1. https://core.telegram.org/api/business#quick-reply-shortcuts
//  2. https://core.telegram.org/constructor/updateQuickReplies
//
// Possible errors:
//
//	403 PREMIUM_ACCOUNT_REQUIRED: A premium account is required to execute this action.
//
// See https://core.telegram.org/method/messages.reorderQuickReplies for reference.
func (c *Client) MessagesReorderQuickReplies(ctx context.Context, order []int) (bool, error) {
	var result BoolBox

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