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

// ChatListMain represents TL type `chatListMain#e8195bac`.
type ChatListMain struct {
}

// ChatListMainTypeID is TL type id of ChatListMain.
const ChatListMainTypeID = 0xe8195bac

// construct implements constructor of ChatListClass.
func (c ChatListMain) construct() ChatListClass { return &c }

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

	_ ChatListClass = &ChatListMain{}
)

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (c *ChatListMain) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatListMain#e8195bac as nil")
	}
	b.PutID(ChatListMainTypeID)
	return c.EncodeBare(b)
}

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

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

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatListMain) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatListMain#e8195bac as nil")
	}
	b.ObjStart()
	b.PutID("chatListMain")
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// ChatListArchive represents TL type `chatListArchive#159f6ec3`.
type ChatListArchive struct {
}

// ChatListArchiveTypeID is TL type id of ChatListArchive.
const ChatListArchiveTypeID = 0x159f6ec3

// construct implements constructor of ChatListClass.
func (c ChatListArchive) construct() ChatListClass { return &c }

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

	_ ChatListClass = &ChatListArchive{}
)

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (c *ChatListArchive) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatListArchive#159f6ec3 as nil")
	}
	b.PutID(ChatListArchiveTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatListArchive) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatListArchive#159f6ec3 as nil")
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatListArchive) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatListArchive#159f6ec3 to nil")
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatListArchive) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatListArchive#159f6ec3 as nil")
	}
	b.ObjStart()
	b.PutID("chatListArchive")
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatListArchive"); err != nil {
				return fmt.Errorf("unable to decode chatListArchive#159f6ec3: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// ChatListFolder represents TL type `chatListFolder#16fe3e58`.
type ChatListFolder struct {
	// Chat folder identifier
	ChatFolderID int32
}

// ChatListFolderTypeID is TL type id of ChatListFolder.
const ChatListFolderTypeID = 0x16fe3e58

// construct implements constructor of ChatListClass.
func (c ChatListFolder) construct() ChatListClass { return &c }

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

	_ ChatListClass = &ChatListFolder{}
)

func (c *ChatListFolder) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.ChatFolderID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatListFolder) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatListFolder",
		ID:   ChatListFolderTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatFolderID",
			SchemaName: "chat_folder_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatListFolder) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatListFolder#16fe3e58 as nil")
	}
	b.PutID(ChatListFolderTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatListFolder) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatListFolder#16fe3e58 as nil")
	}
	b.PutInt32(c.ChatFolderID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatListFolder) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatListFolder#16fe3e58 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chatListFolder#16fe3e58: field chat_folder_id: %w", err)
		}
		c.ChatFolderID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatListFolder) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatListFolder#16fe3e58 as nil")
	}
	b.ObjStart()
	b.PutID("chatListFolder")
	b.Comma()
	b.FieldStart("chat_folder_id")
	b.PutInt32(c.ChatFolderID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatListFolder"); err != nil {
				return fmt.Errorf("unable to decode chatListFolder#16fe3e58: %w", err)
			}
		case "chat_folder_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatListFolder#16fe3e58: field chat_folder_id: %w", err)
			}
			c.ChatFolderID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatFolderID returns value of ChatFolderID field.
func (c *ChatListFolder) GetChatFolderID() (value int32) {
	if c == nil {
		return
	}
	return c.ChatFolderID
}

// ChatListClassName is schema name of ChatListClass.
const ChatListClassName = "ChatList"

// ChatListClass represents ChatList generic type.
//
// Example:
//
//	g, err := tdapi.DecodeChatList(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.ChatListMain: // chatListMain#e8195bac
//	case *tdapi.ChatListArchive: // chatListArchive#159f6ec3
//	case *tdapi.ChatListFolder: // chatListFolder#16fe3e58
//	default: panic(v)
//	}
type ChatListClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() ChatListClass

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

// DecodeChatList implements binary de-serialization for ChatListClass.
func DecodeChatList(buf *bin.Buffer) (ChatListClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case ChatListMainTypeID:
		// Decoding chatListMain#e8195bac.
		v := ChatListMain{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatListClass: %w", err)
		}
		return &v, nil
	case ChatListArchiveTypeID:
		// Decoding chatListArchive#159f6ec3.
		v := ChatListArchive{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatListClass: %w", err)
		}
		return &v, nil
	case ChatListFolderTypeID:
		// Decoding chatListFolder#16fe3e58.
		v := ChatListFolder{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatListClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode ChatListClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONChatList implements binary de-serialization for ChatListClass.
func DecodeTDLibJSONChatList(buf tdjson.Decoder) (ChatListClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "chatListMain":
		// Decoding chatListMain#e8195bac.
		v := ChatListMain{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatListClass: %w", err)
		}
		return &v, nil
	case "chatListArchive":
		// Decoding chatListArchive#159f6ec3.
		v := ChatListArchive{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatListClass: %w", err)
		}
		return &v, nil
	case "chatListFolder":
		// Decoding chatListFolder#16fe3e58.
		v := ChatListFolder{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatListClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode ChatListClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// ChatList boxes the ChatListClass providing a helper.
type ChatListBox struct {
	ChatList ChatListClass
}

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

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

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

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