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

// StoriesUpdateAlbumRequest represents TL type `stories.updateAlbum#5e5259b6`.
// Rename a story albums »¹, or add, delete or reorder stories in it.
//
// Links:
//  1. https://core.telegram.org/api/stories#story-albums
//
// See https://core.telegram.org/method/stories.updateAlbum for reference.
type StoriesUpdateAlbumRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Peer where the album is posted.
	Peer InputPeerClass
	// Album ID.
	AlbumID int
	// New album title.
	//
	// Use SetTitle and GetTitle helpers.
	Title string
	// If set, deletes the specified stories from the album.
	//
	// Use SetDeleteStories and GetDeleteStories helpers.
	DeleteStories []int
	// If set, adds the specified stories to the album.
	//
	// Use SetAddStories and GetAddStories helpers.
	AddStories []int
	// If set, reorders the stories in the album by their IDs.
	//
	// Use SetOrder and GetOrder helpers.
	Order []int
}

// StoriesUpdateAlbumRequestTypeID is TL type id of StoriesUpdateAlbumRequest.
const StoriesUpdateAlbumRequestTypeID = 0x5e5259b6

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

func (u *StoriesUpdateAlbumRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Flags.Zero()) {
		return false
	}
	if !(u.Peer == nil) {
		return false
	}
	if !(u.AlbumID == 0) {
		return false
	}
	if !(u.Title == "") {
		return false
	}
	if !(u.DeleteStories == nil) {
		return false
	}
	if !(u.AddStories == nil) {
		return false
	}
	if !(u.Order == nil) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesUpdateAlbumRequest from given interface.
func (u *StoriesUpdateAlbumRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetAlbumID() (value int)
	GetTitle() (value string, ok bool)
	GetDeleteStories() (value []int, ok bool)
	GetAddStories() (value []int, ok bool)
	GetOrder() (value []int, ok bool)
}) {
	u.Peer = from.GetPeer()
	u.AlbumID = from.GetAlbumID()
	if val, ok := from.GetTitle(); ok {
		u.Title = val
	}

	if val, ok := from.GetDeleteStories(); ok {
		u.DeleteStories = val
	}

	if val, ok := from.GetAddStories(); ok {
		u.AddStories = val
	}

	if val, ok := from.GetOrder(); ok {
		u.Order = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (u *StoriesUpdateAlbumRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.updateAlbum",
		ID:   StoriesUpdateAlbumRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "AlbumID",
			SchemaName: "album_id",
		},
		{
			Name:       "Title",
			SchemaName: "title",
			Null:       !u.Flags.Has(0),
		},
		{
			Name:       "DeleteStories",
			SchemaName: "delete_stories",
			Null:       !u.Flags.Has(1),
		},
		{
			Name:       "AddStories",
			SchemaName: "add_stories",
			Null:       !u.Flags.Has(2),
		},
		{
			Name:       "Order",
			SchemaName: "order",
			Null:       !u.Flags.Has(3),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (u *StoriesUpdateAlbumRequest) SetFlags() {
	if !(u.Title == "") {
		u.Flags.Set(0)
	}
	if !(u.DeleteStories == nil) {
		u.Flags.Set(1)
	}
	if !(u.AddStories == nil) {
		u.Flags.Set(2)
	}
	if !(u.Order == nil) {
		u.Flags.Set(3)
	}
}

// Encode implements bin.Encoder.
func (u *StoriesUpdateAlbumRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode stories.updateAlbum#5e5259b6 as nil")
	}
	b.PutID(StoriesUpdateAlbumRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *StoriesUpdateAlbumRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode stories.updateAlbum#5e5259b6 as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.updateAlbum#5e5259b6: field flags: %w", err)
	}
	if u.Peer == nil {
		return fmt.Errorf("unable to encode stories.updateAlbum#5e5259b6: field peer is nil")
	}
	if err := u.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.updateAlbum#5e5259b6: field peer: %w", err)
	}
	b.PutInt(u.AlbumID)
	if u.Flags.Has(0) {
		b.PutString(u.Title)
	}
	if u.Flags.Has(1) {
		b.PutVectorHeader(len(u.DeleteStories))
		for _, v := range u.DeleteStories {
			b.PutInt(v)
		}
	}
	if u.Flags.Has(2) {
		b.PutVectorHeader(len(u.AddStories))
		for _, v := range u.AddStories {
			b.PutInt(v)
		}
	}
	if u.Flags.Has(3) {
		b.PutVectorHeader(len(u.Order))
		for _, v := range u.Order {
			b.PutInt(v)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *StoriesUpdateAlbumRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode stories.updateAlbum#5e5259b6 to nil")
	}
	if err := b.ConsumeID(StoriesUpdateAlbumRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stories.updateAlbum#5e5259b6: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *StoriesUpdateAlbumRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode stories.updateAlbum#5e5259b6 to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode stories.updateAlbum#5e5259b6: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stories.updateAlbum#5e5259b6: field peer: %w", err)
		}
		u.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode stories.updateAlbum#5e5259b6: field album_id: %w", err)
		}
		u.AlbumID = value
	}
	if u.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode stories.updateAlbum#5e5259b6: field title: %w", err)
		}
		u.Title = value
	}
	if u.Flags.Has(1) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.updateAlbum#5e5259b6: field delete_stories: %w", err)
		}

		if headerLen > 0 {
			u.DeleteStories = 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.updateAlbum#5e5259b6: field delete_stories: %w", err)
			}
			u.DeleteStories = append(u.DeleteStories, value)
		}
	}
	if u.Flags.Has(2) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.updateAlbum#5e5259b6: field add_stories: %w", err)
		}

		if headerLen > 0 {
			u.AddStories = 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.updateAlbum#5e5259b6: field add_stories: %w", err)
			}
			u.AddStories = append(u.AddStories, value)
		}
	}
	if u.Flags.Has(3) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.updateAlbum#5e5259b6: field order: %w", err)
		}

		if headerLen > 0 {
			u.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.updateAlbum#5e5259b6: field order: %w", err)
			}
			u.Order = append(u.Order, value)
		}
	}
	return nil
}

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

