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

// EditChatFolderRequest represents TL type `editChatFolder#332fb32`.
type EditChatFolderRequest struct {
	// Chat folder identifier
	ChatFolderID int32
	// The edited chat folder
	Folder ChatFolder
}

// EditChatFolderRequestTypeID is TL type id of EditChatFolderRequest.
const EditChatFolderRequestTypeID = 0x332fb32

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

func (e *EditChatFolderRequest) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.ChatFolderID == 0) {
		return false
	}
	if !(e.Folder.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EditChatFolderRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "editChatFolder",
		ID:   EditChatFolderRequestTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatFolderID",
			SchemaName: "chat_folder_id",
		},
		{
			Name:       "Folder",
			SchemaName: "folder",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EditChatFolderRequest) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editChatFolder#332fb32 as nil")
	}
	b.PutID(EditChatFolderRequestTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EditChatFolderRequest) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editChatFolder#332fb32 as nil")
	}
	b.PutInt32(e.ChatFolderID)
	if err := e.Folder.Encode(b); err != nil {
		return fmt.Errorf("unable to encode editChatFolder#332fb32: field folder: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (e *EditChatFolderRequest) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode editChatFolder#332fb32 to nil")
	}
	if err := b.ConsumeID(EditChatFolderRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode editChatFolder#332fb32: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EditChatFolderRequest) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode editChatFolder#332fb32 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode editChatFolder#332fb32: field chat_folder_id: %w", err)
		}
		e.ChatFolderID = value
	}
	{
		if err := e.Folder.Decode(b); err != nil {
			return fmt.Errorf("unable to decode editChatFolder#332fb32: field folder: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EditChatFolderRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode editChatFolder#332fb32 as nil")
	}
	b.ObjStart()
	b.PutID("editChatFolder")
	b.Comma()
	b.FieldStart("chat_folder_id")
	b.PutInt32(e.ChatFolderID)
	b.Comma()
	b.FieldStart("folder")
	if err := e.Folder.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editChatFolder#332fb32: field folder: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EditChatFolderRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode editChatFolder#332fb32 to nil")
	}

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

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

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

// EditChatFolder invokes method editChatFolder#332fb32 returning error if any.
func (c *Client) EditChatFolder(ctx context.Context, request *EditChatFolderRequest) (*ChatFolderInfo, error) {
	var result ChatFolderInfo

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