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

// StoryInteractionInfo represents TL type `storyInteractionInfo#e1557fc7`.
type StoryInteractionInfo struct {
	// Number of times the story was viewed
	ViewCount int32
	// Number of times the story was forwarded; 0 if none or unknown
	ForwardCount int32
	// Number of reactions added to the story; 0 if none or unknown
	ReactionCount int32
	// Identifiers of at most 3 recent viewers of the story
	RecentViewerUserIDs []int64
}

// StoryInteractionInfoTypeID is TL type id of StoryInteractionInfo.
const StoryInteractionInfoTypeID = 0xe1557fc7

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

func (s *StoryInteractionInfo) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ViewCount == 0) {
		return false
	}
	if !(s.ForwardCount == 0) {
		return false
	}
	if !(s.ReactionCount == 0) {
		return false
	}
	if !(s.RecentViewerUserIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StoryInteractionInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storyInteractionInfo",
		ID:   StoryInteractionInfoTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ViewCount",
			SchemaName: "view_count",
		},
		{
			Name:       "ForwardCount",
			SchemaName: "forward_count",
		},
		{
			Name:       "ReactionCount",
			SchemaName: "reaction_count",
		},
		{
			Name:       "RecentViewerUserIDs",
			SchemaName: "recent_viewer_user_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StoryInteractionInfo) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionInfo#e1557fc7 as nil")
	}
	b.PutID(StoryInteractionInfoTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoryInteractionInfo) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionInfo#e1557fc7 as nil")
	}
	b.PutInt32(s.ViewCount)
	b.PutInt32(s.ForwardCount)
	b.PutInt32(s.ReactionCount)
	b.PutInt(len(s.RecentViewerUserIDs))
	for _, v := range s.RecentViewerUserIDs {
		b.PutInt53(v)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StoryInteractionInfo) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storyInteractionInfo#e1557fc7 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode storyInteractionInfo#e1557fc7: field view_count: %w", err)
		}
		s.ViewCount = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode storyInteractionInfo#e1557fc7: field forward_count: %w", err)
		}
		s.ForwardCount = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode storyInteractionInfo#e1557fc7: field reaction_count: %w", err)
		}
		s.ReactionCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode storyInteractionInfo#e1557fc7: field recent_viewer_user_ids: %w", err)
		}

		if headerLen > 0 {
			s.RecentViewerUserIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode storyInteractionInfo#e1557fc7: field recent_viewer_user_ids: %w", err)
			}
			s.RecentViewerUserIDs = append(s.RecentViewerUserIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StoryInteractionInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionInfo#e1557fc7 as nil")
	}
	b.ObjStart()
	b.PutID("storyInteractionInfo")
	b.Comma()
	b.FieldStart("view_count")
	b.PutInt32(s.ViewCount)
	b.Comma()
	b.FieldStart("forward_count")
	b.PutInt32(s.ForwardCount)
	b.Comma()
	b.FieldStart("reaction_count")
	b.PutInt32(s.ReactionCount)
	b.Comma()
	b.FieldStart("recent_viewer_user_ids")
	b.ArrStart()
	for _, v := range s.RecentViewerUserIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("storyInteractionInfo"); err != nil {
				return fmt.Errorf("unable to decode storyInteractionInfo#e1557fc7: %w", err)
			}
		case "view_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode storyInteractionInfo#e1557fc7: field view_count: %w", err)
			}
			s.ViewCount = value
		case "forward_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode storyInteractionInfo#e1557fc7: field forward_count: %w", err)
			}
			s.ForwardCount = value
		case "reaction_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode storyInteractionInfo#e1557fc7: field reaction_count: %w", err)
			}
			s.ReactionCount = value
		case "recent_viewer_user_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode storyInteractionInfo#e1557fc7: field recent_viewer_user_ids: %w", err)
				}
				s.RecentViewerUserIDs = append(s.RecentViewerUserIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode storyInteractionInfo#e1557fc7: field recent_viewer_user_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetViewCount returns value of ViewCount field.
func (s *StoryInteractionInfo) GetViewCount() (value int32) {
	if s == nil {
		return
	}
	return s.ViewCount
}

// GetForwardCount returns value of ForwardCount field.
func (s *StoryInteractionInfo) GetForwardCount() (value int32) {
	if s == nil {
		return
	}
	return s.ForwardCount
}

// GetReactionCount returns value of ReactionCount field.
func (s *StoryInteractionInfo) GetReactionCount() (value int32) {
	if s == nil {
		return
	}
	return s.ReactionCount
}

// GetRecentViewerUserIDs returns value of RecentViewerUserIDs field.
func (s *StoryInteractionInfo) GetRecentViewerUserIDs() (value []int64) {
	if s == nil {
		return
	}
	return s.RecentViewerUserIDs
}
