/// class PD.Manage.Modules.SystemVariables.SystemVariableDeleteCommand

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

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

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

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

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

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

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

        var ids = _.map(this.get_tags(), "id");
        
        this._showConfirmationDialog(ids, () => {
            this._startTask(ids);
        });

    },

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

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

    _showConfirmationDialog: function (tag, okFn) {

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            title: "Delete System Variable",
            message: "Delete the selected variable(s)?",
            select: (e) => {
                e.close();
                if (e.button == Jade.UI.MessageDialogButton.ok) {
                    okFn();
                }
            }
        });

    },

    _startTask: function (ids) {

        PDJS.Shared.TaskStatusDialog.open({
            title: "Delete Variables",
            taskStart: (e) => {

                PD.ajax({
                    type: "DELETE",
                    url: Jade.Path.resolve("~/Manage/SystemVariables/Delete"),
                    data: {
                        ids: JSON.stringify(ids)
                    },
                    success: (e2) => {
                        e.startPolling(e2);
                    },
                    error: (e2) => {
                        e.abort(e2.responseJSON);
                        e2.errorHandled = true;
                    }
                });

            },
            taskSuccess: (e) => {

                PD.context
                    .get_default()
                    .get_events()
                    .publish("variable/delete");

            }
        });

    }

});