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

// StoriesGetStoriesArchiveRequest represents TL type `stories.getStoriesArchive#b4352016`.
// Fetch the story archive »¹ of a peer we control.
//
// Links:
//  1. https://core.telegram.org/api/stories#pinned-or-archived-stories
//
// See https://core.telegram.org/method/stories.getStoriesArchive for reference.
type StoriesGetStoriesArchiveRequest struct {
	// Peer whose archived stories should be fetched
	Peer InputPeerClass
	// Offsets for pagination, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	OffsetID int
	// Maximum number of results to return, see pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	Limit int
}

// StoriesGetStoriesArchiveRequestTypeID is TL type id of StoriesGetStoriesArchiveRequest.
const StoriesGetStoriesArchiveRequestTypeID = 0xb4352016

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

func (g *StoriesGetStoriesArchiveRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.OffsetID == 0) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesGetStoriesArchiveRequest from given interface.
func (g *StoriesGetStoriesArchiveRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetOffsetID() (value int)
	GetLimit() (value int)
}) {
	g.Peer = from.GetPeer()
	g.OffsetID = from.GetOffsetID()
	g.Limit = from.GetLimit()
}

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

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

// TypeInfo returns info about TL type.
func (g *StoriesGetStoriesArchiveRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.getStoriesArchive",
		ID:   StoriesGetStoriesArchiveRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "OffsetID",
			SchemaName: "offset_id",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *StoriesGetStoriesArchiveRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stories.getStoriesArchive#b4352016 as nil")
	}
	b.PutID(StoriesGetStoriesArchiveRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *StoriesGetStoriesArchiveRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stories.getStoriesArchive#b4352016 as nil")
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode stories.getStoriesArchive#b4352016: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.getStoriesArchive#b4352016: field peer: %w", err)
	}
	b.PutInt(g.OffsetID)
	b.PutInt(g.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *StoriesGetStoriesArchiveRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stories.getStoriesArchive#b4352016 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stories.getStoriesArchive#b4352016: field peer: %w", err)
		}
		g.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode stories.getStoriesArchive#b4352016: field offset_id: %w", err)
		}
		g.OffsetID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode stories.getStoriesArchive#b4352016: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

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

// GetOffsetID returns value of OffsetID field.
func (g *StoriesGetStoriesArchiveRequest) GetOffsetID() (value int) {
	if g == nil {
		return
	}
	return g.OffsetID
}

// GetLimit returns value of Limit field.
func (g *StoriesGetStoriesArchiveRequest) GetLimit() (value int) {
	if g == nil {
		return
	}
	return g.Limit
}

// StoriesGetStoriesArchive invokes method stories.getStoriesArchive#b4352016 returning error if any.
// Fetch the story archive »¹ of a peer we control.
//
// Links:
//  1. https://core.telegram.org/api/stories#pinned-or-archived-stories
//
// Possible errors:
//
//	400 CHAT_ADMIN_REQUIRED: You must be an admin in this chat to do this.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/stories.getStoriesArchive for reference.
func (c *Client) StoriesGetStoriesArchive(ctx context.Context, request *StoriesGetStoriesArchiveRequest) (*StoriesStories, error) {
	var result StoriesStories

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