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

// CreateChatFolderRequest represents TL type `createChatFolder#3c85c500`.
type CreateChatFolderRequest struct {
	// The new chat folder
	Folder ChatFolder
}

// CreateChatFolderRequestTypeID is TL type id of CreateChatFolderRequest.
const CreateChatFolderRequestTypeID = 0x3c85c500

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

func (c *CreateChatFolderRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Folder.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CreateChatFolderRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "createChatFolder",
		ID:   CreateChatFolderRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Folder",
			SchemaName: "folder",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CreateChatFolderRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createChatFolder#3c85c500 as nil")
	}
	b.PutID(CreateChatFolderRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CreateChatFolderRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createChatFolder#3c85c500 as nil")
	}
	if err := c.Folder.Encode(b); err != nil {
		return fmt.Errorf("unable to encode createChatFolder#3c85c500: field folder: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *CreateChatFolderRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createChatFolder#3c85c500 to nil")
	}
	{
		if err := c.Folder.Decode(b); err != nil {
			return fmt.Errorf("unable to decode createChatFolder#3c85c500: field folder: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CreateChatFolderRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode createChatFolder#3c85c500 as nil")
	}
	b.ObjStart()
	b.PutID("createChatFolder")
	b.Comma()
	b.FieldStart("folder")
	if err := c.Folder.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode createChatFolder#3c85c500: field folder: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetFolder returns value of Folder field.
func (c *CreateChatFolderRequest) GetFolder() (value ChatFolder) {
	if c == nil {
		return
	}
	return c.Folder
}

// CreateChatFolder invokes method createChatFolder#3c85c500 returning error if any.
func (c *Client) CreateChatFolder(ctx context.Context, folder ChatFolder) (*ChatFolderInfo, error) {
	var result ChatFolderInfo

	request := &CreateChatFolderRequest{
		Folder: folder,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
