/// class PD.Manage.Modules.ExtensionColumnDeleteDialogCommand

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

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

    setDefaults: function () {

        this.props("id", "extension-column/delete-dialog");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("table_selection_column_delete.png"));
        this.props("label", "Delete");
        this.props("tooltip", "Delete the selected Column.");

        this.state("extensionColumns", null);

    },

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

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

        var extensionColumns = this.state("extensionColumns");
        var extensionColumnsIds = _.map(extensionColumns, "id");

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            allowClose: true,
            title: "Delete Columns",
            message: "Delete the selected column(s)?",
            select: (e) => {
                e.close();
                if (e.button === Jade.UI.MessageDialogButton.ok) {
                    this._deleteColumns(extensionColumnsIds, null);
                }
            }
        });

    },

    isEnabled: function () {

        var extensionColumns = this.state("extensionColumns");

        return extensionColumns != null
            && _.isArray(extensionColumns)
            && extensionColumns.length > 0;

    },

    _deleteColumns: function (ids, confirmation) {

        var that = this;
        var service = new PD.Manage.Services.ExtensionColumnService();

        service.deleteColumns(ids, confirmation, function (e) {
            if (e.requiresConfirmation) {
                that._showDeleteConfirmationDialog(ids, e);
            }
            else {

                PD.context
                    .get_default()
                    .get_events()
                    .publish("extension-column/delete", {
                        ids: ids
                    });

            }
        });

    },

    _showDeleteConfirmationDialog: function (ids, args) {

        Jade.UI.ErrorDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            title: "Data Loss Warning",
            message: `Deleting the extension columns listed below
                      will result in the loss of data.
                      Continue with the deletion?`,
            errors: args.names,
            select: (e) => {
                e.close();
                if (e.button == Jade.UI.MessageDialogButton.ok) {
                    this._deleteColumns(ids, args.confirmation);
                }
            }
        });

    }

});