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

// AccountChatThemesNotModified represents TL type `account.chatThemesNotModified#e011e1c4`.
// The available chat themes were not modified
//
// See https://core.telegram.org/constructor/account.chatThemesNotModified for reference.
type AccountChatThemesNotModified struct {
}

// AccountChatThemesNotModifiedTypeID is TL type id of AccountChatThemesNotModified.
const AccountChatThemesNotModifiedTypeID = 0xe011e1c4

// construct implements constructor of AccountChatThemesClass.
func (c AccountChatThemesNotModified) construct() AccountChatThemesClass { return &c }

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

	_ AccountChatThemesClass = &AccountChatThemesNotModified{}
)

func (c *AccountChatThemesNotModified) Zero() bool {
	if c == nil {
		return true
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*AccountChatThemesNotModified) TypeName() string {
	return "account.chatThemesNotModified"
}

// TypeInfo returns info about TL type.
func (c *AccountChatThemesNotModified) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.chatThemesNotModified",
		ID:   AccountChatThemesNotModifiedTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (c *AccountChatThemesNotModified) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode account.chatThemesNotModified#e011e1c4 as nil")
	}
	b.PutID(AccountChatThemesNotModifiedTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *AccountChatThemesNotModified) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode account.chatThemesNotModified#e011e1c4 as nil")
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *AccountChatThemesNotModified) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode account.chatThemesNotModified#e011e1c4 to nil")
	}
	return nil
}

// AccountChatThemes represents TL type `account.chatThemes#be098173`.
// Available chat themes¹
//
// Links:
//  1. https://core.telegram.org/api/themes#chat-themes
//
// See https://core.telegram.org/constructor/account.chatThemes for reference.
type AccountChatThemes struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Hash to pass to the method that returned this constructor, to avoid refetching the
	// result if it hasn't changed.
	Hash int64
	// Themes.
	Themes []ChatThemeClass
	// Chats mentioned in the themes field.
	Chats []ChatClass
	// Users mentioned in the themes field.
	Users []UserClass
	// Next offset for pagination.
	//
	// Use SetNextOffset and GetNextOffset helpers.
	NextOffset string
}

// AccountChatThemesTypeID is TL type id of AccountChatThemes.
const AccountChatThemesTypeID = 0xbe098173

// construct implements constructor of AccountChatThemesClass.
func (c AccountChatThemes) construct() AccountChatThemesClass { return &c }

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

	_ AccountChatThemesClass = &AccountChatThemes{}
)

func (c *AccountChatThemes) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.Hash == 0) {
		return false
	}
	if !(c.Themes == nil) {
		return false
	}
	if !(c.Chats == nil) {
		return false
	}
	if !(c.Users == nil) {
		return false
	}
	if !(c.NextOffset == "") {
		return false
	}

	return true
}

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

// FillFrom fills AccountChatThemes from given interface.
func (c *AccountChatThemes) FillFrom(from interface {
	GetHash() (value int64)
	GetThemes() (value []ChatThemeClass)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
	GetNextOffset() (value string, ok bool)
}) {
	c.Hash = from.GetHash()
	c.Themes = from.GetThemes()
	c.Chats = from.GetChats()
	c.Users = from.GetUsers()
	if val, ok := from.GetNextOffset(); ok {
		c.NextOffset = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*AccountChatThemes) TypeName() string {
	return "account.chatThemes"
}

// TypeInfo returns info about TL type.
func (c *AccountChatThemes) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.chatThemes",
		ID:   AccountChatThemesTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
		{
			Name:       "Themes",
			SchemaName: "themes",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
			Null:       !c.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *AccountChatThemes) SetFlags() {
	if !(c.NextOffset == "") {
		c.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (c *AccountChatThemes) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode account.chatThemes#be098173 as nil")
	}
	b.PutID(AccountChatThemesTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *AccountChatThemes) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode account.chatThemes#be098173 as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.chatThemes#be098173: field flags: %w", err)
	}
	b.PutLong(c.Hash)
	b.PutVectorHeader(len(c.Themes))
	for idx, v := range c.Themes {
		if v == nil {
			return fmt.Errorf("unable to encode account.chatThemes#be098173: field themes element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.chatThemes#be098173: field themes element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(c.Chats))
	for idx, v := range c.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode account.chatThemes#be098173: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.chatThemes#be098173: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(c.Users))
	for idx, v := range c.Users {
		if v == nil {
			return fmt.Errorf("unable to encode account.chatThemes#be098173: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.chatThemes#be098173: field users element with index %d: %w", idx, err)
		}
	}
	if c.Flags.Has(0) {
		b.PutString(c.NextOffset)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *AccountChatThemes) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode account.chatThemes#be098173 to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.chatThemes#be098173: field flags: %w", err)
		}
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode account.chatThemes#be098173: field hash: %w", err)
		}
		c.Hash = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode account.chatThemes#be098173: field themes: %w", err)
		}

		if headerLen > 0 {
			c.Themes = make([]ChatThemeClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChatTheme(b)
			if err != nil {
				return fmt.Errorf("unable to decode account.chatThemes#be098173: field themes: %w", err)
			}
			c.Themes = append(c.Themes, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode account.chatThemes#be098173: field chats: %w", err)
		}

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

		if headerLen > 0 {
			c.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 account.chatThemes#be098173: field users: %w", err)
			}
			c.Users = append(c.Users, value)
		}
	}
	if c.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode account.chatThemes#be098173: field next_offset: %w", err)
		}
		c.NextOffset = value
	}
	return nil
}

