/// class PD.Manage.Modules.LicenseDeleteCommand

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

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

    get_selection: function () {
        return this.state("selection");
    },

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

    setDefaults: function () {

        this.props("id", "license/delete");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("label", "Remove License");
        this.props("tooltip", "Removes the license from the selected product.");
        this.state("selection", []);

    },

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

    isEnabled: function () {
        return this.get_selection() != null
            && this.get_selection() != "Missing";
    },

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

        let license = this.get_selection();
        
        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            allowClose: true,
            title: "Remove License",
            message: "Remove the selected license?",
            select: (e) => {
                e.close();
                if (e.button === Jade.UI.MessageDialogButton.ok) {
                    this._executeDelete(license);
                }
            }
        });

    },

    _executeDelete: function (license) {

        let service = new PD.Manage.Services.LicenseService();

        service.delete(license.id, () => {
            PD.context
                .get_default()
                .get_events()
                .publish("license/deleted", {
                    id: license.id
                });
        });

    }

});