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

// StoriesTogglePinnedToTopRequest represents TL type `stories.togglePinnedToTop#b297e9b`.
// Pin some stories to the top of the profile, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/stories#pinned-or-archived-stories
//
// See https://core.telegram.org/method/stories.togglePinnedToTop for reference.
type StoriesTogglePinnedToTopRequest struct {
	// Peer where to pin stories.
	Peer InputPeerClass
	// IDs of the stories to pin (max stories_pinned_to_top_count_max¹).
	//
	// Links:
	//  1) https://core.telegram.org/api/config#stories-pinned-to-top-count-max
	ID []int
}

// StoriesTogglePinnedToTopRequestTypeID is TL type id of StoriesTogglePinnedToTopRequest.
const StoriesTogglePinnedToTopRequestTypeID = 0xb297e9b

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

func (t *StoriesTogglePinnedToTopRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Peer == nil) {
		return false
	}
	if !(t.ID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesTogglePinnedToTopRequest from given interface.
func (t *StoriesTogglePinnedToTopRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetID() (value []int)
}) {
	t.Peer = from.GetPeer()
	t.ID = from.GetID()
}

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

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

// TypeInfo returns info about TL type.
func (t *StoriesTogglePinnedToTopRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.togglePinnedToTop",
		ID:   StoriesTogglePinnedToTopRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *StoriesTogglePinnedToTopRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode stories.togglePinnedToTop#b297e9b as nil")
	}
	b.PutID(StoriesTogglePinnedToTopRequestTypeID)
	return t.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (t *StoriesTogglePinnedToTopRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode stories.togglePinnedToTop#b297e9b to nil")
	}
	if err := b.ConsumeID(StoriesTogglePinnedToTopRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stories.togglePinnedToTop#b297e9b: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *StoriesTogglePinnedToTopRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode stories.togglePinnedToTop#b297e9b to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stories.togglePinnedToTop#b297e9b: field peer: %w", err)
		}
		t.Peer = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.togglePinnedToTop#b297e9b: field id: %w", err)
		}

		if headerLen > 0 {
			t.ID = 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.togglePinnedToTop#b297e9b: field id: %w", err)
			}
			t.ID = append(t.ID, value)
		}
	}
	return nil
}

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

// GetID returns value of ID field.
func (t *StoriesTogglePinnedToTopRequest) GetID() (value []int) {
	if t == nil {
		return
	}
	return t.ID
}

// StoriesTogglePinnedToTop invokes method stories.togglePinnedToTop#b297e9b returning error if any.
// Pin some stories to the top of the profile, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/stories#pinned-or-archived-stories
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//	400 STORY_ID_INVALID: The specified story ID is invalid.
//
// See https://core.telegram.org/method/stories.togglePinnedToTop for reference.
func (c *Client) StoriesTogglePinnedToTop(ctx context.Context, request *StoriesTogglePinnedToTopRequest) (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
}
