/// class PD.Manage.Modules.ExtensionDeleteDialogCommand

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

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

    setDefaults: function () {

        this.props("id", "extension/delete-dialog");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("table_delete.png"));
        this.props("label", "Delete");
        this.props("tooltip", "Delete the selected Extension.");
        this.state("extension", null);

    },

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

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

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

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            allowClose: true,
            title: "Delete Extension",
            message: "Delete the \"" + extension.name + "\" extension?",
            select: (e) => {
                e.close();
                if (e.button === Jade.UI.MessageDialogButton.ok) {
                    this._deleteExtension(extension.id, extension.name, false);
                }
            }
        });

    },

    isEnabled: function () {
        return this.state("extension") != null
            && this.state("extension").id != null;
    },

    _deleteExtension: function (id, name, confirmation) {

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

        service.deleteExtension(id, confirmation, function (e) {
            if (e.requiresConfirmation) {
                that._showDeleteConfirmationDialog(id, name, e);
            }
            else {

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

            }
        });

    },

    _showDeleteConfirmationDialog: function (id, name, args) {

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            allowClose: true,
            title: "Data Loss Warning",
            message: `The "${name}" extension contains data. Deletion will result in the loss of this data. Continue with the deletion?`,
            select: (e) => {
                e.close();
                if (e.button === Jade.UI.MessageDialogButton.ok) {
                    this._deleteExtension(id, name, true);
                }
            }
        });

    }

});