/// 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 that = this;
        var extension = this.state("extension");

        PD.Web.UI.MessageDialog.warn({
            title: "Delete Extension",
            message: "Delete the \"" + extension.name + "\" extension?",
            ok: function (e) {
                e.close();
                that._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) {

        var that = this;

        PD.Web.UI.MessageDialog.warn({
            title: "Data Loss Warning",
            message: "The \"" + name + "\" extension contains data. Deletion will result in the loss of this data. Continue with the deletion?",
            ok: function (e) {
                e.close();
                that._deleteExtension(id, name, true);
            }
        });

    }

});