// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// MessagesForumTopics represents TL type `messages.forumTopics#367617d3`.
// Contains information about multiple forum topics¹
//
// Links:
//  1. https://core.telegram.org/api/forum#forum-topics
//
// See https://core.telegram.org/constructor/messages.forumTopics for reference.
type MessagesForumTopics struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether the returned topics are ordered by creation date; if set, pagination by
	// offset_date should use forumTopic¹.date; otherwise topics are ordered by the last
	// message date, so paginate by the date of the message² referenced by forumTopic³
	// top_message.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/forumTopic
	//  2) https://core.telegram.org/type/Message
	//  3) https://core.telegram.org/constructor/forumTopic
	OrderByCreateDate bool
	// Total number of topics matching query; may be more than the topics contained in topics
	// in which case pagination¹ is required.
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	Count int
	// Forum topics
	Topics []ForumTopicClass
	// Related messages (contains the messages mentioned by forumTopic¹.top_message).
	//
	// Links:
	//  1) https://core.telegram.org/constructor/forumTopic
	Messages []MessageClass
	// Related chats
	Chats []ChatClass
	// Related users
	Users []UserClass
	// Event count after generation¹
	//
	// Links:
	//  1) https://core.telegram.org/api/updates
	Pts int
}

// MessagesForumTopicsTypeID is TL type id of MessagesForumTopics.
const MessagesForumTopicsTypeID = 0x367617d3

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

func (f *MessagesForumTopics) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Flags.Zero()) {
		return false
	}
	if !(f.OrderByCreateDate == false) {
		return false
	}
	if !(f.Count == 0) {
		return false
	}
	if !(f.Topics == nil) {
		return false
	}
	if !(f.Messages == nil) {
		return false
	}
	if !(f.Chats == nil) {
		return false
	}
	if !(f.Users == nil) {
		return false
	}
	if !(f.Pts == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesForumTopics from given interface.
func (f *MessagesForumTopics) FillFrom(from interface {
	GetOrderByCreateDate() (value bool)
	GetCount() (value int)
	GetTopics() (value []ForumTopicClass)
	GetMessages() (value []MessageClass)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
	GetPts() (value int)
}) {
	f.OrderByCreateDate = from.GetOrderByCreateDate()
	f.Count = from.GetCount()
	f.Topics = from.GetTopics()
	f.Messages = from.GetMessages()
	f.Chats = from.GetChats()
	f.Users = from.GetUsers()
	f.Pts = from.GetPts()
}

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

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

// TypeInfo returns info about TL type.
func (f *MessagesForumTopics) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.forumTopics",
		ID:   MessagesForumTopicsTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OrderByCreateDate",
			SchemaName: "order_by_create_date",
			Null:       !f.Flags.Has(0),
		},
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "Topics",
			SchemaName: "topics",
		},
		{
			Name:       "Messages",
			SchemaName: "messages",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
		{
			Name:       "Pts",
			SchemaName: "pts",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (f *MessagesForumTopics) SetFlags() {
	if !(f.OrderByCreateDate == false) {
		f.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (f *MessagesForumTopics) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode messages.forumTopics#367617d3 as nil")
	}
	b.PutID(MessagesForumTopicsTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *MessagesForumTopics) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode messages.forumTopics#367617d3 as nil")
	}
	f.SetFlags()
	if err := f.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.forumTopics#367617d3: field flags: %w", err)
	}
	b.PutInt(f.Count)
	b.PutVectorHeader(len(f.Topics))
	for idx, v := range f.Topics {
		if v == nil {
			return fmt.Errorf("unable to encode messages.forumTopics#367617d3: field topics element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.forumTopics#367617d3: field topics element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(f.Messages))
	for idx, v := range f.Messages {
		if v == nil {
			return fmt.Errorf("unable to encode messages.forumTopics#367617d3: field messages element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.forumTopics#367617d3: field messages element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(f.Chats))
	for idx, v := range f.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode messages.forumTopics#367617d3: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.forumTopics#367617d3: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(f.Users))
	for idx, v := range f.Users {
		if v == nil {
			return fmt.Errorf("unable to encode messages.forumTopics#367617d3: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.forumTopics#367617d3: field users element with index %d: %w", idx, err)
		}
	}
	b.PutInt(f.Pts)
	return nil
}

// Decode implements bin.Decoder.
func (f *MessagesForumTopics) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode messages.forumTopics#367617d3 to nil")
	}
	if err := b.ConsumeID(MessagesForumTopicsTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.forumTopics#367617d3: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *MessagesForumTopics) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode messages.forumTopics#367617d3 to nil")
	}
	{
		if err := f.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.forumTopics#367617d3: field flags: %w", err)
		}
	}
	f.OrderByCreateDate = f.Flags.Has(0)
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.forumTopics#367617d3: field count: %w", err)
		}
		f.Count = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.forumTopics#367617d3: field topics: %w", err)
		}

		if headerLen > 0 {
			f.Topics = make([]ForumTopicClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeForumTopic(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.forumTopics#367617d3: field topics: %w", err)
			}
			f.Topics = append(f.Topics, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.forumTopics#367617d3: field messages: %w", err)
		}

		if headerLen > 0 {
			f.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.forumTopics#367617d3: field messages: %w", err)
			}
			f.Messages = append(f.Messages, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.forumTopics#367617d3: field chats: %w", err)
		}

		if headerLen > 0 {
			f.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.forumTopics#367617d3: field chats: %w", err)
			}
			f.Chats = append(f.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.forumTopics#367617d3: field users: %w", err)
		}

		if headerLen > 0 {
			f.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.forumTopics#367617d3: field users: %w", err)
			}
			f.Users = append(f.Users, value)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.forumTopics#367617d3: field pts: %w", err)
		}
		f.Pts = value
	}
	return nil
}

// SetOrderByCreateDate sets value of OrderByCreateDate conditional field.
func (f *MessagesForumTopics) SetOrderByCreateDate(value bool) {
	if value {
		f.Flags.Set(0)
		f.OrderByCreateDate = true
	} else {
		f.Flags.Unset(0)
		f.OrderByCreateDate = false
	}
}

// GetOrderByCreateDate returns value of OrderByCreateDate conditional field.
func (f *MessagesForumTopics) GetOrderByCreateDate() (value bool) {
	if f == nil {
		return
	}
	return f.Flags.Has(0)
}

// GetCount returns value of Count field.
func (f *MessagesForumTopics) GetCount() (value int) {
	if f == nil {
		return
	}
	return f.Count
}

// GetTopics returns value of Topics field.
func (f *MessagesForumTopics) GetTopics() (value []ForumTopicClass) {
	if f == nil {
		return
	}
	return f.Topics
}

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

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

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

// GetPts returns value of Pts field.
func (f *MessagesForumTopics) GetPts() (value int) {
	if f == nil {
		return
	}
	return f.Pts
}

// MapTopics returns field Topics wrapped in ForumTopicClassArray helper.
func (f *MessagesForumTopics) MapTopics() (value ForumTopicClassArray) {
	return ForumTopicClassArray(f.Topics)
}

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

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

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