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

// StoriesStoryViews represents TL type `stories.storyViews#de9eed1d`.
// Reaction and view counters for a list of stories¹
//
// Links:
//  1. https://core.telegram.org/api/stories
//
// See https://core.telegram.org/constructor/stories.storyViews for reference.
type StoriesStoryViews struct {
	// View date and reaction information of multiple stories
	Views []StoryViews
	// Mentioned users
	Users []UserClass
}

// StoriesStoryViewsTypeID is TL type id of StoriesStoryViews.
const StoriesStoryViewsTypeID = 0xde9eed1d

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

func (s *StoriesStoryViews) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Views == nil) {
		return false
	}
	if !(s.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesStoryViews from given interface.
func (s *StoriesStoryViews) FillFrom(from interface {
	GetViews() (value []StoryViews)
	GetUsers() (value []UserClass)
}) {
	s.Views = from.GetViews()
	s.Users = from.GetUsers()
}

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

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

// TypeInfo returns info about TL type.
func (s *StoriesStoryViews) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.storyViews",
		ID:   StoriesStoryViewsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Views",
			SchemaName: "views",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

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

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

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

// DecodeBare implements bin.BareDecoder.
func (s *StoriesStoryViews) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode stories.storyViews#de9eed1d to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.storyViews#de9eed1d: field views: %w", err)
		}

		if headerLen > 0 {
			s.Views = make([]StoryViews, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StoryViews
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode stories.storyViews#de9eed1d: field views: %w", err)
			}
			s.Views = append(s.Views, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.storyViews#de9eed1d: field users: %w", err)
		}

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

// GetViews returns value of Views field.
func (s *StoriesStoryViews) GetViews() (value []StoryViews) {
	if s == nil {
		return
	}
	return s.Views
}

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

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