// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// MessagesDiscussionMessage represents TL type `messages.discussionMessage#a6341782`.
// Information about a message thread¹
//
// Links:
//  1. https://core.telegram.org/api/threads
//
// See https://core.telegram.org/constructor/messages.discussionMessage for reference.
type MessagesDiscussionMessage struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Discussion messages
	Messages []MessageClass
	// Message ID of latest reply in this thread¹
	//
	// Links:
	//  1) https://core.telegram.org/api/threads
	//
	// Use SetMaxID and GetMaxID helpers.
	MaxID int
	// Message ID of latest read incoming message in this thread¹
	//
	// Links:
	//  1) https://core.telegram.org/api/threads
	//
	// Use SetReadInboxMaxID and GetReadInboxMaxID helpers.
	ReadInboxMaxID int
	// Message ID of latest read outgoing message in this thread¹
	//
	// Links:
	//  1) https://core.telegram.org/api/threads
	//
	// Use SetReadOutboxMaxID and GetReadOutboxMaxID helpers.
	ReadOutboxMaxID int
	// Number of unread messages
	UnreadCount int
	// Chats mentioned in constructor
	Chats []ChatClass
	// Users mentioned in constructor
	Users []UserClass
}

// MessagesDiscussionMessageTypeID is TL type id of MessagesDiscussionMessage.
const MessagesDiscussionMessageTypeID = 0xa6341782

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

func (d *MessagesDiscussionMessage) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Flags.Zero()) {
		return false
	}
	if !(d.Messages == nil) {
		return false
	}
	if !(d.MaxID == 0) {
		return false
	}
	if !(d.ReadInboxMaxID == 0) {
		return false
	}
	if !(d.ReadOutboxMaxID == 0) {
		return false
	}
	if !(d.UnreadCount == 0) {
		return false
	}
	if !(d.Chats == nil) {
		return false
	}
	if !(d.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesDiscussionMessage from given interface.
func (d *MessagesDiscussionMessage) FillFrom(from interface {
	GetMessages() (value []MessageClass)
	GetMaxID() (value int, ok bool)
	GetReadInboxMaxID() (value int, ok bool)
	GetReadOutboxMaxID() (value int, ok bool)
	GetUnreadCount() (value int)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	d.Messages = from.GetMessages()
	if val, ok := from.GetMaxID(); ok {
		d.MaxID = val
	}

	if val, ok := from.GetReadInboxMaxID(); ok {
		d.ReadInboxMaxID = val
	}

	if val, ok := from.GetReadOutboxMaxID(); ok {
		d.ReadOutboxMaxID = val
	}

	d.UnreadCount = from.GetUnreadCount()
	d.Chats = from.GetChats()
	d.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesDiscussionMessage) TypeName() string {
	return "messages.discussionMessage"
}

// TypeInfo returns info about TL type.
func (d *MessagesDiscussionMessage) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.discussionMessage",
		ID:   MessagesDiscussionMessageTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Messages",
			SchemaName: "messages",
		},
		{
			Name:       "MaxID",
			SchemaName: "max_id",
			Null:       !d.Flags.Has(0),
		},
		{
			Name:       "ReadInboxMaxID",
			SchemaName: "read_inbox_max_id",
			Null:       !d.Flags.Has(1),
		},
		{
			Name:       "ReadOutboxMaxID",
			SchemaName: "read_outbox_max_id",
			Null:       !d.Flags.Has(2),
		},
		{
			Name:       "UnreadCount",
			SchemaName: "unread_count",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (d *MessagesDiscussionMessage) SetFlags() {
	if !(d.MaxID == 0) {
		d.Flags.Set(0)
	}
	if !(d.ReadInboxMaxID == 0) {
		d.Flags.Set(1)
	}
	if !(d.ReadOutboxMaxID == 0) {
		d.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (d *MessagesDiscussionMessage) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode messages.discussionMessage#a6341782 as nil")
	}
	b.PutID(MessagesDiscussionMessageTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *MessagesDiscussionMessage) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode messages.discussionMessage#a6341782 as nil")
	}
	d.SetFlags()
	if err := d.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.discussionMessage#a6341782: field flags: %w", err)
	}
	b.PutVectorHeader(len(d.Messages))
	for idx, v := range d.Messages {
		if v == nil {
			return fmt.Errorf("unable to encode messages.discussionMessage#a6341782: field messages element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.discussionMessage#a6341782: field messages element with index %d: %w", idx, err)
		}
	}
	if d.Flags.Has(0) {
		b.PutInt(d.MaxID)
	}
	if d.Flags.Has(1) {
		b.PutInt(d.ReadInboxMaxID)
	}
	if d.Flags.Has(2) {
		b.PutInt(d.ReadOutboxMaxID)
	}
	b.PutInt(d.UnreadCount)
	b.PutVectorHeader(len(d.Chats))
	for idx, v := range d.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode messages.discussionMessage#a6341782: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.discussionMessage#a6341782: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(d.Users))
	for idx, v := range d.Users {
		if v == nil {
			return fmt.Errorf("unable to encode messages.discussionMessage#a6341782: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.discussionMessage#a6341782: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (d *MessagesDiscussionMessage) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode messages.discussionMessage#a6341782 to nil")
	}
	if err := b.ConsumeID(MessagesDiscussionMessageTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *MessagesDiscussionMessage) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode messages.discussionMessage#a6341782 to nil")
	}
	{
		if err := d.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: field flags: %w", err)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: field messages: %w", err)
		}

		if headerLen > 0 {
			d.Messages = make([]MessageClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeMessage(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: field messages: %w", err)
			}
			d.Messages = append(d.Messages, value)
		}
	}
	if d.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: field max_id: %w", err)
		}
		d.MaxID = value
	}
	if d.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: field read_inbox_max_id: %w", err)
		}
		d.ReadInboxMaxID = value
	}
	if d.Flags.Has(2) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: field read_outbox_max_id: %w", err)
		}
		d.ReadOutboxMaxID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: field unread_count: %w", err)
		}
		d.UnreadCount = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: field chats: %w", err)
		}

		if headerLen > 0 {
			d.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: field chats: %w", err)
			}
			d.Chats = append(d.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: field users: %w", err)
		}

		if headerLen > 0 {
			d.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.discussionMessage#a6341782: field users: %w", err)
			}
			d.Users = append(d.Users, value)
		}
	}
	return nil
}