// GetAlbumID returns value of AlbumID field.
func (u *StoriesUpdateAlbumRequest) GetAlbumID() (value int) {
	if u == nil {
		return
	}
	return u.AlbumID
}

// SetTitle sets value of Title conditional field.
func (u *StoriesUpdateAlbumRequest) SetTitle(value string) {
	u.Flags.Set(0)
	u.Title = value
}

// GetTitle returns value of Title conditional field and
// boolean which is true if field was set.
func (u *StoriesUpdateAlbumRequest) GetTitle() (value string, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(0) {
		return value, false
	}
	return u.Title, true
}

// SetDeleteStories sets value of DeleteStories conditional field.
func (u *StoriesUpdateAlbumRequest) SetDeleteStories(value []int) {
	u.Flags.Set(1)
	u.DeleteStories = value
}

// GetDeleteStories returns value of DeleteStories conditional field and
// boolean which is true if field was set.
func (u *StoriesUpdateAlbumRequest) GetDeleteStories() (value []int, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(1) {
		return value, false
	}
	return u.DeleteStories, true
}

// SetAddStories sets value of AddStories conditional field.
func (u *StoriesUpdateAlbumRequest) SetAddStories(value []int) {
	u.Flags.Set(2)
	u.AddStories = value
}

// GetAddStories returns value of AddStories conditional field and
// boolean which is true if field was set.
func (u *StoriesUpdateAlbumRequest) GetAddStories() (value []int, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(2) {
		return value, false
	}
	return u.AddStories, true
}

// SetOrder sets value of Order conditional field.
func (u *StoriesUpdateAlbumRequest) SetOrder(value []int) {
	u.Flags.Set(3)
	u.Order = value
}

// GetOrder returns value of Order conditional field and
// boolean which is true if field was set.
func (u *StoriesUpdateAlbumRequest) GetOrder() (value []int, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(3) {
		return value, false
	}
	return u.Order, true
}

// StoriesUpdateAlbum invokes method stories.updateAlbum#5e5259b6 returning error if any.
// Rename a story albums »¹, or add, delete or reorder stories in it.
//
// 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.updateAlbum for reference.
func (c *Client) StoriesUpdateAlbum(ctx context.Context, request *StoriesUpdateAlbumRequest) (*StoryAlbum, error) {
	var result StoryAlbum

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