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

// MessageTopicForum represents TL type `messageTopicForum#77083069`.
type MessageTopicForum struct {
	// Unique identifier of the forum topic; all messages in a non-forum supergroup chats
	// belongs to the General topic
	ForumTopicID int64
}

// MessageTopicForumTypeID is TL type id of MessageTopicForum.
const MessageTopicForumTypeID = 0x77083069

// construct implements constructor of MessageTopicClass.
func (m MessageTopicForum) construct() MessageTopicClass { return &m }

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

	_ MessageTopicClass = &MessageTopicForum{}
)

func (m *MessageTopicForum) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.ForumTopicID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageTopicForum) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageTopicForum",
		ID:   MessageTopicForumTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ForumTopicID",
			SchemaName: "forum_topic_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageTopicForum) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageTopicForum#77083069 as nil")
	}
	b.PutID(MessageTopicForumTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageTopicForum) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageTopicForum#77083069 as nil")
	}
	b.PutInt53(m.ForumTopicID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (m *MessageTopicForum) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageTopicForum#77083069 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode messageTopicForum#77083069: field forum_topic_id: %w", err)
		}
		m.ForumTopicID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageTopicForum) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageTopicForum#77083069 as nil")
	}
	b.ObjStart()
	b.PutID("messageTopicForum")
	b.Comma()
	b.FieldStart("forum_topic_id")
	b.PutInt53(m.ForumTopicID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageTopicForum"); err != nil {
				return fmt.Errorf("unable to decode messageTopicForum#77083069: %w", err)
			}
		case "forum_topic_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode messageTopicForum#77083069: field forum_topic_id: %w", err)
			}
			m.ForumTopicID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetForumTopicID returns value of ForumTopicID field.
func (m *MessageTopicForum) GetForumTopicID() (value int64) {
	if m == nil {
		return
	}
	return m.ForumTopicID
}

// MessageTopicDirectMessages represents TL type `messageTopicDirectMessages#b362add9`.
type MessageTopicDirectMessages struct {
	// Unique identifier of the topic
	DirectMessagesChatTopicID int64
}

// MessageTopicDirectMessagesTypeID is TL type id of MessageTopicDirectMessages.
const MessageTopicDirectMessagesTypeID = 0xb362add9

// construct implements constructor of MessageTopicClass.
func (m MessageTopicDirectMessages) construct() MessageTopicClass { return &m }

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

	_ MessageTopicClass = &MessageTopicDirectMessages{}
)

func (m *MessageTopicDirectMessages) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.DirectMessagesChatTopicID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageTopicDirectMessages) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageTopicDirectMessages",
		ID:   MessageTopicDirectMessagesTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "DirectMessagesChatTopicID",
			SchemaName: "direct_messages_chat_topic_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageTopicDirectMessages) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageTopicDirectMessages#b362add9 as nil")
	}
	b.PutID(MessageTopicDirectMessagesTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageTopicDirectMessages) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageTopicDirectMessages#b362add9 as nil")
	}
	b.PutInt53(m.DirectMessagesChatTopicID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (m *MessageTopicDirectMessages) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageTopicDirectMessages#b362add9 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode messageTopicDirectMessages#b362add9: field direct_messages_chat_topic_id: %w", err)
		}
		m.DirectMessagesChatTopicID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageTopicDirectMessages) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageTopicDirectMessages#b362add9 as nil")
	}
	b.ObjStart()
	b.PutID("messageTopicDirectMessages")
	b.Comma()
	b.FieldStart("direct_messages_chat_topic_id")
	b.PutInt53(m.DirectMessagesChatTopicID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageTopicDirectMessages"); err != nil {
				return fmt.Errorf("unable to decode messageTopicDirectMessages#b362add9: %w", err)
			}
		case "direct_messages_chat_topic_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode messageTopicDirectMessages#b362add9: field direct_messages_chat_topic_id: %w", err)
			}
			m.DirectMessagesChatTopicID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetDirectMessagesChatTopicID returns value of DirectMessagesChatTopicID field.
func (m *MessageTopicDirectMessages) GetDirectMessagesChatTopicID() (value int64) {
	if m == nil {
		return
	}
	return m.DirectMessagesChatTopicID
}

// MessageTopicSavedMessages represents TL type `messageTopicSavedMessages#230c946f`.
type MessageTopicSavedMessages struct {
	// Unique identifier of the Saved Messages topic
	SavedMessagesTopicID int64
}

// MessageTopicSavedMessagesTypeID is TL type id of MessageTopicSavedMessages.
const MessageTopicSavedMessagesTypeID = 0x230c946f

