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

// MessageReplyInfo represents TL type `messageReplyInfo#69240cb9`.
type MessageReplyInfo struct {
	// Number of times the message was directly or indirectly replied
	ReplyCount int32
	// Identifiers of at most 3 recent repliers to the message; available in channels with a
	// discussion supergroup. The users and chats are expected to be inaccessible: only their
	// photo and name will be available
	RecentReplierIDs []MessageSenderClass
	// Identifier of the last read incoming reply to the message
	LastReadInboxMessageID int64
	// Identifier of the last read outgoing reply to the message
	LastReadOutboxMessageID int64
	// Identifier of the last reply to the message
	LastMessageID int64
}

// MessageReplyInfoTypeID is TL type id of MessageReplyInfo.
const MessageReplyInfoTypeID = 0x69240cb9

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

func (m *MessageReplyInfo) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.ReplyCount == 0) {
		return false
	}
	if !(m.RecentReplierIDs == nil) {
		return false
	}
	if !(m.LastReadInboxMessageID == 0) {
		return false
	}
	if !(m.LastReadOutboxMessageID == 0) {
		return false
	}
	if !(m.LastMessageID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageReplyInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageReplyInfo",
		ID:   MessageReplyInfoTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ReplyCount",
			SchemaName: "reply_count",
		},
		{
			Name:       "RecentReplierIDs",
			SchemaName: "recent_replier_ids",
		},
		{
			Name:       "LastReadInboxMessageID",
			SchemaName: "last_read_inbox_message_id",
		},
		{
			Name:       "LastReadOutboxMessageID",
			SchemaName: "last_read_outbox_message_id",
		},
		{
			Name:       "LastMessageID",
			SchemaName: "last_message_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageReplyInfo) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageReplyInfo#69240cb9 as nil")
	}
	b.PutID(MessageReplyInfoTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageReplyInfo) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageReplyInfo#69240cb9 as nil")
	}
	b.PutInt32(m.ReplyCount)
	b.PutInt(len(m.RecentReplierIDs))
	for idx, v := range m.RecentReplierIDs {
		if v == nil {
			return fmt.Errorf("unable to encode messageReplyInfo#69240cb9: field recent_replier_ids element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare messageReplyInfo#69240cb9: field recent_replier_ids element with index %d: %w", idx, err)
		}
	}
	b.PutInt53(m.LastReadInboxMessageID)
	b.PutInt53(m.LastReadOutboxMessageID)
	b.PutInt53(m.LastMessageID)
	return nil
}

// Decode implements bin.Decoder.
func (m *MessageReplyInfo) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageReplyInfo#69240cb9 to nil")
	}
	if err := b.ConsumeID(MessageReplyInfoTypeID); err != nil {
		return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MessageReplyInfo) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageReplyInfo#69240cb9 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: field reply_count: %w", err)
		}
		m.ReplyCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: field recent_replier_ids: %w", err)
		}

		if headerLen > 0 {
			m.RecentReplierIDs = 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 messageReplyInfo#69240cb9: field recent_replier_ids: %w", err)
			}
			m.RecentReplierIDs = append(m.RecentReplierIDs, value)
		}
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: field last_read_inbox_message_id: %w", err)
		}
		m.LastReadInboxMessageID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: field last_read_outbox_message_id: %w", err)
		}
		m.LastReadOutboxMessageID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: field last_message_id: %w", err)
		}
		m.LastMessageID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageReplyInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageReplyInfo#69240cb9 as nil")
	}
	b.ObjStart()
	b.PutID("messageReplyInfo")
	b.Comma()
	b.FieldStart("reply_count")
	b.PutInt32(m.ReplyCount)
	b.Comma()
	b.FieldStart("recent_replier_ids")
	b.ArrStart()
	for idx, v := range m.RecentReplierIDs {
		if v == nil {
			return fmt.Errorf("unable to encode messageReplyInfo#69240cb9: field recent_replier_ids element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode messageReplyInfo#69240cb9: field recent_replier_ids element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("last_read_inbox_message_id")
	b.PutInt53(m.LastReadInboxMessageID)
	b.Comma()
	b.FieldStart("last_read_outbox_message_id")
	b.PutInt53(m.LastReadOutboxMessageID)
	b.Comma()
	b.FieldStart("last_message_id")
	b.PutInt53(m.LastMessageID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (m *MessageReplyInfo) DecodeTDLibJSON(b tdjson.Decoder) error {
	if m == nil {
		return fmt.Errorf("can't decode messageReplyInfo#69240cb9 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageReplyInfo"); err != nil {
				return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: %w", err)
			}
		case "reply_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: field reply_count: %w", err)
			}
			m.ReplyCount = value
		case "recent_replier_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONMessageSender(b)
				if err != nil {
					return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: field recent_replier_ids: %w", err)
				}
				m.RecentReplierIDs = append(m.RecentReplierIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: field recent_replier_ids: %w", err)
			}
		case "last_read_inbox_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: field last_read_inbox_message_id: %w", err)
			}
			m.LastReadInboxMessageID = value
		case "last_read_outbox_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: field last_read_outbox_message_id: %w", err)
			}
			m.LastReadOutboxMessageID = value
		case "last_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode messageReplyInfo#69240cb9: field last_message_id: %w", err)
			}
			m.LastMessageID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetReplyCount returns value of ReplyCount field.
func (m *MessageReplyInfo) GetReplyCount() (value int32) {
	if m == nil {
		return
	}
	return m.ReplyCount
}

// GetRecentReplierIDs returns value of RecentReplierIDs field.
func (m *MessageReplyInfo) GetRecentReplierIDs() (value []MessageSenderClass) {
	if m == nil {
		return
	}
	return m.RecentReplierIDs
}

// GetLastReadInboxMessageID returns value of LastReadInboxMessageID field.
func (m *MessageReplyInfo) GetLastReadInboxMessageID() (value int64) {
	if m == nil {
		return
	}
	return m.LastReadInboxMessageID
}

// GetLastReadOutboxMessageID returns value of LastReadOutboxMessageID field.
func (m *MessageReplyInfo) GetLastReadOutboxMessageID() (value int64) {
	if m == nil {
		return
	}
	return m.LastReadOutboxMessageID
}

// GetLastMessageID returns value of LastMessageID field.
func (m *MessageReplyInfo) GetLastMessageID() (value int64) {
	if m == nil {
		return
	}
	return m.LastMessageID
}
