/// 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);

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

        PD.Web.UI.MessageDialog.warn({
            title: "Restore Recycle Bin Items",
            message: "Restore the selected items from the Recycle Bin?",
            ok: function (e) {
                e.close();
                that._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) {

        PD.Shared.Dialogs.TaskStatus.open({
            config: {
                title: "Restore from Recycle Bin"
            },
            showDetail: true,
            taskStart: function (args) {

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

            },
            taskSuccess: function (e) {
                PD.context.get_default().get_events().publish("recycle-bin/restore");

            },
            close: function (e) {

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

            }
        });

    }

});