// GetMessages returns value of Messages field.
func (d *MessagesDiscussionMessage) GetMessages() (value []MessageClass) {
	if d == nil {
		return
	}
	return d.Messages
}

// SetMaxID sets value of MaxID conditional field.
func (d *MessagesDiscussionMessage) SetMaxID(value int) {
	d.Flags.Set(0)
	d.MaxID = value
}

// GetMaxID returns value of MaxID conditional field and
// boolean which is true if field was set.
func (d *MessagesDiscussionMessage) GetMaxID() (value int, ok bool) {
	if d == nil {
		return
	}
	if !d.Flags.Has(0) {
		return value, false
	}
	return d.MaxID, true
}

// SetReadInboxMaxID sets value of ReadInboxMaxID conditional field.
func (d *MessagesDiscussionMessage) SetReadInboxMaxID(value int) {
	d.Flags.Set(1)
	d.ReadInboxMaxID = value
}

// GetReadInboxMaxID returns value of ReadInboxMaxID conditional field and
// boolean which is true if field was set.
func (d *MessagesDiscussionMessage) GetReadInboxMaxID() (value int, ok bool) {
	if d == nil {
		return
	}
	if !d.Flags.Has(1) {
		return value, false
	}
	return d.ReadInboxMaxID, true
}

// SetReadOutboxMaxID sets value of ReadOutboxMaxID conditional field.
func (d *MessagesDiscussionMessage) SetReadOutboxMaxID(value int) {
	d.Flags.Set(2)
	d.ReadOutboxMaxID = value
}

// GetReadOutboxMaxID returns value of ReadOutboxMaxID conditional field and
// boolean which is true if field was set.
func (d *MessagesDiscussionMessage) GetReadOutboxMaxID() (value int, ok bool) {
	if d == nil {
		return
	}
	if !d.Flags.Has(2) {
		return value, false
	}
	return d.ReadOutboxMaxID, true
}

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

// GetChats returns value of Chats field.
func (d *MessagesDiscussionMessage) GetChats() (value []ChatClass) {
	if d == nil {
		return
	}
	return d.Chats
}

// GetUsers returns value of Users field.
func (d *MessagesDiscussionMessage) GetUsers() (value []UserClass) {
	if d == nil {
		return
	}
	return d.Users
}

// MapMessages returns field Messages wrapped in MessageClassArray helper.
func (d *MessagesDiscussionMessage) MapMessages() (value MessageClassArray) {
	return MessageClassArray(d.Messages)
}

// MapChats returns field Chats wrapped in ChatClassArray helper.
func (d *MessagesDiscussionMessage) MapChats() (value ChatClassArray) {
	return ChatClassArray(d.Chats)
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func (d *MessagesDiscussionMessage) MapUsers() (value UserClassArray) {
	return UserClassArray(d.Users)
}
