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

// StoryInfo represents TL type `storyInfo#8997c352`.
type StoryInfo struct {
	// Unique story identifier among stories of the chat
	StoryID int32
	// Point in time (Unix timestamp) when the story was published
	Date int32
	// True, if the story is available only to close friends
	IsForCloseFriends bool
}

// StoryInfoTypeID is TL type id of StoryInfo.
const StoryInfoTypeID = 0x8997c352

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

func (s *StoryInfo) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.StoryID == 0) {
		return false
	}
	if !(s.Date == 0) {
		return false
	}
	if !(s.IsForCloseFriends == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StoryInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storyInfo",
		ID:   StoryInfoTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StoryID",
			SchemaName: "story_id",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "IsForCloseFriends",
			SchemaName: "is_for_close_friends",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StoryInfo) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInfo#8997c352 as nil")
	}
	b.PutID(StoryInfoTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoryInfo) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInfo#8997c352 as nil")
	}
	b.PutInt32(s.StoryID)
	b.PutInt32(s.Date)
	b.PutBool(s.IsForCloseFriends)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StoryInfo) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storyInfo#8997c352 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode storyInfo#8997c352: field story_id: %w", err)
		}
		s.StoryID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode storyInfo#8997c352: field date: %w", err)
		}
		s.Date = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode storyInfo#8997c352: field is_for_close_friends: %w", err)
		}
		s.IsForCloseFriends = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StoryInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInfo#8997c352 as nil")
	}
	b.ObjStart()
	b.PutID("storyInfo")
	b.Comma()
	b.FieldStart("story_id")
	b.PutInt32(s.StoryID)
	b.Comma()
	b.FieldStart("date")
	b.PutInt32(s.Date)
	b.Comma()
	b.FieldStart("is_for_close_friends")
	b.PutBool(s.IsForCloseFriends)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("storyInfo"); err != nil {
				return fmt.Errorf("unable to decode storyInfo#8997c352: %w", err)
			}
		case "story_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode storyInfo#8997c352: field story_id: %w", err)
			}
			s.StoryID = value
		case "date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode storyInfo#8997c352: field date: %w", err)
			}
			s.Date = value
		case "is_for_close_friends":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode storyInfo#8997c352: field is_for_close_friends: %w", err)
			}
			s.IsForCloseFriends = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetStoryID returns value of StoryID field.
func (s *StoryInfo) GetStoryID() (value int32) {
	if s == nil {
		return
	}
	return s.StoryID
}

// GetDate returns value of Date field.
func (s *StoryInfo) GetDate() (value int32) {
	if s == nil {
		return
	}
	return s.Date
}

// GetIsForCloseFriends returns value of IsForCloseFriends field.
func (s *StoryInfo) GetIsForCloseFriends() (value bool) {
	if s == nil {
		return
	}
	return s.IsForCloseFriends
}
