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

// ChatAvailableReactionsAll represents TL type `chatAvailableReactionsAll#29600b97`.
type ChatAvailableReactionsAll struct {
	// The maximum allowed number of reactions per message; 1-11
	MaxReactionCount int32
}

// ChatAvailableReactionsAllTypeID is TL type id of ChatAvailableReactionsAll.
const ChatAvailableReactionsAllTypeID = 0x29600b97

// construct implements constructor of ChatAvailableReactionsClass.
func (c ChatAvailableReactionsAll) construct() ChatAvailableReactionsClass { return &c }

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

	_ ChatAvailableReactionsClass = &ChatAvailableReactionsAll{}
)

func (c *ChatAvailableReactionsAll) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.MaxReactionCount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatAvailableReactionsAll) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatAvailableReactionsAll",
		ID:   ChatAvailableReactionsAllTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MaxReactionCount",
			SchemaName: "max_reaction_count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatAvailableReactionsAll) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatAvailableReactionsAll#29600b97 as nil")
	}
	b.PutID(ChatAvailableReactionsAllTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatAvailableReactionsAll) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatAvailableReactionsAll#29600b97 as nil")
	}
	b.PutInt32(c.MaxReactionCount)
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatAvailableReactionsAll) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatAvailableReactionsAll#29600b97 to nil")
	}
	if err := b.ConsumeID(ChatAvailableReactionsAllTypeID); err != nil {
		return fmt.Errorf("unable to decode chatAvailableReactionsAll#29600b97: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatAvailableReactionsAll) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatAvailableReactionsAll#29600b97 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chatAvailableReactionsAll#29600b97: field max_reaction_count: %w", err)
		}
		c.MaxReactionCount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatAvailableReactionsAll) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatAvailableReactionsAll#29600b97 as nil")
	}
	b.ObjStart()
	b.PutID("chatAvailableReactionsAll")
	b.Comma()
	b.FieldStart("max_reaction_count")
	b.PutInt32(c.MaxReactionCount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ChatAvailableReactionsAll) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatAvailableReactionsAll#29600b97 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatAvailableReactionsAll"); err != nil {
				return fmt.Errorf("unable to decode chatAvailableReactionsAll#29600b97: %w", err)
			}
		case "max_reaction_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatAvailableReactionsAll#29600b97: field max_reaction_count: %w", err)
			}
			c.MaxReactionCount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetMaxReactionCount returns value of MaxReactionCount field.
func (c *ChatAvailableReactionsAll) GetMaxReactionCount() (value int32) {
	if c == nil {
		return
	}
	return c.MaxReactionCount
}

// ChatAvailableReactionsSome represents TL type `chatAvailableReactionsSome#36b46b7d`.
type ChatAvailableReactionsSome struct {
	// The list of reactions
	Reactions []ReactionTypeClass
	// The maximum allowed number of reactions per message; 1-11
	MaxReactionCount int32
}

// ChatAvailableReactionsSomeTypeID is TL type id of ChatAvailableReactionsSome.
const ChatAvailableReactionsSomeTypeID = 0x36b46b7d

// construct implements constructor of ChatAvailableReactionsClass.
func (c ChatAvailableReactionsSome) construct() ChatAvailableReactionsClass { return &c }

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

	_ ChatAvailableReactionsClass = &ChatAvailableReactionsSome{}
)

