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

// StoriesStoryReactionsList represents TL type `stories.storyReactionsList#aa5f789c`.
// List of peers that reacted to or intercated with a specific story¹
//
// Links:
//  1. https://core.telegram.org/api/stories
//
// See https://core.telegram.org/constructor/stories.storyReactionsList for reference.
type StoriesStoryReactionsList struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Total number of reactions matching query
	Count int
	// List of peers that reacted to or interacted with a specific story
	Reactions []StoryReactionClass
	// Mentioned chats
	Chats []ChatClass
	// Mentioned users
	Users []UserClass
	// If set, indicates the next offset to use to load more results by invoking stories
	// getStoryReactionsList¹.
	//
	// Links:
	//  1) https://core.telegram.org/method/stories.getStoryReactionsList
	//
	// Use SetNextOffset and GetNextOffset helpers.
	NextOffset string
}

// StoriesStoryReactionsListTypeID is TL type id of StoriesStoryReactionsList.
const StoriesStoryReactionsListTypeID = 0xaa5f789c

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

func (s *StoriesStoryReactionsList) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Count == 0) {
		return false
	}
	if !(s.Reactions == nil) {
		return false
	}
	if !(s.Chats == nil) {
		return false
	}
	if !(s.Users == nil) {
		return false
	}
	if !(s.NextOffset == "") {
		return false
	}

	return true
}

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

// FillFrom fills StoriesStoryReactionsList from given interface.
func (s *StoriesStoryReactionsList) FillFrom(from interface {
	GetCount() (value int)
	GetReactions() (value []StoryReactionClass)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
	GetNextOffset() (value string, ok bool)
}) {
	s.Count = from.GetCount()
	s.Reactions = from.GetReactions()
	s.Chats = from.GetChats()
	s.Users = from.GetUsers()
	if val, ok := from.GetNextOffset(); ok {
		s.NextOffset = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (s *StoriesStoryReactionsList) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.storyReactionsList",
		ID:   StoriesStoryReactionsListTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "Reactions",
			SchemaName: "reactions",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
			Null:       !s.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StoriesStoryReactionsList) SetFlags() {
	if !(s.NextOffset == "") {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *StoriesStoryReactionsList) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode stories.storyReactionsList#aa5f789c as nil")
	}
	b.PutID(StoriesStoryReactionsListTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoriesStoryReactionsList) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode stories.storyReactionsList#aa5f789c as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.storyReactionsList#aa5f789c: field flags: %w", err)
	}
	b.PutInt(s.Count)
	b.PutVectorHeader(len(s.Reactions))
	for idx, v := range s.Reactions {
		if v == nil {
			return fmt.Errorf("unable to encode stories.storyReactionsList#aa5f789c: field reactions element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode stories.storyReactionsList#aa5f789c: field reactions element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Chats))
	for idx, v := range s.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode stories.storyReactionsList#aa5f789c: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode stories.storyReactionsList#aa5f789c: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Users))
	for idx, v := range s.Users {
		if v == nil {
			return fmt.Errorf("unable to encode stories.storyReactionsList#aa5f789c: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode stories.storyReactionsList#aa5f789c: field users element with index %d: %w", idx, err)
		}
	}
	if s.Flags.Has(0) {
		b.PutString(s.NextOffset)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StoriesStoryReactionsList) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode stories.storyReactionsList#aa5f789c to nil")
	}
	if err := b.ConsumeID(StoriesStoryReactionsListTypeID); err != nil {
		return fmt.Errorf("unable to decode stories.storyReactionsList#aa5f789c: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StoriesStoryReactionsList) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode stories.storyReactionsList#aa5f789c to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode stories.storyReactionsList#aa5f789c: field flags: %w", err)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode stories.storyReactionsList#aa5f789c: field count: %w", err)
		}
		s.Count = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.storyReactionsList#aa5f789c: field reactions: %w", err)
		}

		if headerLen > 0 {
			s.Reactions = make([]StoryReactionClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeStoryReaction(b)
			if err != nil {
				return fmt.Errorf("unable to decode stories.storyReactionsList#aa5f789c: field reactions: %w", err)
			}
			s.Reactions = append(s.Reactions, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.storyReactionsList#aa5f789c: field chats: %w", err)
		}

		if headerLen > 0 {
			s.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(b)
			if err != nil {
				return fmt.Errorf("unable to decode stories.storyReactionsList#aa5f789c: field chats: %w", err)
			}
			s.Chats = append(s.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.storyReactionsList#aa5f789c: field users: %w", err)
		}

		if headerLen > 0 {
			s.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode stories.storyReactionsList#aa5f789c: field users: %w", err)
			}
			s.Users = append(s.Users, value)
		}
	}
	if s.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode stories.storyReactionsList#aa5f789c: field next_offset: %w", err)
		}
		s.NextOffset = value
	}
	return nil
}

// GetCount returns value of Count field.
func (s *StoriesStoryReactionsList) GetCount() (value int) {
	if s == nil {
		return
	}
	return s.Count
}

// GetReactions returns value of Reactions field.
func (s *StoriesStoryReactionsList) GetReactions() (value []StoryReactionClass) {
	if s == nil {
		return
	}
	return s.Reactions
}

// GetChats returns value of Chats field.
func (s *StoriesStoryReactionsList) GetChats() (value []ChatClass) {
	if s == nil {
		return
	}
	return s.Chats
}

// GetUsers returns value of Users field.
func (s *StoriesStoryReactionsList) GetUsers() (value []UserClass) {
	if s == nil {
		return
	}
	return s.Users
}

// SetNextOffset sets value of NextOffset conditional field.
func (s *StoriesStoryReactionsList) SetNextOffset(value string) {
	s.Flags.Set(0)
	s.NextOffset = value
}

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

// MapReactions returns field Reactions wrapped in StoryReactionClassArray helper.
func (s *StoriesStoryReactionsList) MapReactions() (value StoryReactionClassArray) {
	return StoryReactionClassArray(s.Reactions)
}

// MapChats returns field Chats wrapped in ChatClassArray helper.
func (s *StoriesStoryReactionsList) MapChats() (value ChatClassArray) {
	return ChatClassArray(s.Chats)
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func (s *StoriesStoryReactionsList) MapUsers() (value UserClassArray) {
	return UserClassArray(s.Users)
}
