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

// ChatFolder represents TL type `chatFolder#a2e17d38`.
type ChatFolder struct {
	// The name of the folder
	Name ChatFolderName
	// The chosen icon for the chat folder; may be null. If null, use
	// getChatFolderDefaultIconName to get default icon name for the folder
	Icon ChatFolderIcon
	// The identifier of the chosen color for the chat folder icon; from -1 to 6. If -1, then
	// color is disabled. Can't be changed if folder tags are disabled or the current user
	// doesn't have Telegram Premium subscription
	ColorID int32
	// True, if at least one link has been created for the folder
	IsShareable bool
	// The chat identifiers of pinned chats in the folder. There can be up to
	// getOption("chat_folder_chosen_chat_count_max") pinned and always included non-secret
	// chats and the same number of secret chats, but the limit can be increased with
	// Telegram Premium
	PinnedChatIDs []int64
	// The chat identifiers of always included chats in the folder. There can be up to
	// getOption("chat_folder_chosen_chat_count_max") pinned and always included non-secret
	// chats and the same number of secret chats, but the limit can be increased with
	// Telegram Premium
	IncludedChatIDs []int64
	// The chat identifiers of always excluded chats in the folder. There can be up to
	// getOption("chat_folder_chosen_chat_count_max") always excluded non-secret chats and
	// the same number of secret chats, but the limit can be increased with Telegram Premium
	ExcludedChatIDs []int64
	// True, if muted chats need to be excluded
	ExcludeMuted bool
	// True, if read chats need to be excluded
	ExcludeRead bool
	// True, if archived chats need to be excluded
	ExcludeArchived bool
	// True, if contacts need to be included
	IncludeContacts bool
	// True, if non-contact users need to be included
	IncludeNonContacts bool
	// True, if bots need to be included
	IncludeBots bool
	// True, if basic groups and supergroups need to be included
	IncludeGroups bool
	// True, if channels need to be included
	IncludeChannels bool
}

// ChatFolderTypeID is TL type id of ChatFolder.
const ChatFolderTypeID = 0xa2e17d38

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

