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

// DirectMessagesChatTopic represents TL type `directMessagesChatTopic#7c6c89e7`.
type DirectMessagesChatTopic struct {
	// Identifier of the chat to which the topic belongs
	ChatID int64
	// Unique topic identifier
	ID int64
	// Identifier of the user or chat that sends the messages to the topic
	SenderID MessageSenderClass
	// A parameter used to determine order of the topic in the topic list. Topics must be
	// sorted by the order in descending order
	Order int64
	// True, if the forum topic is marked as unread
	IsMarkedAsUnread bool
	// Number of unread messages in the chat
	UnreadCount int64
	// Identifier of the last read incoming message
	LastReadInboxMessageID int64
	// Identifier of the last read outgoing message
	LastReadOutboxMessageID int64
	// Number of messages with unread reactions in the chat
	UnreadReactionCount int64
	// Last message in the topic; may be null if none or unknown
	LastMessage Message
	// A draft of a message in the topic; may be null if none
	DraftMessage DraftMessage
}

// DirectMessagesChatTopicTypeID is TL type id of DirectMessagesChatTopic.
const DirectMessagesChatTopicTypeID = 0x7c6c89e7

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

func (d *DirectMessagesChatTopic) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.ChatID == 0) {
		return false
	}
	if !(d.ID == 0) {
		return false
	}
	if !(d.SenderID == nil) {
		return false
	}
	if !(d.Order == 0) {
		return false
	}
	if !(d.IsMarkedAsUnread == false) {
		return false
	}
	if !(d.UnreadCount == 0) {
		return false
	}
	if !(d.LastReadInboxMessageID == 0) {
		return false
	}
	if !(d.LastReadOutboxMessageID == 0) {
		return false
	}
	if !(d.UnreadReactionCount == 0) {
		return false
	}
	if !(d.LastMessage.Zero()) {
		return false
	}
	if !(d.DraftMessage.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DirectMessagesChatTopic) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "directMessagesChatTopic",
		ID:   DirectMessagesChatTopicTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "SenderID",
			SchemaName: "sender_id",
		},
		{
			Name:       "Order",
			SchemaName: "order",
		},
		{
			Name:       "IsMarkedAsUnread",
			SchemaName: "is_marked_as_unread",
		},
		{
			Name:       "UnreadCount",
			SchemaName: "unread_count",
		},
		{
			Name:       "LastReadInboxMessageID",
			SchemaName: "last_read_inbox_message_id",
		},
		{
			Name:       "LastReadOutboxMessageID",
			SchemaName: "last_read_outbox_message_id",
		},
		{
			Name:       "UnreadReactionCount",
			SchemaName: "unread_reaction_count",
		},
		{
			Name:       "LastMessage",
			SchemaName: "last_message",
		},
		{
			Name:       "DraftMessage",
			SchemaName: "draft_message",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DirectMessagesChatTopic) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode directMessagesChatTopic#7c6c89e7 as nil")
	}
	b.PutID(DirectMessagesChatTopicTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DirectMessagesChatTopic) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode directMessagesChatTopic#7c6c89e7 as nil")
	}
	b.PutInt53(d.ChatID)
	b.PutInt53(d.ID)
	if d.SenderID == nil {
		return fmt.Errorf("unable to encode directMessagesChatTopic#7c6c89e7: field sender_id is nil")
	}
	if err := d.SenderID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode directMessagesChatTopic#7c6c89e7: field sender_id: %w", err)
	}
	b.PutLong(d.Order)
	b.PutBool(d.IsMarkedAsUnread)
	b.PutInt53(d.UnreadCount)
	b.PutInt53(d.LastReadInboxMessageID)
	b.PutInt53(d.LastReadOutboxMessageID)
	b.PutInt53(d.UnreadReactionCount)
	if err := d.LastMessage.Encode(b); err != nil {
		return fmt.Errorf("unable to encode directMessagesChatTopic#7c6c89e7: field last_message: %w", err)
	}
	if err := d.DraftMessage.Encode(b); err != nil {
		return fmt.Errorf("unable to encode directMessagesChatTopic#7c6c89e7: field draft_message: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (d *DirectMessagesChatTopic) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode directMessagesChatTopic#7c6c89e7 to nil")
	}
	if err := b.ConsumeID(DirectMessagesChatTopicTypeID); err != nil {
		return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DirectMessagesChatTopic) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode directMessagesChatTopic#7c6c89e7 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field chat_id: %w", err)
		}
		d.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field id: %w", err)
		}
		d.ID = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field sender_id: %w", err)
		}
		d.SenderID = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field order: %w", err)
		}
		d.Order = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field is_marked_as_unread: %w", err)
		}
		d.IsMarkedAsUnread = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field unread_count: %w", err)
		}
		d.UnreadCount = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field last_read_inbox_message_id: %w", err)
		}
		d.LastReadInboxMessageID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field last_read_outbox_message_id: %w", err)
		}
		d.LastReadOutboxMessageID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field unread_reaction_count: %w", err)
		}
		d.UnreadReactionCount = value
	}
	{
		if err := d.LastMessage.Decode(b); err != nil {
			return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field last_message: %w", err)
		}
	}
	{
		if err := d.DraftMessage.Decode(b); err != nil {
			return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field draft_message: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *DirectMessagesChatTopic) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode directMessagesChatTopic#7c6c89e7 as nil")
	}
	b.ObjStart()
	b.PutID("directMessagesChatTopic")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(d.ChatID)
	b.Comma()
	b.FieldStart("id")
	b.PutInt53(d.ID)
	b.Comma()
	b.FieldStart("sender_id")
	if d.SenderID == nil {
		return fmt.Errorf("unable to encode directMessagesChatTopic#7c6c89e7: field sender_id is nil")
	}
	if err := d.SenderID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode directMessagesChatTopic#7c6c89e7: field sender_id: %w", err)
	}
	b.Comma()
	b.FieldStart("order")
	b.PutLong(d.Order)
	b.Comma()
	b.FieldStart("is_marked_as_unread")
	b.PutBool(d.IsMarkedAsUnread)
	b.Comma()
	b.FieldStart("unread_count")
	b.PutInt53(d.UnreadCount)
	b.Comma()
	b.FieldStart("last_read_inbox_message_id")
	b.PutInt53(d.LastReadInboxMessageID)
	b.Comma()
	b.FieldStart("last_read_outbox_message_id")
	b.PutInt53(d.LastReadOutboxMessageID)
	b.Comma()
	b.FieldStart("unread_reaction_count")
	b.PutInt53(d.UnreadReactionCount)
	b.Comma()
	b.FieldStart("last_message")
	if err := d.LastMessage.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode directMessagesChatTopic#7c6c89e7: field last_message: %w", err)
	}
	b.Comma()
	b.FieldStart("draft_message")
	if err := d.DraftMessage.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode directMessagesChatTopic#7c6c89e7: field draft_message: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *DirectMessagesChatTopic) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode directMessagesChatTopic#7c6c89e7 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("directMessagesChatTopic"); err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field chat_id: %w", err)
			}
			d.ChatID = value
		case "id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field id: %w", err)
			}
			d.ID = value
		case "sender_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field sender_id: %w", err)
			}
			d.SenderID = value
		case "order":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field order: %w", err)
			}
			d.Order = value
		case "is_marked_as_unread":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field is_marked_as_unread: %w", err)
			}
			d.IsMarkedAsUnread = value
		case "unread_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field unread_count: %w", err)
			}
			d.UnreadCount = value
		case "last_read_inbox_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field last_read_inbox_message_id: %w", err)
			}
			d.LastReadInboxMessageID = value
		case "last_read_outbox_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field last_read_outbox_message_id: %w", err)
			}
			d.LastReadOutboxMessageID = value
		case "unread_reaction_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field unread_reaction_count: %w", err)
			}
			d.UnreadReactionCount = value
		case "last_message":
			if err := d.LastMessage.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field last_message: %w", err)
			}
		case "draft_message":
			if err := d.DraftMessage.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode directMessagesChatTopic#7c6c89e7: field draft_message: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetID returns value of ID field.
