/// class PD.Manage.Modules.Entities.EntityChangeStatusCommand

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

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

    setDefaults: function () {

        this.props("id", "entity/change-status");
        this.props("icon", PD.Path.to16x16("contrast.png"));
        this.props("label", "Change Status");
        this.props("tooltip", "Change the Status of the selected Entities.");

        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._showOptionDialog(_.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;

    },

    _showOptionDialog: function (ids) {

        var that = this;

        PD.Web.UI.RadioDialog.warn({
            title: "Change Status",
            message: "What status would you like to assign to the selected entities?",
            ok: function (e) {
                e.close();
                that._showTaskDialog(ids, e.value);
            },
            radios: [
                { value: 1, label: "<b>Set to Active</b><br/>Entities that are set to an \"active\" state will be made available to end users of the system, subject to security settings." },
                { value: 2, label: "<b>Set to Inactive</b><br/>Entities that are set to an \"inactive\" state will no longer be accessible to end users of the system and will only be accessible to users granted manage permissions to the entity type." },
            ]
        });

    },

    _showTaskDialog: function (ids, status) {

        PD.Shared.Dialogs.TaskStatus.open({
            config: {
                title: "Change Status"
            },
            showDetail: true,
            taskStart: function (args) {

                PD.ajax({
                    type: "POST",
                    url: PD.Path.resolve("~/Manage/EntityStatus/Change"),
                    data: {
                        ids: ids,
                        status: status
                    },
                    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/change-status", {
                        ids: ids
                    });

            }
        });

    }

});