func (c *ChatAvailableReactionsSome) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Reactions == nil) {
		return false
	}
	if !(c.MaxReactionCount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatAvailableReactionsSome) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatAvailableReactionsSome",
		ID:   ChatAvailableReactionsSomeTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Reactions",
			SchemaName: "reactions",
		},
		{
			Name:       "MaxReactionCount",
			SchemaName: "max_reaction_count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatAvailableReactionsSome) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatAvailableReactionsSome#36b46b7d as nil")
	}
	b.PutID(ChatAvailableReactionsSomeTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatAvailableReactionsSome) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatAvailableReactionsSome#36b46b7d as nil")
	}
	b.PutInt(len(c.Reactions))
	for idx, v := range c.Reactions {
		if v == nil {
			return fmt.Errorf("unable to encode chatAvailableReactionsSome#36b46b7d: field reactions element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare chatAvailableReactionsSome#36b46b7d: field reactions element with index %d: %w", idx, err)
		}
	}
	b.PutInt32(c.MaxReactionCount)
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatAvailableReactionsSome) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatAvailableReactionsSome#36b46b7d to nil")
	}
	if err := b.ConsumeID(ChatAvailableReactionsSomeTypeID); err != nil {
		return fmt.Errorf("unable to decode chatAvailableReactionsSome#36b46b7d: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatAvailableReactionsSome) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatAvailableReactionsSome#36b46b7d to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chatAvailableReactionsSome#36b46b7d: field reactions: %w", err)
		}

		if headerLen > 0 {
			c.Reactions = make([]ReactionTypeClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeReactionType(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatAvailableReactionsSome#36b46b7d: field reactions: %w", err)
			}
			c.Reactions = append(c.Reactions, value)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chatAvailableReactionsSome#36b46b7d: field max_reaction_count: %w", err)
		}
		c.MaxReactionCount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatAvailableReactionsSome) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatAvailableReactionsSome#36b46b7d as nil")
	}
	b.ObjStart()
	b.PutID("chatAvailableReactionsSome")
	b.Comma()
	b.FieldStart("reactions")
	b.ArrStart()
	for idx, v := range c.Reactions {
		if v == nil {
			return fmt.Errorf("unable to encode chatAvailableReactionsSome#36b46b7d: field reactions element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode chatAvailableReactionsSome#36b46b7d: field reactions element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("max_reaction_count")
	b.PutInt32(c.MaxReactionCount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ChatAvailableReactionsSome) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatAvailableReactionsSome#36b46b7d to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatAvailableReactionsSome"); err != nil {
				return fmt.Errorf("unable to decode chatAvailableReactionsSome#36b46b7d: %w", err)
			}
		case "reactions":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONReactionType(b)
				if err != nil {
					return fmt.Errorf("unable to decode chatAvailableReactionsSome#36b46b7d: field reactions: %w", err)
				}
				c.Reactions = append(c.Reactions, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatAvailableReactionsSome#36b46b7d: field reactions: %w", err)
			}
		case "max_reaction_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatAvailableReactionsSome#36b46b7d: field max_reaction_count: %w", err)
			}
			c.MaxReactionCount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetReactions returns value of Reactions field.
func (c *ChatAvailableReactionsSome) GetReactions() (value []ReactionTypeClass) {
	if c == nil {
		return
	}
	return c.Reactions
}

// GetMaxReactionCount returns value of MaxReactionCount field.
func (c *ChatAvailableReactionsSome) GetMaxReactionCount() (value int32) {
	if c == nil {
		return
	}
	return c.MaxReactionCount
}

// ChatAvailableReactionsClassName is schema name of ChatAvailableReactionsClass.
const ChatAvailableReactionsClassName = "ChatAvailableReactions"

// ChatAvailableReactionsClass represents ChatAvailableReactions generic type.
//
// Example:
//
//	g, err := tdapi.DecodeChatAvailableReactions(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.ChatAvailableReactionsAll: // chatAvailableReactionsAll#29600b97
//	case *tdapi.ChatAvailableReactionsSome: // chatAvailableReactionsSome#36b46b7d
//	default: panic(v)
//	}
type ChatAvailableReactionsClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() ChatAvailableReactionsClass

	// 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

	// The maximum allowed number of reactions per message; 1-11
	GetMaxReactionCount() (value int32)
}

// DecodeChatAvailableReactions implements binary de-serialization for ChatAvailableReactionsClass.
func DecodeChatAvailableReactions(buf *bin.Buffer) (ChatAvailableReactionsClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case ChatAvailableReactionsAllTypeID:
		// Decoding chatAvailableReactionsAll#29600b97.
		v := ChatAvailableReactionsAll{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatAvailableReactionsClass: %w", err)
		}
		return &v, nil
	case ChatAvailableReactionsSomeTypeID:
		// Decoding chatAvailableReactionsSome#36b46b7d.
		v := ChatAvailableReactionsSome{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatAvailableReactionsClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode ChatAvailableReactionsClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONChatAvailableReactions implements binary de-serialization for ChatAvailableReactionsClass.
func DecodeTDLibJSONChatAvailableReactions(buf tdjson.Decoder) (ChatAvailableReactionsClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "chatAvailableReactionsAll":
		// Decoding chatAvailableReactionsAll#29600b97.
		v := ChatAvailableReactionsAll{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatAvailableReactionsClass: %w", err)
		}
		return &v, nil
	case "chatAvailableReactionsSome":
		// Decoding chatAvailableReactionsSome#36b46b7d.
		v := ChatAvailableReactionsSome{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatAvailableReactionsClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode ChatAvailableReactionsClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// ChatAvailableReactions boxes the ChatAvailableReactionsClass providing a helper.
type ChatAvailableReactionsBox struct {
	ChatAvailableReactions ChatAvailableReactionsClass
}

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

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

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

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