func (d *DirectMessagesChatTopic) GetID() (value int64) {
	if d == nil {
		return
	}
	return d.ID
}

// GetSenderID returns value of SenderID field.
func (d *DirectMessagesChatTopic) GetSenderID() (value MessageSenderClass) {
	if d == nil {
		return
	}
	return d.SenderID
}

// GetOrder returns value of Order field.
func (d *DirectMessagesChatTopic) GetOrder() (value int64) {
	if d == nil {
		return
	}
	return d.Order
}

// GetIsMarkedAsUnread returns value of IsMarkedAsUnread field.
func (d *DirectMessagesChatTopic) GetIsMarkedAsUnread() (value bool) {
	if d == nil {
		return
	}
	return d.IsMarkedAsUnread
}

// GetUnreadCount returns value of UnreadCount field.
func (d *DirectMessagesChatTopic) GetUnreadCount() (value int64) {
	if d == nil {
		return
	}
	return d.UnreadCount
}

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

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

// GetUnreadReactionCount returns value of UnreadReactionCount field.
func (d *DirectMessagesChatTopic) GetUnreadReactionCount() (value int64) {
	if d == nil {
		return
	}
	return d.UnreadReactionCount
}

// GetLastMessage returns value of LastMessage field.
func (d *DirectMessagesChatTopic) GetLastMessage() (value Message) {
	if d == nil {
		return
	}
	return d.LastMessage
}

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