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

// StoriesPeerStories represents TL type `stories.peerStories#cae68768`.
// Active story list¹ of a specific peer.
//
// Links:
//  1. https://core.telegram.org/api/stories#watching-stories
//
// See https://core.telegram.org/constructor/stories.peerStories for reference.
type StoriesPeerStories struct {
	// Stories
	Stories PeerStories
	// Mentioned chats
	Chats []ChatClass
	// Mentioned users
	Users []UserClass
}

// StoriesPeerStoriesTypeID is TL type id of StoriesPeerStories.
const StoriesPeerStoriesTypeID = 0xcae68768

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

func (p *StoriesPeerStories) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Stories.Zero()) {
		return false
	}
	if !(p.Chats == nil) {
		return false
	}
	if !(p.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesPeerStories from given interface.
func (p *StoriesPeerStories) FillFrom(from interface {
	GetStories() (value PeerStories)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	p.Stories = from.GetStories()
	p.Chats = from.GetChats()
	p.Users = from.GetUsers()
}

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

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

// TypeInfo returns info about TL type.
func (p *StoriesPeerStories) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.peerStories",
		ID:   StoriesPeerStoriesTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Stories",
			SchemaName: "stories",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *StoriesPeerStories) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode stories.peerStories#cae68768 as nil")
	}
	b.PutID(StoriesPeerStoriesTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *StoriesPeerStories) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode stories.peerStories#cae68768 as nil")
	}
	if err := p.Stories.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.peerStories#cae68768: field stories: %w", err)
	}
	b.PutVectorHeader(len(p.Chats))
	for idx, v := range p.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode stories.peerStories#cae68768: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode stories.peerStories#cae68768: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(p.Users))
	for idx, v := range p.Users {
		if v == nil {
			return fmt.Errorf("unable to encode stories.peerStories#cae68768: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode stories.peerStories#cae68768: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *StoriesPeerStories) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode stories.peerStories#cae68768 to nil")
	}
	{
		if err := p.Stories.Decode(b); err != nil {
			return fmt.Errorf("unable to decode stories.peerStories#cae68768: field stories: %w", err)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.peerStories#cae68768: field chats: %w", err)
		}

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

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

// GetStories returns value of Stories field.
func (p *StoriesPeerStories) GetStories() (value PeerStories) {
	if p == nil {
		return
	}
	return p.Stories
}

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

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

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

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