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

// MessageThreadInfo represents TL type `messageThreadInfo#683fd5ad`.
type MessageThreadInfo struct {
	// Identifier of the chat to which the message thread belongs
	ChatID int64
	// Message thread identifier, unique within the chat
	MessageThreadID int64
	// Information about the message thread; may be null for forum topic threads
	ReplyInfo MessageReplyInfo
	// Approximate number of unread messages in the message thread
	UnreadMessageCount int32
	// The messages from which the thread starts. The messages are returned in reverse
	// chronological order (i.e., in order of decreasing message_id)
	Messages []Message
	// A draft of a message in the message thread; may be null if none
	DraftMessage DraftMessage
}

// MessageThreadInfoTypeID is TL type id of MessageThreadInfo.
const MessageThreadInfoTypeID = 0x683fd5ad

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

func (m *MessageThreadInfo) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.ChatID == 0) {
		return false
	}
	if !(m.MessageThreadID == 0) {
		return false
	}
	if !(m.ReplyInfo.Zero()) {
		return false
	}
	if !(m.UnreadMessageCount == 0) {
		return false
	}
	if !(m.Messages == nil) {
		return false
	}
	if !(m.DraftMessage.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageThreadInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageThreadInfo",
		ID:   MessageThreadInfoTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageThreadID",
			SchemaName: "message_thread_id",
		},
		{
			Name:       "ReplyInfo",
			SchemaName: "reply_info",
		},
		{
			Name:       "UnreadMessageCount",
			SchemaName: "unread_message_count",
		},
		{
			Name:       "Messages",
			SchemaName: "messages",
		},
		{
			Name:       "DraftMessage",
			SchemaName: "draft_message",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageThreadInfo) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageThreadInfo#683fd5ad as nil")
	}
	b.PutID(MessageThreadInfoTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageThreadInfo) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageThreadInfo#683fd5ad as nil")
	}
	b.PutInt53(m.ChatID)
	b.PutInt53(m.MessageThreadID)
	if err := m.ReplyInfo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageThreadInfo#683fd5ad: field reply_info: %w", err)
	}
	b.PutInt32(m.UnreadMessageCount)
	b.PutInt(len(m.Messages))
	for idx, v := range m.Messages {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare messageThreadInfo#683fd5ad: field messages element with index %d: %w", idx, err)
		}
	}
	if err := m.DraftMessage.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageThreadInfo#683fd5ad: field draft_message: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (m *MessageThreadInfo) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageThreadInfo#683fd5ad to nil")
	}
	if err := b.ConsumeID(MessageThreadInfoTypeID); err != nil {
		return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MessageThreadInfo) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageThreadInfo#683fd5ad to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field chat_id: %w", err)
		}
		m.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field message_thread_id: %w", err)
		}
		m.MessageThreadID = value
	}
	{
		if err := m.ReplyInfo.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field reply_info: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field unread_message_count: %w", err)
		}
		m.UnreadMessageCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field messages: %w", err)
		}

		if headerLen > 0 {
			m.Messages = make([]Message, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value Message
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare messageThreadInfo#683fd5ad: field messages: %w", err)
			}
			m.Messages = append(m.Messages, value)
		}
	}
	{
		if err := m.DraftMessage.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field draft_message: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageThreadInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageThreadInfo#683fd5ad as nil")
	}
	b.ObjStart()
	b.PutID("messageThreadInfo")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(m.ChatID)
	b.Comma()
	b.FieldStart("message_thread_id")
	b.PutInt53(m.MessageThreadID)
	b.Comma()
	b.FieldStart("reply_info")
	if err := m.ReplyInfo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageThreadInfo#683fd5ad: field reply_info: %w", err)
	}
	b.Comma()
	b.FieldStart("unread_message_count")
	b.PutInt32(m.UnreadMessageCount)
	b.Comma()
	b.FieldStart("messages")
	b.ArrStart()
	for idx, v := range m.Messages {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode messageThreadInfo#683fd5ad: field messages element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("draft_message")
	if err := m.DraftMessage.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageThreadInfo#683fd5ad: field draft_message: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (m *MessageThreadInfo) DecodeTDLibJSON(b tdjson.Decoder) error {
	if m == nil {
		return fmt.Errorf("can't decode messageThreadInfo#683fd5ad to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageThreadInfo"); err != nil {
				return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field chat_id: %w", err)
			}
			m.ChatID = value
		case "message_thread_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field message_thread_id: %w", err)
			}
			m.MessageThreadID = value
		case "reply_info":
			if err := m.ReplyInfo.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field reply_info: %w", err)
			}
		case "unread_message_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field unread_message_count: %w", err)
			}
			m.UnreadMessageCount = value
		case "messages":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value Message
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field messages: %w", err)
				}
				m.Messages = append(m.Messages, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field messages: %w", err)
			}
		case "draft_message":
			if err := m.DraftMessage.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode messageThreadInfo#683fd5ad: field draft_message: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (m *MessageThreadInfo) GetChatID() (value int64) {
	if m == nil {
		return
	}
	return m.ChatID
}

// GetMessageThreadID returns value of MessageThreadID field.
func (m *MessageThreadInfo) GetMessageThreadID() (value int64) {
	if m == nil {
		return
	}
	return m.MessageThreadID
}

// GetReplyInfo returns value of ReplyInfo field.
func (m *MessageThreadInfo) GetReplyInfo() (value MessageReplyInfo) {
	if m == nil {
		return
	}
	return m.ReplyInfo
}

// GetUnreadMessageCount returns value of UnreadMessageCount field.
func (m *MessageThreadInfo) GetUnreadMessageCount() (value int32) {
	if m == nil {
		return
	}
	return m.UnreadMessageCount
}

// GetMessages returns value of Messages field.
func (m *MessageThreadInfo) GetMessages() (value []Message) {
	if m == nil {
		return
	}
	return m.Messages
}

// GetDraftMessage returns value of DraftMessage field.
func (m *MessageThreadInfo) GetDraftMessage() (value DraftMessage) {
	if m == nil {
		return
	}
	return m.DraftMessage
}
