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

// StoriesReorderAlbumsRequest represents TL type `stories.reorderAlbums#8535fbd9`.
// Reorder story albums on a profile »¹.
//
// Links:
//  1. https://core.telegram.org/api/stories#story-albums
//
// See https://core.telegram.org/method/stories.reorderAlbums for reference.
type StoriesReorderAlbumsRequest struct {
	// Peer where the albums are located.
	Peer InputPeerClass
	// New order of the albums.
	Order []int
}

// StoriesReorderAlbumsRequestTypeID is TL type id of StoriesReorderAlbumsRequest.
const StoriesReorderAlbumsRequestTypeID = 0x8535fbd9

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

func (r *StoriesReorderAlbumsRequest) 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 *StoriesReorderAlbumsRequest) String() string {
	if r == nil {
		return "StoriesReorderAlbumsRequest(nil)"
	}
	type Alias StoriesReorderAlbumsRequest
	return fmt.Sprintf("StoriesReorderAlbumsRequest%+v", Alias(*r))
}

// FillFrom fills StoriesReorderAlbumsRequest from given interface.
func (r *StoriesReorderAlbumsRequest) 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 (*StoriesReorderAlbumsRequest) TypeID() uint32 {
	return StoriesReorderAlbumsRequestTypeID
}

// TypeName returns name of type in TL schema.
func (*StoriesReorderAlbumsRequest) TypeName() string {
	return "stories.reorderAlbums"
}

// TypeInfo returns info about TL type.
func (r *StoriesReorderAlbumsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.reorderAlbums",
		ID:   StoriesReorderAlbumsRequestTypeID,
	}
	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 *StoriesReorderAlbumsRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode stories.reorderAlbums#8535fbd9 as nil")
	}
	b.PutID(StoriesReorderAlbumsRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *StoriesReorderAlbumsRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode stories.reorderAlbums#8535fbd9 as nil")
	}
	if r.Peer == nil {
		return fmt.Errorf("unable to encode stories.reorderAlbums#8535fbd9: field peer is nil")
	}
	if err := r.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.reorderAlbums#8535fbd9: 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 *StoriesReorderAlbumsRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode stories.reorderAlbums#8535fbd9 to nil")
	}
	if err := b.ConsumeID(StoriesReorderAlbumsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stories.reorderAlbums#8535fbd9: %w", err)
	}
	return r.DecodeBare(b)
}

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

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

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

// StoriesReorderAlbums invokes method stories.reorderAlbums#8535fbd9 returning error if any.
// Reorder story albums on a profile »¹.
//
// Links:
//  1. https://core.telegram.org/api/stories#story-albums
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/stories.reorderAlbums for reference.
func (c *Client) StoriesReorderAlbums(ctx context.Context, request *StoriesReorderAlbumsRequest) (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
}
