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

// StoriesFoundStories represents TL type `stories.foundStories#e2de7737`.
// Stories found using global story search »¹.
//
// Links:
//  1. https://core.telegram.org/api/stories#searching-stories
//
// See https://core.telegram.org/constructor/stories.foundStories for reference.
type StoriesFoundStories struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Total number of results found for the query.
	Count int
	// Matching stories.
	Stories []FoundStory
	// Offset used to fetch the next page, if not set this is the final page.
	//
	// Use SetNextOffset and GetNextOffset helpers.
	NextOffset string
	// Mentioned chats
	Chats []ChatClass
	// Mentioned users
	Users []UserClass
}

// StoriesFoundStoriesTypeID is TL type id of StoriesFoundStories.
const StoriesFoundStoriesTypeID = 0xe2de7737

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

func (f *StoriesFoundStories) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Flags.Zero()) {
		return false
	}
	if !(f.Count == 0) {
		return false
	}
	if !(f.Stories == nil) {
		return false
	}
	if !(f.NextOffset == "") {
		return false
	}
	if !(f.Chats == nil) {
		return false
	}
	if !(f.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesFoundStories from given interface.
func (f *StoriesFoundStories) FillFrom(from interface {
	GetCount() (value int)
	GetStories() (value []FoundStory)
	GetNextOffset() (value string, ok bool)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	f.Count = from.GetCount()
	f.Stories = from.GetStories()
	if val, ok := from.GetNextOffset(); ok {
		f.NextOffset = val
	}

	f.Chats = from.GetChats()
	f.Users = from.GetUsers()
}

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

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

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

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

// Encode implements bin.Encoder.
func (f *StoriesFoundStories) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode stories.foundStories#e2de7737 as nil")
	}
	b.PutID(StoriesFoundStoriesTypeID)
	return f.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (f *StoriesFoundStories) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode stories.foundStories#e2de7737 to nil")
	}
	if err := b.ConsumeID(StoriesFoundStoriesTypeID); err != nil {
		return fmt.Errorf("unable to decode stories.foundStories#e2de7737: %w", err)
	}
	return f.DecodeBare(b)
}

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

		if headerLen > 0 {
			f.Stories = make([]FoundStory, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value FoundStory
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode stories.foundStories#e2de7737: field stories: %w", err)
			}
			f.Stories = append(f.Stories, value)
		}
	}
	if f.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode stories.foundStories#e2de7737: field next_offset: %w", err)
		}
		f.NextOffset = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.foundStories#e2de7737: field chats: %w", err)
		}

		if headerLen > 0 {
			f.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.foundStories#e2de7737: field chats: %w", err)
			}
			f.Chats = append(f.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.foundStories#e2de7737: field users: %w", err)
		}

		if headerLen > 0 {
			f.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.foundStories#e2de7737: field users: %w", err)
			}
			f.Users = append(f.Users, value)
		}
	}
	return nil
}

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

// GetStories returns value of Stories field.
func (f *StoriesFoundStories) GetStories() (value []FoundStory) {
	if f == nil {
		return
	}
	return f.Stories
}

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

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

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

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

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

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