// GetHash returns value of Hash field.
func (c *AccountChatThemes) GetHash() (value int64) {
	if c == nil {
		return
	}
	return c.Hash
}

// GetThemes returns value of Themes field.
func (c *AccountChatThemes) GetThemes() (value []ChatThemeClass) {
	if c == nil {
		return
	}
	return c.Themes
}

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

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

// SetNextOffset sets value of NextOffset conditional field.
func (c *AccountChatThemes) SetNextOffset(value string) {
	c.Flags.Set(0)
	c.NextOffset = value
}

// GetNextOffset returns value of NextOffset conditional field and
// boolean which is true if field was set.
func (c *AccountChatThemes) GetNextOffset() (value string, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(0) {
		return value, false
	}
	return c.NextOffset, true
}

// MapThemes returns field Themes wrapped in ChatThemeClassArray helper.
func (c *AccountChatThemes) MapThemes() (value ChatThemeClassArray) {
	return ChatThemeClassArray(c.Themes)
}

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

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

// AccountChatThemesClassName is schema name of AccountChatThemesClass.
const AccountChatThemesClassName = "account.ChatThemes"

// AccountChatThemesClass represents account.ChatThemes generic type.
//
// See https://core.telegram.org/type/account.ChatThemes for reference.
//
// Example:
//
//	g, err := tg.DecodeAccountChatThemes(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.AccountChatThemesNotModified: // account.chatThemesNotModified#e011e1c4
//	case *tg.AccountChatThemes: // account.chatThemes#be098173
//	default: panic(v)
//	}
type AccountChatThemesClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() AccountChatThemesClass

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

	// AsModified tries to map AccountChatThemesClass to AccountChatThemes.
	AsModified() (*AccountChatThemes, bool)
}

// AsModified tries to map AccountChatThemesNotModified to AccountChatThemes.
func (c *AccountChatThemesNotModified) AsModified() (*AccountChatThemes, bool) {
	return nil, false
}

// AsModified tries to map AccountChatThemes to AccountChatThemes.
func (c *AccountChatThemes) AsModified() (*AccountChatThemes, bool) {
	return c, true
}

// DecodeAccountChatThemes implements binary de-serialization for AccountChatThemesClass.
func DecodeAccountChatThemes(buf *bin.Buffer) (AccountChatThemesClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case AccountChatThemesNotModifiedTypeID:
		// Decoding account.chatThemesNotModified#e011e1c4.
		v := AccountChatThemesNotModified{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AccountChatThemesClass: %w", err)
		}
		return &v, nil
	case AccountChatThemesTypeID:
		// Decoding account.chatThemes#be098173.
		v := AccountChatThemes{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AccountChatThemesClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode AccountChatThemesClass: %w", bin.NewUnexpectedID(id))
	}
}

// AccountChatThemes boxes the AccountChatThemesClass providing a helper.
type AccountChatThemesBox struct {
	ChatThemes AccountChatThemesClass
}

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

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