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

// Stories represents TL type `stories#b88ff8ff`.
type Stories struct {
	// Approximate total number of stories found
	TotalCount int32
	// The list of stories
	Stories []Story
	// Identifiers of the pinned stories; returned only in getChatPostedToChatPageStories
	// with from_story_id == 0
	PinnedStoryIDs []int32
}

// StoriesTypeID is TL type id of Stories.
const StoriesTypeID = 0xb88ff8ff

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

func (s *Stories) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.TotalCount == 0) {
		return false
	}
	if !(s.Stories == nil) {
		return false
	}
	if !(s.PinnedStoryIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *Stories) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories",
		ID:   StoriesTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Stories",
			SchemaName: "stories",
		},
		{
			Name:       "PinnedStoryIDs",
			SchemaName: "pinned_story_ids",
		},
	}
	return typ
}

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

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

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

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

		if headerLen > 0 {
			s.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 stories#b88ff8ff: field stories: %w", err)
			}
			s.Stories = append(s.Stories, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode stories#b88ff8ff: field pinned_story_ids: %w", err)
		}

		if headerLen > 0 {
			s.PinnedStoryIDs = make([]int32, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode stories#b88ff8ff: field pinned_story_ids: %w", err)
			}
			s.PinnedStoryIDs = append(s.PinnedStoryIDs, value)
		}
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *Stories) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode stories#b88ff8ff to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("stories"); err != nil {
				return fmt.Errorf("unable to decode stories#b88ff8ff: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode stories#b88ff8ff: field total_count: %w", err)
			}
			s.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 stories#b88ff8ff: field stories: %w", err)
				}
				s.Stories = append(s.Stories, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode stories#b88ff8ff: field stories: %w", err)
			}
		case "pinned_story_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int32()
				if err != nil {
					return fmt.Errorf("unable to decode stories#b88ff8ff: field pinned_story_ids: %w", err)
				}
				s.PinnedStoryIDs = append(s.PinnedStoryIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode stories#b88ff8ff: field pinned_story_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

// GetPinnedStoryIDs returns value of PinnedStoryIDs field.
func (s *Stories) GetPinnedStoryIDs() (value []int32) {
	if s == nil {
		return
	}
	return s.PinnedStoryIDs
}