func (c *ChatFolder) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Name.Zero()) {
		return false
	}
	if !(c.Icon.Zero()) {
		return false
	}
	if !(c.ColorID == 0) {
		return false
	}
	if !(c.IsShareable == false) {
		return false
	}
	if !(c.PinnedChatIDs == nil) {
		return false
	}
	if !(c.IncludedChatIDs == nil) {
		return false
	}
	if !(c.ExcludedChatIDs == nil) {
		return false
	}
	if !(c.ExcludeMuted == false) {
		return false
	}
	if !(c.ExcludeRead == false) {
		return false
	}
	if !(c.ExcludeArchived == false) {
		return false
	}
	if !(c.IncludeContacts == false) {
		return false
	}
	if !(c.IncludeNonContacts == false) {
		return false
	}
	if !(c.IncludeBots == false) {
		return false
	}
	if !(c.IncludeGroups == false) {
		return false
	}
	if !(c.IncludeChannels == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatFolder) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatFolder",
		ID:   ChatFolderTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "Icon",
			SchemaName: "icon",
		},
		{
			Name:       "ColorID",
			SchemaName: "color_id",
		},
		{
			Name:       "IsShareable",
			SchemaName: "is_shareable",
		},
		{
			Name:       "PinnedChatIDs",
			SchemaName: "pinned_chat_ids",
		},
		{
			Name:       "IncludedChatIDs",
			SchemaName: "included_chat_ids",
		},
		{
			Name:       "ExcludedChatIDs",
			SchemaName: "excluded_chat_ids",
		},
		{
			Name:       "ExcludeMuted",
			SchemaName: "exclude_muted",
		},
		{
			Name:       "ExcludeRead",
			SchemaName: "exclude_read",
		},
		{
			Name:       "ExcludeArchived",
			SchemaName: "exclude_archived",
		},
		{
			Name:       "IncludeContacts",
			SchemaName: "include_contacts",
		},
		{
			Name:       "IncludeNonContacts",
			SchemaName: "include_non_contacts",
		},
		{
			Name:       "IncludeBots",
			SchemaName: "include_bots",
		},
		{
			Name:       "IncludeGroups",
			SchemaName: "include_groups",
		},
		{
			Name:       "IncludeChannels",
			SchemaName: "include_channels",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatFolder) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatFolder#a2e17d38 as nil")
	}
	b.PutID(ChatFolderTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatFolder) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatFolder#a2e17d38 as nil")
	}
	if err := c.Name.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatFolder#a2e17d38: field name: %w", err)
	}
	if err := c.Icon.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatFolder#a2e17d38: field icon: %w", err)
	}
	b.PutInt32(c.ColorID)
	b.PutBool(c.IsShareable)
	b.PutInt(len(c.PinnedChatIDs))
	for _, v := range c.PinnedChatIDs {
		b.PutInt53(v)
	}
	b.PutInt(len(c.IncludedChatIDs))
	for _, v := range c.IncludedChatIDs {
		b.PutInt53(v)
	}
	b.PutInt(len(c.ExcludedChatIDs))
	for _, v := range c.ExcludedChatIDs {
		b.PutInt53(v)
	}
	b.PutBool(c.ExcludeMuted)
	b.PutBool(c.ExcludeRead)
	b.PutBool(c.ExcludeArchived)
	b.PutBool(c.IncludeContacts)
	b.PutBool(c.IncludeNonContacts)
	b.PutBool(c.IncludeBots)
	b.PutBool(c.IncludeGroups)
	b.PutBool(c.IncludeChannels)
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatFolder) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatFolder#a2e17d38 to nil")
	}
	if err := b.ConsumeID(ChatFolderTypeID); err != nil {
		return fmt.Errorf("unable to decode chatFolder#a2e17d38: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatFolder) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatFolder#a2e17d38 to nil")
	}
	{
		if err := c.Name.Decode(b); err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field name: %w", err)
		}
	}
	{
		if err := c.Icon.Decode(b); err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field icon: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field color_id: %w", err)
		}
		c.ColorID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field is_shareable: %w", err)
		}
		c.IsShareable = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field pinned_chat_ids: %w", err)
		}

		if headerLen > 0 {
			c.PinnedChatIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field pinned_chat_ids: %w", err)
			}
			c.PinnedChatIDs = append(c.PinnedChatIDs, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field included_chat_ids: %w", err)
		}

		if headerLen > 0 {
			c.IncludedChatIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field included_chat_ids: %w", err)
			}
			c.IncludedChatIDs = append(c.IncludedChatIDs, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field excluded_chat_ids: %w", err)
		}

		if headerLen > 0 {
			c.ExcludedChatIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field excluded_chat_ids: %w", err)
			}
			c.ExcludedChatIDs = append(c.ExcludedChatIDs, value)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field exclude_muted: %w", err)
		}
		c.ExcludeMuted = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field exclude_read: %w", err)
		}
		c.ExcludeRead = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field exclude_archived: %w", err)
		}
		c.ExcludeArchived = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field include_contacts: %w", err)
		}
		c.IncludeContacts = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field include_non_contacts: %w", err)
		}
		c.IncludeNonContacts = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field include_bots: %w", err)
		}
		c.IncludeBots = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field include_groups: %w", err)
		}
		c.IncludeGroups = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolder#a2e17d38: field include_channels: %w", err)
		}
		c.IncludeChannels = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatFolder) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatFolder#a2e17d38 as nil")
	}
	b.ObjStart()
	b.PutID("chatFolder")
	b.Comma()
	b.FieldStart("name")
	if err := c.Name.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatFolder#a2e17d38: field name: %w", err)
	}
	b.Comma()
	b.FieldStart("icon")
	if err := c.Icon.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatFolder#a2e17d38: field icon: %w", err)
	}
	b.Comma()
	b.FieldStart("color_id")
	b.PutInt32(c.ColorID)
	b.Comma()
	b.FieldStart("is_shareable")
	b.PutBool(c.IsShareable)
	b.Comma()
	b.FieldStart("pinned_chat_ids")
	b.ArrStart()
	for _, v := range c.PinnedChatIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("included_chat_ids")
	b.ArrStart()
	for _, v := range c.IncludedChatIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("excluded_chat_ids")
	b.ArrStart()
	for _, v := range c.ExcludedChatIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("exclude_muted")
	b.PutBool(c.ExcludeMuted)
	b.Comma()
	b.FieldStart("exclude_read")
	b.PutBool(c.ExcludeRead)
	b.Comma()
	b.FieldStart("exclude_archived")
	b.PutBool(c.ExcludeArchived)
	b.Comma()
	b.FieldStart("include_contacts")
	b.PutBool(c.IncludeContacts)
	b.Comma()
	b.FieldStart("include_non_contacts")
	b.PutBool(c.IncludeNonContacts)
	b.Comma()
	b.FieldStart("include_bots")
	b.PutBool(c.IncludeBots)
	b.Comma()
	b.FieldStart("include_groups")
	b.PutBool(c.IncludeGroups)
	b.Comma()
	b.FieldStart("include_channels")
	b.PutBool(c.IncludeChannels)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ChatFolder) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatFolder#a2e17d38 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatFolder"); err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: %w", err)
			}
		case "name":
			if err := c.Name.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field name: %w", err)
			}
		case "icon":
			if err := c.Icon.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field icon: %w", err)
			}
		case "color_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field color_id: %w", err)
			}
			c.ColorID = value
		case "is_shareable":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field is_shareable: %w", err)
			}
			c.IsShareable = value
		case "pinned_chat_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode chatFolder#a2e17d38: field pinned_chat_ids: %w", err)
				}
				c.PinnedChatIDs = append(c.PinnedChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field pinned_chat_ids: %w", err)
			}
		case "included_chat_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode chatFolder#a2e17d38: field included_chat_ids: %w", err)
				}
				c.IncludedChatIDs = append(c.IncludedChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field included_chat_ids: %w", err)
			}
		case "excluded_chat_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode chatFolder#a2e17d38: field excluded_chat_ids: %w", err)
				}
				c.ExcludedChatIDs = append(c.ExcludedChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field excluded_chat_ids: %w", err)
			}
		case "exclude_muted":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field exclude_muted: %w", err)
			}
			c.ExcludeMuted = value
		case "exclude_read":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field exclude_read: %w", err)
			}
			c.ExcludeRead = value
		case "exclude_archived":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field exclude_archived: %w", err)
			}
			c.ExcludeArchived = value
		case "include_contacts":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field include_contacts: %w", err)
			}
			c.IncludeContacts = value
		case "include_non_contacts":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field include_non_contacts: %w", err)
			}
			c.IncludeNonContacts = value
		case "include_bots":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field include_bots: %w", err)
			}
			c.IncludeBots = value
		case "include_groups":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field include_groups: %w", err)
			}
			c.IncludeGroups = value
		case "include_channels":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolder#a2e17d38: field include_channels: %w", err)
			}
			c.IncludeChannels = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetName returns value of Name field.
