(function() {
    let FileExplorerTool_DeleteCustom = function(fe) {
        if (!(this instanceof FileExplorerTool_DeleteCustom))  return new FileExplorerTool_DeleteCustom(fe);

        let enabled = false;
        let node = fe.AddToolbarButton('fe_fileexplorer_folder_tool_delete', fe.Translate('Delete'));

        let DeleteHandler = function (fe) {
            let paths = fe.GetCurrentFolder().GetPath();
            let path = "";
            let entries = fe.GetSelectedFolderEntries();

            paths.forEach(function (item) {
                path += item[0] + "/";
            });

            entries.forEach(function (entry) {
                let theFile = entry.name;
                let xhr = new fe.PrepareXHR({
                    url: '/cgi-cpn/prontus_edit_borrar.cgi',
                    params: {
                        _prontus_id: $("body").data("prontus-id"),
                        _path_file: (entry.type == "folder" ? "" : path + theFile),
                        _curr_dir: (entry.type == "folder" ? path + theFile : ""),
                        type_item: (entry.type == "folder" ? "dir" : "file")
                    },
                    onsuccess: function(e) {
                        let data = JSON.parse(e.target.response);

                        if (data.status == 1) {
                            fe.RefreshFolders(true);
                        } else {
                            showModalAlert({
                                title: 'Error al eliminar',
                                type: 'red',
                                msg: data.msg
                            });
                        }

                    },
                    onerror: function(e) {
                        showModalAlert({
                            title: 'Error al eliminar',
                            type: 'red',
                            msg: "Ocurrió un error al tratar de eliminar el archivo."
                        });
                    }
                });

                xhr.Send();
            });
        };

        // Handle clicks.
        let ClickHandler = function(e) {
            if (enabled) {
                showModalConfirm({
                    msg: "Se eliminará el archivo y no se podrá recuperar. ¿Estás seguro?",
                    type: "blue",
                    icon: "glyphicon glyphicon-question-sign",
                    okTitle: "Eliminar archivo",
                    cancelTitle: "Cancelar",
                    btnClassOk: 'hovwhite radius15 noborder txtnextwhite pad-top-bot7 pad-lef-rig20 int-nextred nextred',
                    cbBtnOK: function () {
                        DeleteHandler(fe);
                    },
                    cbBtnCancel: function () {
                    }
                });
            }
        };

        node.addEventListener('click', ClickHandler);

        let UpdateToolHandler = function(currfolder, attrs) {
            let prevenabled = enabled;

            enabled = (!currfolder.waiting && fe.GetNumSelectedItems());

            // Limite de archivos seleccionados a borrar.
            if (fe.GetNumSelectedItems() > 5) enabled = false;

            if (enabled) {
                let selectedFolderEntries = fe.GetSelectedFolderEntries();

                if (selectedFolderEntries[0].type == "folder") {
                    if (selectedFolderEntries[0].empty == 1) {
                        enabled = true;
                    } else {
                        enabled = false;
                    }
                }
            }

            if (prevenabled !== enabled) {
                if (enabled) {
                    node.classList.remove('fe_fileexplorer_disabled');
                } else {
                    node.classList.add('fe_fileexplorer_disabled');
                }

                fe.ToolStateUpdated();
            }
        };

        fe.addEventListener('update_tool', UpdateToolHandler);

        let DestroyToolHandler = function() {
            node.removeEventListener('click', ClickHandler);
        };

        fe.addEventListener('destroy', DestroyToolHandler);
    };

    window.FileExplorer.RegisterTool(1, FileExplorerTool_DeleteCustom);
})();