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

// StoryInteractionTypeView represents TL type `storyInteractionTypeView#53e337d0`.
type StoryInteractionTypeView struct {
	// Type of the reaction that was chosen by the viewer; may be null if none
	ChosenReactionType ReactionTypeClass
}

// StoryInteractionTypeViewTypeID is TL type id of StoryInteractionTypeView.
const StoryInteractionTypeViewTypeID = 0x53e337d0

// construct implements constructor of StoryInteractionTypeClass.
func (s StoryInteractionTypeView) construct() StoryInteractionTypeClass { return &s }

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

	_ StoryInteractionTypeClass = &StoryInteractionTypeView{}
)

func (s *StoryInteractionTypeView) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ChosenReactionType == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StoryInteractionTypeView) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storyInteractionTypeView",
		ID:   StoryInteractionTypeViewTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChosenReactionType",
			SchemaName: "chosen_reaction_type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StoryInteractionTypeView) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionTypeView#53e337d0 as nil")
	}
	b.PutID(StoryInteractionTypeViewTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoryInteractionTypeView) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionTypeView#53e337d0 as nil")
	}
	if s.ChosenReactionType == nil {
		return fmt.Errorf("unable to encode storyInteractionTypeView#53e337d0: field chosen_reaction_type is nil")
	}
	if err := s.ChosenReactionType.Encode(b); err != nil {
		return fmt.Errorf("unable to encode storyInteractionTypeView#53e337d0: field chosen_reaction_type: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StoryInteractionTypeView) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storyInteractionTypeView#53e337d0 to nil")
	}
	{
		value, err := DecodeReactionType(b)
		if err != nil {
			return fmt.Errorf("unable to decode storyInteractionTypeView#53e337d0: field chosen_reaction_type: %w", err)
		}
		s.ChosenReactionType = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StoryInteractionTypeView) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionTypeView#53e337d0 as nil")
	}
	b.ObjStart()
	b.PutID("storyInteractionTypeView")
	b.Comma()
	b.FieldStart("chosen_reaction_type")
	if s.ChosenReactionType == nil {
		return fmt.Errorf("unable to encode storyInteractionTypeView#53e337d0: field chosen_reaction_type is nil")
	}
	if err := s.ChosenReactionType.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode storyInteractionTypeView#53e337d0: field chosen_reaction_type: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("storyInteractionTypeView"); err != nil {
				return fmt.Errorf("unable to decode storyInteractionTypeView#53e337d0: %w", err)
			}
		case "chosen_reaction_type":
			value, err := DecodeTDLibJSONReactionType(b)
			if err != nil {
				return fmt.Errorf("unable to decode storyInteractionTypeView#53e337d0: field chosen_reaction_type: %w", err)
			}
			s.ChosenReactionType = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChosenReactionType returns value of ChosenReactionType field.
func (s *StoryInteractionTypeView) GetChosenReactionType() (value ReactionTypeClass) {
	if s == nil {
		return
	}
	return s.ChosenReactionType
}

// StoryInteractionTypeForward represents TL type `storyInteractionTypeForward#27d23cff`.
type StoryInteractionTypeForward struct {
	// The message with story forward
	Message Message
}

// StoryInteractionTypeForwardTypeID is TL type id of StoryInteractionTypeForward.
const StoryInteractionTypeForwardTypeID = 0x27d23cff

// construct implements constructor of StoryInteractionTypeClass.
func (s StoryInteractionTypeForward) construct() StoryInteractionTypeClass { return &s }

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

	_ StoryInteractionTypeClass = &StoryInteractionTypeForward{}
)

func (s *StoryInteractionTypeForward) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Message.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StoryInteractionTypeForward) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storyInteractionTypeForward",
		ID:   StoryInteractionTypeForwardTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Message",
			SchemaName: "message",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StoryInteractionTypeForward) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionTypeForward#27d23cff as nil")
	}
	b.PutID(StoryInteractionTypeForwardTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoryInteractionTypeForward) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionTypeForward#27d23cff as nil")
	}
	if err := s.Message.Encode(b); err != nil {
		return fmt.Errorf("unable to encode storyInteractionTypeForward#27d23cff: field message: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StoryInteractionTypeForward) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storyInteractionTypeForward#27d23cff to nil")
	}
	{
		if err := s.Message.Decode(b); err != nil {
			return fmt.Errorf("unable to decode storyInteractionTypeForward#27d23cff: field message: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StoryInteractionTypeForward) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionTypeForward#27d23cff as nil")
	}
	b.ObjStart()
	b.PutID("storyInteractionTypeForward")
	b.Comma()
	b.FieldStart("message")
	if err := s.Message.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode storyInteractionTypeForward#27d23cff: field message: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("storyInteractionTypeForward"); err != nil {
				return fmt.Errorf("unable to decode storyInteractionTypeForward#27d23cff: %w", err)
			}
		case "message":
			if err := s.Message.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode storyInteractionTypeForward#27d23cff: field message: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetMessage returns value of Message field.
func (s *StoryInteractionTypeForward) GetMessage() (value Message) {
	if s == nil {
		return
	}
	return s.Message
}

// StoryInteractionTypeRepost represents TL type `storyInteractionTypeRepost#c32279c4`.
type StoryInteractionTypeRepost struct {
	// The reposted story
	Story Story
}

// StoryInteractionTypeRepostTypeID is TL type id of StoryInteractionTypeRepost.
const StoryInteractionTypeRepostTypeID = 0xc32279c4