func (c *ChatFolder) GetName() (value ChatFolderName) {
	if c == nil {
		return
	}
	return c.Name
}

// GetIcon returns value of Icon field.
func (c *ChatFolder) GetIcon() (value ChatFolderIcon) {
	if c == nil {
		return
	}
	return c.Icon
}

// GetColorID returns value of ColorID field.
func (c *ChatFolder) GetColorID() (value int32) {
	if c == nil {
		return
	}
	return c.ColorID
}

// GetIsShareable returns value of IsShareable field.
func (c *ChatFolder) GetIsShareable() (value bool) {
	if c == nil {
		return
	}
	return c.IsShareable
}

// GetPinnedChatIDs returns value of PinnedChatIDs field.
func (c *ChatFolder) GetPinnedChatIDs() (value []int64) {
	if c == nil {
		return
	}
	return c.PinnedChatIDs
}

// GetIncludedChatIDs returns value of IncludedChatIDs field.
func (c *ChatFolder) GetIncludedChatIDs() (value []int64) {
	if c == nil {
		return
	}
	return c.IncludedChatIDs
}

// GetExcludedChatIDs returns value of ExcludedChatIDs field.
func (c *ChatFolder) GetExcludedChatIDs() (value []int64) {
	if c == nil {
		return
	}
	return c.ExcludedChatIDs
}

// GetExcludeMuted returns value of ExcludeMuted field.
func (c *ChatFolder) GetExcludeMuted() (value bool) {
	if c == nil {
		return
	}
	return c.ExcludeMuted
}

// GetExcludeRead returns value of ExcludeRead field.
func (c *ChatFolder) GetExcludeRead() (value bool) {
	if c == nil {
		return
	}
	return c.ExcludeRead
}

// GetExcludeArchived returns value of ExcludeArchived field.
func (c *ChatFolder) GetExcludeArchived() (value bool) {
	if c == nil {
		return
	}
	return c.ExcludeArchived
}

// GetIncludeContacts returns value of IncludeContacts field.
func (c *ChatFolder) GetIncludeContacts() (value bool) {
	if c == nil {
		return
	}
	return c.IncludeContacts
}

// GetIncludeNonContacts returns value of IncludeNonContacts field.
func (c *ChatFolder) GetIncludeNonContacts() (value bool) {
	if c == nil {
		return
	}
	return c.IncludeNonContacts
}

// GetIncludeBots returns value of IncludeBots field.
func (c *ChatFolder) GetIncludeBots() (value bool) {
	if c == nil {
		return
	}
	return c.IncludeBots
}

// GetIncludeGroups returns value of IncludeGroups field.
func (c *ChatFolder) GetIncludeGroups() (value bool) {
	if c == nil {
		return
	}
	return c.IncludeGroups
}

// GetIncludeChannels returns value of IncludeChannels field.
func (c *ChatFolder) GetIncludeChannels() (value bool) {
	if c == nil {
		return
	}
	return c.IncludeChannels
}
