// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// FoundStories represents TL type `foundStories#854b067c`.
type FoundStories struct {
	// Approximate total number of stories found
	TotalCount int32
	// List of stories
	Stories []Story
	// The offset for the next request. If empty, then there are no more results
	NextOffset string
}

// FoundStoriesTypeID is TL type id of FoundStories.
const FoundStoriesTypeID = 0x854b067c

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

func (f *FoundStories) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.TotalCount == 0) {
		return false
	}
	if !(f.Stories == nil) {
		return false
	}
	if !(f.NextOffset == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *FoundStories) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "foundStories",
		ID:   FoundStoriesTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Stories",
			SchemaName: "stories",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
		},
	}
	return typ
}

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

// EncodeBare implements bin.BareEncoder.
func (f *FoundStories) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundStories#854b067c as nil")
	}
	b.PutInt32(f.TotalCount)
	b.PutInt(len(f.Stories))
	for idx, v := range f.Stories {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare foundStories#854b067c: field stories element with index %d: %w", idx, err)
		}
	}
	b.PutString(f.NextOffset)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (f *FoundStories) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundStories#854b067c to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode foundStories#854b067c: field total_count: %w", err)
		}
		f.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode foundStories#854b067c: field stories: %w", err)
		}

		if headerLen > 0 {
			f.Stories = make([]Story, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value Story
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare foundStories#854b067c: field stories: %w", err)
			}
			f.Stories = append(f.Stories, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode foundStories#854b067c: field next_offset: %w", err)
		}
		f.NextOffset = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *FoundStories) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode foundStories#854b067c as nil")
	}
	b.ObjStart()
	b.PutID("foundStories")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(f.TotalCount)
	b.Comma()
	b.FieldStart("stories")
	b.ArrStart()
	for idx, v := range f.Stories {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode foundStories#854b067c: field stories element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("next_offset")
	b.PutString(f.NextOffset)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (f *FoundStories) DecodeTDLibJSON(b tdjson.Decoder) error {
	if f == nil {
		return fmt.Errorf("can't decode foundStories#854b067c to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("foundStories"); err != nil {
				return fmt.Errorf("unable to decode foundStories#854b067c: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode foundStories#854b067c: field total_count: %w", err)
			}
			f.TotalCount = value
		case "stories":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value Story
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode foundStories#854b067c: field stories: %w", err)
				}
				f.Stories = append(f.Stories, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode foundStories#854b067c: field stories: %w", err)
			}
		case "next_offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode foundStories#854b067c: field next_offset: %w", err)
			}
			f.NextOffset = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTotalCount returns value of TotalCount field.
func (f *FoundStories) GetTotalCount() (value int32) {
	if f == nil {
		return
	}
	return f.TotalCount
}

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

// GetNextOffset returns value of NextOffset field.
func (f *FoundStories) GetNextOffset() (value string) {
	if f == nil {
		return
	}
	return f.NextOffset
}
