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

        var that = this;

        ProspectDirector.Web.UI.MessageDialog.confirm({
            message: "Delete the selected expression(s)?",
            buttons: {
                ok: {
                    label: "OK",
                    callback: function () {
                        that._invokeServiceCall(ids)
                    }
                },
                cancel: {
                    label: "Cancel"
                }
            }
        });

    },

    _showInUseConfirmationDialog: function (ids) {

        var that = this;

        ProspectDirector.Web.UI.MessageDialog.confirm({
            message: "Some of the selected expressions are in use.<br/>Proceed with deletion anyway?",
            buttons: {
                ok: {
                    label: "Yes",
                    callback: function (e) {
                        that._invokeServiceCall(ids, true);
                    }
                },
                cancel: {
                    label: "No"
                }
            }
        });

    },

    _startTask: function (ids) {

        var that = this;

        PD.Shared.Dialogs.TaskStatus.open({
            config: {
                title: "Delete Expressions"
            },
            showDetail: true,
            enableClose: false,
            taskStart: function (e) {
                e.startPolling(ids);
            },
            taskSuccess: function (e) {
                e.enableClose(true);
            },
            close: function (e) {

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

            }
        });


    },

    _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;
            }
        });

    }

});