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

// StoryAlbum represents TL type `storyAlbum#9325705a`.
// Represents a story album »¹.
//
// Links:
//  1. https://core.telegram.org/api/stories#story-albums
//
// See https://core.telegram.org/constructor/storyAlbum for reference.
type StoryAlbum struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// ID of the album.
	AlbumID int
	// Name of the album.
	Title string
	// Photo from the first story of the album, if it's a photo.
	//
	// Use SetIconPhoto and GetIconPhoto helpers.
	IconPhoto PhotoClass
	// Video from the first story of the album, if it's a video.
	//
	// Use SetIconVideo and GetIconVideo helpers.
	IconVideo DocumentClass
}

// StoryAlbumTypeID is TL type id of StoryAlbum.
const StoryAlbumTypeID = 0x9325705a

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

func (s *StoryAlbum) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.AlbumID == 0) {
		return false
	}
	if !(s.Title == "") {
		return false
	}
	if !(s.IconPhoto == nil) {
		return false
	}
	if !(s.IconVideo == nil) {
		return false
	}

	return true
}

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

// FillFrom fills StoryAlbum from given interface.
func (s *StoryAlbum) FillFrom(from interface {
	GetAlbumID() (value int)
	GetTitle() (value string)
	GetIconPhoto() (value PhotoClass, ok bool)
	GetIconVideo() (value DocumentClass, ok bool)
}) {
	s.AlbumID = from.GetAlbumID()
	s.Title = from.GetTitle()
	if val, ok := from.GetIconPhoto(); ok {
		s.IconPhoto = val
	}

	if val, ok := from.GetIconVideo(); ok {
		s.IconVideo = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*StoryAlbum) TypeName() string {
	return "storyAlbum"
}

// TypeInfo returns info about TL type.
func (s *StoryAlbum) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storyAlbum",
		ID:   StoryAlbumTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "AlbumID",
			SchemaName: "album_id",
		},
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "IconPhoto",
			SchemaName: "icon_photo",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "IconVideo",
			SchemaName: "icon_video",
			Null:       !s.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StoryAlbum) SetFlags() {
	if !(s.IconPhoto == nil) {
		s.Flags.Set(0)
	}
	if !(s.IconVideo == nil) {
		s.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (s *StoryAlbum) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyAlbum#9325705a as nil")
	}
	b.PutID(StoryAlbumTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoryAlbum) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyAlbum#9325705a as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode storyAlbum#9325705a: field flags: %w", err)
	}
	b.PutInt(s.AlbumID)
	b.PutString(s.Title)
	if s.Flags.Has(0) {
		if s.IconPhoto == nil {
			return fmt.Errorf("unable to encode storyAlbum#9325705a: field icon_photo is nil")
		}
		if err := s.IconPhoto.Encode(b); err != nil {
			return fmt.Errorf("unable to encode storyAlbum#9325705a: field icon_photo: %w", err)
		}
	}
	if s.Flags.Has(1) {
		if s.IconVideo == nil {
			return fmt.Errorf("unable to encode storyAlbum#9325705a: field icon_video is nil")
		}
		if err := s.IconVideo.Encode(b); err != nil {
			return fmt.Errorf("unable to encode storyAlbum#9325705a: field icon_video: %w", err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StoryAlbum) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storyAlbum#9325705a to nil")
	}
	if err := b.ConsumeID(StoryAlbumTypeID); err != nil {
		return fmt.Errorf("unable to decode storyAlbum#9325705a: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StoryAlbum) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storyAlbum#9325705a to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode storyAlbum#9325705a: field flags: %w", err)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode storyAlbum#9325705a: field album_id: %w", err)
		}
		s.AlbumID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode storyAlbum#9325705a: field title: %w", err)
		}
		s.Title = value
	}
	if s.Flags.Has(0) {
		value, err := DecodePhoto(b)
		if err != nil {
			return fmt.Errorf("unable to decode storyAlbum#9325705a: field icon_photo: %w", err)
		}
		s.IconPhoto = value
	}
	if s.Flags.Has(1) {
		value, err := DecodeDocument(b)
		if err != nil {
			return fmt.Errorf("unable to decode storyAlbum#9325705a: field icon_video: %w", err)
		}
		s.IconVideo = value
	}
	return nil
}

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

// GetTitle returns value of Title field.
func (s *StoryAlbum) GetTitle() (value string) {
	if s == nil {
		return
	}
	return s.Title
}

// SetIconPhoto sets value of IconPhoto conditional field.
func (s *StoryAlbum) SetIconPhoto(value PhotoClass) {
	s.Flags.Set(0)
	s.IconPhoto = value
}

// GetIconPhoto returns value of IconPhoto conditional field and
// boolean which is true if field was set.
func (s *StoryAlbum) GetIconPhoto() (value PhotoClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.IconPhoto, true
}

// SetIconVideo sets value of IconVideo conditional field.
func (s *StoryAlbum) SetIconVideo(value DocumentClass) {
	s.Flags.Set(1)
	s.IconVideo = value
}

// GetIconVideo returns value of IconVideo conditional field and
// boolean which is true if field was set.
func (s *StoryAlbum) GetIconVideo() (value DocumentClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.IconVideo, true
}

// GetIconPhotoAsNotEmpty returns mapped value of IconPhoto conditional field and
// boolean which is true if field was set.
func (s *StoryAlbum) GetIconPhotoAsNotEmpty() (*Photo, bool) {
	if value, ok := s.GetIconPhoto(); ok {
		return value.AsNotEmpty()
	}
	return nil, false
}

// GetIconVideoAsNotEmpty returns mapped value of IconVideo conditional field and
// boolean which is true if field was set.
func (s *StoryAlbum) GetIconVideoAsNotEmpty() (*Document, bool) {
	if value, ok := s.GetIconVideo(); ok {
		return value.AsNotEmpty()
	}
	return nil, false
}
