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

// StoriesTogglePinnedRequest represents TL type `stories.togglePinned#9a75a1ef`.
// Pin or unpin one or more stories
//
// See https://core.telegram.org/method/stories.togglePinned for reference.
type StoriesTogglePinnedRequest struct {
	// Peer where to pin or unpin stories
	Peer InputPeerClass
	// IDs of stories to pin or unpin
	ID []int
	// Whether to pin or unpin the stories
	Pinned bool
}

// StoriesTogglePinnedRequestTypeID is TL type id of StoriesTogglePinnedRequest.
const StoriesTogglePinnedRequestTypeID = 0x9a75a1ef

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

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

	return true
}

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

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

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

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

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

// Encode implements bin.Encoder.
func (t *StoriesTogglePinnedRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode stories.togglePinned#9a75a1ef as nil")
	}
	b.PutID(StoriesTogglePinnedRequestTypeID)
	return t.EncodeBare(b)
}

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

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

// DecodeBare implements bin.BareDecoder.
func (t *StoriesTogglePinnedRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode stories.togglePinned#9a75a1ef to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stories.togglePinned#9a75a1ef: field peer: %w", err)
		}
		t.Peer = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.togglePinned#9a75a1ef: 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.togglePinned#9a75a1ef: field id: %w", err)
			}
			t.ID = append(t.ID, value)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode stories.togglePinned#9a75a1ef: field pinned: %w", err)
		}
		t.Pinned = value
	}
	return nil
}

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

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

// GetPinned returns value of Pinned field.
func (t *StoriesTogglePinnedRequest) GetPinned() (value bool) {
	if t == nil {
		return
	}
	return t.Pinned
}

// StoriesTogglePinned invokes method stories.togglePinned#9a75a1ef returning error if any.
// Pin or unpin one or more stories
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/stories.togglePinned for reference.
func (c *Client) StoriesTogglePinned(ctx context.Context, request *StoriesTogglePinnedRequest) ([]int, error) {
	var result IntVector

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return []int(result.Elems), nil
}
