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

// ChatActiveStories represents TL type `chatActiveStories#d923d773`.
type ChatActiveStories struct {
	// Identifier of the chat that posted the stories
	ChatID int64
	// Identifier of the story list in which the stories are shown; may be null if the
	// stories aren't shown in a story list
	List StoryListClass
	// A parameter used to determine order of the stories in the story list; 0 if the stories
	// doesn't need to be shown in the story list. Stories must be sorted by the pair (order,
	// story_poster_chat_id) in descending order
	Order int64
	// Identifier of the last read active story
	MaxReadStoryID int32
	// Basic information about the stories; use getStory to get full information about the
	// stories. The stories are in chronological order (i.e., in order of increasing story
	// identifiers)
	Stories []StoryInfo
}

// ChatActiveStoriesTypeID is TL type id of ChatActiveStories.
const ChatActiveStoriesTypeID = 0xd923d773

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

func (c *ChatActiveStories) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.ChatID == 0) {
		return false
	}
	if !(c.List == nil) {
		return false
	}
	if !(c.Order == 0) {
		return false
	}
	if !(c.MaxReadStoryID == 0) {
		return false
	}
	if !(c.Stories == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatActiveStories) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatActiveStories",
		ID:   ChatActiveStoriesTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "List",
			SchemaName: "list",
		},
		{
			Name:       "Order",
			SchemaName: "order",
		},
		{
			Name:       "MaxReadStoryID",
			SchemaName: "max_read_story_id",
		},
		{
			Name:       "Stories",
			SchemaName: "stories",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatActiveStories) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatActiveStories#d923d773 as nil")
	}
	b.PutID(ChatActiveStoriesTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatActiveStories) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatActiveStories#d923d773 as nil")
	}
	b.PutInt53(c.ChatID)
	if c.List == nil {
		return fmt.Errorf("unable to encode chatActiveStories#d923d773: field list is nil")
	}
	if err := c.List.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatActiveStories#d923d773: field list: %w", err)
	}
	b.PutInt53(c.Order)
	b.PutInt32(c.MaxReadStoryID)
	b.PutInt(len(c.Stories))
	for idx, v := range c.Stories {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare chatActiveStories#d923d773: field stories element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatActiveStories) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatActiveStories#d923d773 to nil")
	}
	if err := b.ConsumeID(ChatActiveStoriesTypeID); err != nil {
		return fmt.Errorf("unable to decode chatActiveStories#d923d773: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatActiveStories) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatActiveStories#d923d773 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode chatActiveStories#d923d773: field chat_id: %w", err)
		}
		c.ChatID = value
	}
	{
		value, err := DecodeStoryList(b)
		if err != nil {
			return fmt.Errorf("unable to decode chatActiveStories#d923d773: field list: %w", err)
		}
		c.List = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode chatActiveStories#d923d773: field order: %w", err)
		}
		c.Order = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chatActiveStories#d923d773: field max_read_story_id: %w", err)
		}
		c.MaxReadStoryID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chatActiveStories#d923d773: field stories: %w", err)
		}

		if headerLen > 0 {
			c.Stories = make([]StoryInfo, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StoryInfo
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare chatActiveStories#d923d773: field stories: %w", err)
			}
			c.Stories = append(c.Stories, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatActiveStories) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatActiveStories#d923d773 as nil")
	}
	b.ObjStart()
	b.PutID("chatActiveStories")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(c.ChatID)
	b.Comma()
	b.FieldStart("list")
	if c.List == nil {
		return fmt.Errorf("unable to encode chatActiveStories#d923d773: field list is nil")
	}
	if err := c.List.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatActiveStories#d923d773: field list: %w", err)
	}
	b.Comma()
	b.FieldStart("order")
	b.PutInt53(c.Order)
	b.Comma()
	b.FieldStart("max_read_story_id")
	b.PutInt32(c.MaxReadStoryID)
	b.Comma()
	b.FieldStart("stories")
	b.ArrStart()
	for idx, v := range c.Stories {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode chatActiveStories#d923d773: field stories element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ChatActiveStories) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatActiveStories#d923d773 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatActiveStories"); err != nil {
				return fmt.Errorf("unable to decode chatActiveStories#d923d773: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode chatActiveStories#d923d773: field chat_id: %w", err)
			}
			c.ChatID = value
		case "list":
			value, err := DecodeTDLibJSONStoryList(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatActiveStories#d923d773: field list: %w", err)
			}
			c.List = value
		case "order":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode chatActiveStories#d923d773: field order: %w", err)
			}
			c.Order = value
		case "max_read_story_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatActiveStories#d923d773: field max_read_story_id: %w", err)
			}
			c.MaxReadStoryID = value
		case "stories":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value StoryInfo
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode chatActiveStories#d923d773: field stories: %w", err)
				}
				c.Stories = append(c.Stories, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatActiveStories#d923d773: field stories: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (c *ChatActiveStories) GetChatID() (value int64) {
	if c == nil {
		return
	}
	return c.ChatID
}

// GetList returns value of List field.
func (c *ChatActiveStories) GetList() (value StoryListClass) {
	if c == nil {
		return
	}
	return c.List
}

// GetOrder returns value of Order field.
func (c *ChatActiveStories) GetOrder() (value int64) {
	if c == nil {
		return
	}
	return c.Order
}

// GetMaxReadStoryID returns value of MaxReadStoryID field.
func (c *ChatActiveStories) GetMaxReadStoryID() (value int32) {
	if c == nil {
		return
	}
	return c.MaxReadStoryID
}

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