/// class PD.Manage.Modules.Entities.EntityDeleteCommand
// PD.Commands.Command

PD.Manage.Modules.Entities.EntityDeleteCommand =
PD.Commands.Command.extend({

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

    setDefaults: function () {

        this.props("id", "entity/delete");
        this.props("label", "Send to Recycle Bin");
        this.props("icon", PD.Path.to16x16("garbage.png"));
        this.props("tooltip", "Send the selected Entities to the Recycle Bin");

        this.state("entities", null);

    },

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

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

        var entities = this._getEntities();

        this._showConfirmationDialog(_.map(entities, "id"));

    },

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

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

    _getEntities: function () {

        var result = [],
            entities = this.state("entities");

        if (_.isArray(entities) && entities.length > 0) {
            result = entities;
        }

        return result;

    },

    _showConfirmationDialog: function (ids) {

        var that = this;

        PD.Web.UI.MessageDialog.warn({
            title: "Send to Recycle Bin",
            message: "Send the selected entities to the Recycle Bin?",
            ok: function (e) {
                e.close();
                that._showTaskDialog(ids);
            }
        });

    },

    _showTaskDialog: function (ids) {

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

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

            },
            close: function () {

                PD.context
                    .get_default()
                    .get_events()
                    .publish("entity/delete", {
                        ids: ids
                    });

            }
        });

    }

});