/// class PD.Manage.Modules.SystemVariables.SystemVariableEditCommand

PD.Manage.Modules.SystemVariables.SystemVariableEditCommand =
PD.Commands.Command.extend({

    //===================================
    // PROPERTIES
    //===================================

    get_tags: function () {
        return this.state("tags") || [];
    },

    get_tag: function () {
        return _.first(this.get_tags());
    },

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

    setDefaults: function () {
        this.props("id", "variable/edit");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("pencil.png"));
        this.props("label", "Edit");
        this.props("tooltip", "Edit the selected system variable.");
        this.state("tags", null);
    },

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

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

        var tag = this.get_tag();

        this._showDialog(tag.id);

    },

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

    _isEnabled: function () {
        return this.get_tags().length === 1;
    },

    _showDialog: function (id) {

        PDJS.Shared.Features.FeatureDialogForNhib.open({
            title: "System Variable Properties",
            args: {
                targetId: id,
                targetType: "ProspectDirector.Web.Manage.Modules.SystemVariables.SystemVariableUIO",
                arbiterType: "ProspectDirector.Web.Manage.Modules.SystemVariables.SystemVariableArbiter"
            },
            callbacks: {
                ok: (sender, e) => {

                    if (e.output?.taskId) {
                        this._showTaskStatusDialog(e.target.id, e.output.taskId);
                    }
                    else {
                        this._publishVariableEditEvent(e.target.id);
                    }

                }
            }
        }, {
            height: 550,
            width: 500,
        });

    },

    _showTaskStatusDialog: function (targetId, taskId) {

        PDJS.Shared.TaskStatusDialog.open({
            title: "Edit | Variable",
            taskStart: (e) => {
                e.startPolling({ taskId: taskId });
            },
            taskSuccess: (e) => {
                this._publishVariableEditEvent(targetId);
            }
        });

    },

    _publishVariableEditEvent: function (targetId) {
        PD.context
            .get_default()
            .get_events()
            .publish("variable/edit", {
                id: targetId
            });
    }

});