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

        Jade.UI.MultipleChoiceDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            type: Jade.UI.MultipleChoiceDialogType.Radio,
            title: "Change Status",
            header: "What status would you like to assign to the selected entities?",
            items: [
                { title: "Set to Active", message: `Entities that are set to an "active" state will be made available to end users of the system, subject to security settings.`, value: 1, selected: true },
                { title: "Set to Inactive", message: `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.`, value: 2 }
            ],
            select: (e) => {
                e.close();
                if (e.button == Jade.UI.MessageDialogButton.ok) {
                    if (e.selected.length == 1) {
                        this._showTaskDialog(ids, e.selected[0]);
                    }
                }
            }
        });

    },

    _showTaskDialog: function (ids, status) {


        PDJS.Shared.TaskStatusDialog.open({
            title: "Change Status",
            enableClose: false,
            taskStart: (e) => {

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

            },
            taskSuccess: (e) => {

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

                e.close();

            }
        });

    }

});