// construct implements constructor of MessageTopicClass.
func (m MessageTopicSavedMessages) construct() MessageTopicClass { return &m }

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

	_ MessageTopicClass = &MessageTopicSavedMessages{}
)

func (m *MessageTopicSavedMessages) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.SavedMessagesTopicID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageTopicSavedMessages) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageTopicSavedMessages",
		ID:   MessageTopicSavedMessagesTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SavedMessagesTopicID",
			SchemaName: "saved_messages_topic_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageTopicSavedMessages) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageTopicSavedMessages#230c946f as nil")
	}
	b.PutID(MessageTopicSavedMessagesTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageTopicSavedMessages) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageTopicSavedMessages#230c946f as nil")
	}
	b.PutInt53(m.SavedMessagesTopicID)
	return nil
}

// Decode implements bin.Decoder.
func (m *MessageTopicSavedMessages) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageTopicSavedMessages#230c946f to nil")
	}
	if err := b.ConsumeID(MessageTopicSavedMessagesTypeID); err != nil {
		return fmt.Errorf("unable to decode messageTopicSavedMessages#230c946f: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MessageTopicSavedMessages) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageTopicSavedMessages#230c946f to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode messageTopicSavedMessages#230c946f: field saved_messages_topic_id: %w", err)
		}
		m.SavedMessagesTopicID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageTopicSavedMessages) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageTopicSavedMessages#230c946f as nil")
	}
	b.ObjStart()
	b.PutID("messageTopicSavedMessages")
	b.Comma()
	b.FieldStart("saved_messages_topic_id")
	b.PutInt53(m.SavedMessagesTopicID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (m *MessageTopicSavedMessages) DecodeTDLibJSON(b tdjson.Decoder) error {
	if m == nil {
		return fmt.Errorf("can't decode messageTopicSavedMessages#230c946f to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageTopicSavedMessages"); err != nil {
				return fmt.Errorf("unable to decode messageTopicSavedMessages#230c946f: %w", err)
			}
		case "saved_messages_topic_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode messageTopicSavedMessages#230c946f: field saved_messages_topic_id: %w", err)
			}
			m.SavedMessagesTopicID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSavedMessagesTopicID returns value of SavedMessagesTopicID field.
func (m *MessageTopicSavedMessages) GetSavedMessagesTopicID() (value int64) {
	if m == nil {
		return
	}
	return m.SavedMessagesTopicID
}

// MessageTopicClassName is schema name of MessageTopicClass.
const MessageTopicClassName = "MessageTopic"

// MessageTopicClass represents MessageTopic generic type.
//
// Example:
//
//	g, err := tdapi.DecodeMessageTopic(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.MessageTopicForum: // messageTopicForum#77083069
//	case *tdapi.MessageTopicDirectMessages: // messageTopicDirectMessages#b362add9
//	case *tdapi.MessageTopicSavedMessages: // messageTopicSavedMessages#230c946f
//	default: panic(v)
//	}
type MessageTopicClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() MessageTopicClass

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

// DecodeMessageTopic implements binary de-serialization for MessageTopicClass.
func DecodeMessageTopic(buf *bin.Buffer) (MessageTopicClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case MessageTopicForumTypeID:
		// Decoding messageTopicForum#77083069.
		v := MessageTopicForum{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessageTopicClass: %w", err)
		}
		return &v, nil
	case MessageTopicDirectMessagesTypeID:
		// Decoding messageTopicDirectMessages#b362add9.
		v := MessageTopicDirectMessages{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessageTopicClass: %w", err)
		}
		return &v, nil
	case MessageTopicSavedMessagesTypeID:
		// Decoding messageTopicSavedMessages#230c946f.
		v := MessageTopicSavedMessages{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessageTopicClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode MessageTopicClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONMessageTopic implements binary de-serialization for MessageTopicClass.
func DecodeTDLibJSONMessageTopic(buf tdjson.Decoder) (MessageTopicClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "messageTopicForum":
		// Decoding messageTopicForum#77083069.
		v := MessageTopicForum{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessageTopicClass: %w", err)
		}
		return &v, nil
	case "messageTopicDirectMessages":
		// Decoding messageTopicDirectMessages#b362add9.
		v := MessageTopicDirectMessages{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessageTopicClass: %w", err)
		}
		return &v, nil
	case "messageTopicSavedMessages":
		// Decoding messageTopicSavedMessages#230c946f.
		v := MessageTopicSavedMessages{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessageTopicClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode MessageTopicClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// MessageTopic boxes the MessageTopicClass providing a helper.
type MessageTopicBox struct {
	MessageTopic MessageTopicClass
}

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

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

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

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