/// class PD.Manage.Modules.ExpressionDeleteCommand

PD.Manage.Modules.ExpressionDeleteCommand =
PD.Commands.Command.extend({

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

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

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

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

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

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

        var ids = _.map(this.get_expressions(), "id");
        this._showConfirmationDialog(ids);

    },

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

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

    _showConfirmationDialog: function (ids) {

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            title: "Delete Expression",
            message: "Delete the selected expression(s)?",
            select: (e) => {
                e.close();
                if (e.button == Jade.UI.MessageDialogButton.ok) {
                    this._invokeServiceCall(ids)
                }
            }
        });

    },

    _showInUseConfirmationDialog: function (ids) {

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.YesNo,
            title: "Expressions In Use",
            message: "Some of the selected expressions are in use.<br/>Proceed with deletion anyway?",
            select: (e) => {
                e.close();
                if (e.button == Jade.UI.MessageDialogButton.yes) {
                    this._invokeServiceCall(ids, true);
                }
            }
        });

    },

    _startTask: function (ids) {

        PDJS.Shared.TaskStatusDialog.open({
            title: "Delete Expressions",
            showDetail: true,
            taskStart: (e) => {
                e.startPolling(ids);
            },
            taskSuccess: (e) => {

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

                e.close();

            }
        });

    },

    _invokeServiceCall: function (ids, confirmed) {

        var that = this;
        var service = new PDJS.Services.Tags.ExpressionService();

        service.delete({
            ids: ids,
            confirmed: confirmed,
            success: function (e) {

                if (e.data.requiresConfirmation) {
                    that._showInUseConfirmationDialog(ids);
                }
                else {
                    that._startTask(e.data);
                }
            },
            error: function (e) {
                e.errorHandled = true;
            }
        });

    }

});