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

// StoriesGetAlbumsRequest represents TL type `stories.getAlbums#25b3eac7`.
// Get story albums¹ created by a peer.
//
// Links:
//  1. https://core.telegram.org/api/stories#story-albums
//
// See https://core.telegram.org/method/stories.getAlbums for reference.
type StoriesGetAlbumsRequest struct {
	// The peer.
	Peer InputPeerClass
	// The hash from a previously returned stories.albums¹, to avoid returning any results
	// if they haven't changed.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/stories.albums
	Hash int64
}

// StoriesGetAlbumsRequestTypeID is TL type id of StoriesGetAlbumsRequest.
const StoriesGetAlbumsRequestTypeID = 0x25b3eac7

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

func (g *StoriesGetAlbumsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesGetAlbumsRequest from given interface.
func (g *StoriesGetAlbumsRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetHash() (value int64)
}) {
	g.Peer = from.GetPeer()
	g.Hash = from.GetHash()
}

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

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

// TypeInfo returns info about TL type.
func (g *StoriesGetAlbumsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.getAlbums",
		ID:   StoriesGetAlbumsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *StoriesGetAlbumsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stories.getAlbums#25b3eac7 as nil")
	}
	b.PutID(StoriesGetAlbumsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *StoriesGetAlbumsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stories.getAlbums#25b3eac7 as nil")
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode stories.getAlbums#25b3eac7: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.getAlbums#25b3eac7: field peer: %w", err)
	}
	b.PutLong(g.Hash)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *StoriesGetAlbumsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stories.getAlbums#25b3eac7 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stories.getAlbums#25b3eac7: field peer: %w", err)
		}
		g.Peer = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode stories.getAlbums#25b3eac7: field hash: %w", err)
		}
		g.Hash = value
	}
	return nil
}

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

// GetHash returns value of Hash field.
func (g *StoriesGetAlbumsRequest) GetHash() (value int64) {
	if g == nil {
		return
	}
	return g.Hash
}

// StoriesGetAlbums invokes method stories.getAlbums#25b3eac7 returning error if any.
// Get story albums¹ created by a peer.
//
// 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.getAlbums for reference.
func (c *Client) StoriesGetAlbums(ctx context.Context, request *StoriesGetAlbumsRequest) (StoriesAlbumsClass, error) {
	var result StoriesAlbumsBox

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