// construct implements constructor of StoryInteractionTypeClass.
func (s StoryInteractionTypeRepost) construct() StoryInteractionTypeClass { return &s }

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

	_ StoryInteractionTypeClass = &StoryInteractionTypeRepost{}
)

func (s *StoryInteractionTypeRepost) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Story.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StoryInteractionTypeRepost) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storyInteractionTypeRepost",
		ID:   StoryInteractionTypeRepostTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Story",
			SchemaName: "story",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StoryInteractionTypeRepost) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionTypeRepost#c32279c4 as nil")
	}
	b.PutID(StoryInteractionTypeRepostTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoryInteractionTypeRepost) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionTypeRepost#c32279c4 as nil")
	}
	if err := s.Story.Encode(b); err != nil {
		return fmt.Errorf("unable to encode storyInteractionTypeRepost#c32279c4: field story: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StoryInteractionTypeRepost) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storyInteractionTypeRepost#c32279c4 to nil")
	}
	{
		if err := s.Story.Decode(b); err != nil {
			return fmt.Errorf("unable to decode storyInteractionTypeRepost#c32279c4: field story: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StoryInteractionTypeRepost) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode storyInteractionTypeRepost#c32279c4 as nil")
	}
	b.ObjStart()
	b.PutID("storyInteractionTypeRepost")
	b.Comma()
	b.FieldStart("story")
	if err := s.Story.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode storyInteractionTypeRepost#c32279c4: field story: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("storyInteractionTypeRepost"); err != nil {
				return fmt.Errorf("unable to decode storyInteractionTypeRepost#c32279c4: %w", err)
			}
		case "story":
			if err := s.Story.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode storyInteractionTypeRepost#c32279c4: field story: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetStory returns value of Story field.
func (s *StoryInteractionTypeRepost) GetStory() (value Story) {
	if s == nil {
		return
	}
	return s.Story
}

// StoryInteractionTypeClassName is schema name of StoryInteractionTypeClass.
const StoryInteractionTypeClassName = "StoryInteractionType"

// StoryInteractionTypeClass represents StoryInteractionType generic type.
//
// Example:
//
//	g, err := tdapi.DecodeStoryInteractionType(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.StoryInteractionTypeView: // storyInteractionTypeView#53e337d0
//	case *tdapi.StoryInteractionTypeForward: // storyInteractionTypeForward#27d23cff
//	case *tdapi.StoryInteractionTypeRepost: // storyInteractionTypeRepost#c32279c4
//	default: panic(v)
//	}
type StoryInteractionTypeClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() StoryInteractionTypeClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodeStoryInteractionType implements binary de-serialization for StoryInteractionTypeClass.
func DecodeStoryInteractionType(buf *bin.Buffer) (StoryInteractionTypeClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case StoryInteractionTypeViewTypeID:
		// Decoding storyInteractionTypeView#53e337d0.
		v := StoryInteractionTypeView{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StoryInteractionTypeClass: %w", err)
		}
		return &v, nil
	case StoryInteractionTypeForwardTypeID:
		// Decoding storyInteractionTypeForward#27d23cff.
		v := StoryInteractionTypeForward{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StoryInteractionTypeClass: %w", err)
		}
		return &v, nil
	case StoryInteractionTypeRepostTypeID:
		// Decoding storyInteractionTypeRepost#c32279c4.
		v := StoryInteractionTypeRepost{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StoryInteractionTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode StoryInteractionTypeClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONStoryInteractionType implements binary de-serialization for StoryInteractionTypeClass.
func DecodeTDLibJSONStoryInteractionType(buf tdjson.Decoder) (StoryInteractionTypeClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "storyInteractionTypeView":
		// Decoding storyInteractionTypeView#53e337d0.
		v := StoryInteractionTypeView{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StoryInteractionTypeClass: %w", err)
		}
		return &v, nil
	case "storyInteractionTypeForward":
		// Decoding storyInteractionTypeForward#27d23cff.
		v := StoryInteractionTypeForward{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StoryInteractionTypeClass: %w", err)
		}
		return &v, nil
	case "storyInteractionTypeRepost":
		// Decoding storyInteractionTypeRepost#c32279c4.
		v := StoryInteractionTypeRepost{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StoryInteractionTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode StoryInteractionTypeClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// StoryInteractionType boxes the StoryInteractionTypeClass providing a helper.
type StoryInteractionTypeBox struct {
	StoryInteractionType StoryInteractionTypeClass
}

// Decode implements bin.Decoder for StoryInteractionTypeBox.
func (b *StoryInteractionTypeBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode StoryInteractionTypeBox to nil")
	}
	v, err := DecodeStoryInteractionType(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.StoryInteractionType = v
	return nil
}

// Encode implements bin.Encode for StoryInteractionTypeBox.
func (b *StoryInteractionTypeBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.StoryInteractionType == nil {
		return fmt.Errorf("unable to encode StoryInteractionTypeClass as nil")
	}
	return b.StoryInteractionType.Encode(buf)
}

// DecodeTDLibJSON implements bin.Decoder for StoryInteractionTypeBox.
func (b *StoryInteractionTypeBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode StoryInteractionTypeBox to nil")
	}
	v, err := DecodeTDLibJSONStoryInteractionType(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.StoryInteractionType = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for StoryInteractionTypeBox.
func (b *StoryInteractionTypeBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.StoryInteractionType == nil {
		return fmt.Errorf("unable to encode StoryInteractionTypeClass as nil")
	}
	return b.StoryInteractionType.EncodeTDLibJSON(buf)
}
