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

// MessageReaction represents TL type `messageReaction#7d9e9864`.
type MessageReaction struct {
	// Type of the reaction
	Type ReactionTypeClass
	// Number of times the reaction was added
	TotalCount int32
	// True, if the reaction is chosen by the current user
	IsChosen bool
	// Identifier of the message sender used by the current user to add the reaction; may be
	// null if unknown or the reaction isn't chosen
	UsedSenderID MessageSenderClass
	// Identifiers of at most 3 recent message senders, added the reaction; available in
	// private, basic group and supergroup chats
	RecentSenderIDs []MessageSenderClass
}

// MessageReactionTypeID is TL type id of MessageReaction.
const MessageReactionTypeID = 0x7d9e9864

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

func (m *MessageReaction) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.Type == nil) {
		return false
	}
	if !(m.TotalCount == 0) {
		return false
	}
	if !(m.IsChosen == false) {
		return false
	}
	if !(m.UsedSenderID == nil) {
		return false
	}
	if !(m.RecentSenderIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageReaction) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageReaction",
		ID:   MessageReactionTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "IsChosen",
			SchemaName: "is_chosen",
		},
		{
			Name:       "UsedSenderID",
			SchemaName: "used_sender_id",
		},
		{
			Name:       "RecentSenderIDs",
			SchemaName: "recent_sender_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageReaction) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageReaction#7d9e9864 as nil")
	}
	b.PutID(MessageReactionTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageReaction) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageReaction#7d9e9864 as nil")
	}
	if m.Type == nil {
		return fmt.Errorf("unable to encode messageReaction#7d9e9864: field type is nil")
	}
	if err := m.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageReaction#7d9e9864: field type: %w", err)
	}
	b.PutInt32(m.TotalCount)
	b.PutBool(m.IsChosen)
	if m.UsedSenderID == nil {
		return fmt.Errorf("unable to encode messageReaction#7d9e9864: field used_sender_id is nil")
	}
	if err := m.UsedSenderID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageReaction#7d9e9864: field used_sender_id: %w", err)
	}
	b.PutInt(len(m.RecentSenderIDs))
	for idx, v := range m.RecentSenderIDs {
		if v == nil {
			return fmt.Errorf("unable to encode messageReaction#7d9e9864: field recent_sender_ids element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare messageReaction#7d9e9864: field recent_sender_ids element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (m *MessageReaction) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageReaction#7d9e9864 to nil")
	}
	if err := b.ConsumeID(MessageReactionTypeID); err != nil {
		return fmt.Errorf("unable to decode messageReaction#7d9e9864: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MessageReaction) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageReaction#7d9e9864 to nil")
	}
	{
		value, err := DecodeReactionType(b)
		if err != nil {
			return fmt.Errorf("unable to decode messageReaction#7d9e9864: field type: %w", err)
		}
		m.Type = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode messageReaction#7d9e9864: field total_count: %w", err)
		}
		m.TotalCount = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode messageReaction#7d9e9864: field is_chosen: %w", err)
		}
		m.IsChosen = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode messageReaction#7d9e9864: field used_sender_id: %w", err)
		}
		m.UsedSenderID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messageReaction#7d9e9864: field recent_sender_ids: %w", err)
		}

		if headerLen > 0 {
			m.RecentSenderIDs = make([]MessageSenderClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode messageReaction#7d9e9864: field recent_sender_ids: %w", err)
			}
			m.RecentSenderIDs = append(m.RecentSenderIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageReaction) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageReaction#7d9e9864 as nil")
	}
	b.ObjStart()
	b.PutID("messageReaction")
	b.Comma()
	b.FieldStart("type")
	if m.Type == nil {
		return fmt.Errorf("unable to encode messageReaction#7d9e9864: field type is nil")
	}
	if err := m.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageReaction#7d9e9864: field type: %w", err)
	}
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(m.TotalCount)
	b.Comma()
	b.FieldStart("is_chosen")
	b.PutBool(m.IsChosen)
	b.Comma()
	b.FieldStart("used_sender_id")
	if m.UsedSenderID == nil {
		return fmt.Errorf("unable to encode messageReaction#7d9e9864: field used_sender_id is nil")
	}
	if err := m.UsedSenderID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageReaction#7d9e9864: field used_sender_id: %w", err)
	}
	b.Comma()
	b.FieldStart("recent_sender_ids")
	b.ArrStart()
	for idx, v := range m.RecentSenderIDs {
		if v == nil {
			return fmt.Errorf("unable to encode messageReaction#7d9e9864: field recent_sender_ids element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode messageReaction#7d9e9864: field recent_sender_ids element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (m *MessageReaction) DecodeTDLibJSON(b tdjson.Decoder) error {
	if m == nil {
		return fmt.Errorf("can't decode messageReaction#7d9e9864 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageReaction"); err != nil {
				return fmt.Errorf("unable to decode messageReaction#7d9e9864: %w", err)
			}
		case "type":
			value, err := DecodeTDLibJSONReactionType(b)
			if err != nil {
				return fmt.Errorf("unable to decode messageReaction#7d9e9864: field type: %w", err)
			}
			m.Type = value
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode messageReaction#7d9e9864: field total_count: %w", err)
			}
			m.TotalCount = value
		case "is_chosen":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode messageReaction#7d9e9864: field is_chosen: %w", err)
			}
			m.IsChosen = value
		case "used_sender_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode messageReaction#7d9e9864: field used_sender_id: %w", err)
			}
			m.UsedSenderID = value
		case "recent_sender_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONMessageSender(b)
				if err != nil {
					return fmt.Errorf("unable to decode messageReaction#7d9e9864: field recent_sender_ids: %w", err)
				}
				m.RecentSenderIDs = append(m.RecentSenderIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode messageReaction#7d9e9864: field recent_sender_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetType returns value of Type field.
func (m *MessageReaction) GetType() (value ReactionTypeClass) {
	if m == nil {
		return
	}
	return m.Type
}

// GetTotalCount returns value of TotalCount field.
func (m *MessageReaction) GetTotalCount() (value int32) {
	if m == nil {
		return
	}
	return m.TotalCount
}

// GetIsChosen returns value of IsChosen field.
func (m *MessageReaction) GetIsChosen() (value bool) {
	if m == nil {
		return
	}
	return m.IsChosen
}

// GetUsedSenderID returns value of UsedSenderID field.
func (m *MessageReaction) GetUsedSenderID() (value MessageSenderClass) {
	if m == nil {
		return
	}
	return m.UsedSenderID
}

// GetRecentSenderIDs returns value of RecentSenderIDs field.
func (m *MessageReaction) GetRecentSenderIDs() (value []MessageSenderClass) {
	if m == nil {
		return
	}
	return m.RecentSenderIDs
}
