/// class PD.Manage.Modules.RecycleBinDeleteCommand

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

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

    setDefaults: function () {
        this.props("id", "recycle-bin/delete");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("label", "Delete");
        this.props("tooltip", "Permanently delete 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.Manage.Modules.RecycleBinDeleteConfirmationDialog.show({
            callback: function (e) {
                e.close();
                that._deleteFromRecycleBin(ids, e.value === "delete");
            }
        });

    },

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

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

    _selectedRecycleBinItems: function () {

        var result = [];

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

        return result;

    },

    _deleteFromRecycleBin: function (ids, deleteReferences) {

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

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

            },
            close: function (e) {

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

            }
        });

    }

});