/// class PD.Manage.Modules.RecycleBinRestoreCommand

PD.Manage.Modules.RecycleBinRestoreCommand =
PD.Commands.Command.extend({

    //===================================
    // PUBLIC METHODS
    //===================================

    setDefaults: function () {
        this.props("id", "recycle-bin/restore");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("clipboard_checks.png"));
        this.props("label", "Restore");
        this.props("tooltip", "Restore the selected Entities.");
        this.state("recycleBinItems", null);
    },

    setState: function (state) {
        this._super(state);
        this.props("enabled", this._isEnabled());
    },

    execute: function (e) {
        this._super(e);

        let ids = _.map(this._selectedRecycleBinItems(), "id");

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            allowClose: true,
            title: "Restore Recycle Bin Items",
            message: "Restore the selected items from the Recycle Bin?",
            select: (e) => {
                e.close();
                if (e.button === Jade.UI.MessageDialogButton.ok) {
                    this._restoreFromRecycleBin(ids);
                }
            }
        });

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _isEnabled: function () {
        return this._selectedRecycleBinItems().length > 0;
    },

    _selectedRecycleBinItems: function () {

        var result = [];

        if (_.isArray(this.state("recycleBinItems"))) {
            result = this.state("recycleBinItems");
        }

        return result;

    },

    _restoreFromRecycleBin: function (ids) {

        PDJS.Shared.TaskStatusDialog.open({
            title: "Restore from Recycle Bin",
            taskStart: (e) => {

                PD.ajax({
                    type: "POST",
                    url: PD.Path.resolve("~/Manage/RecycleBin/Restore/"),
                    data: {
                        ids: ids
                    },
                    success: (e2) => {
                        e.startPolling(e2);
                    },
                    error: (e2) => {
                        e.abort(e2.responseJSON);
                        e2.errorHandled = true;
                    }
                });

            },
            taskSuccess: (e) => {

                PD.context
                    .get_default()
                    .get_events()
                    .publish("recycle-bin/restore");

            }
        });

    }

});