/// class PD.Manage.Modules.SyncDeleteCommand

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

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

    get_selections: function () {
        return this.state("selections") || [];
    },

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

    setDefaults: function () {
        this.props("id", "sync/delete");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("label", "Delete");
        this.props("tooltip", "Delete the Active Directory synchronization.");
        this.state("selections", null);
    },

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

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

        var ids = _.map(this.get_selections(), "id");
        
        this._showConfirmationDialog(ids);

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _isEnabled: function () {
        return this.get_selections().length > 0;
    },

    _showConfirmationDialog: function (ids) {

        var that = this;

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Information,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.YesNo,
            title: "Delete Active Directory Synchroniztion",
            message: "Delete the selected Active Directory synchronization(s)?",
            select: function (e) {
                e.close();
                if (e.button == Jade.UI.MessageDialogButton.yes) {
                    that._invokeServiceCall(ids, false);
                }
            }
        });

    },

    _showInUseConfirmationDialog: function (ids) {

        var that = this;

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.YesNo,
            title: "Delete Active Directory Synchroniztion",
            message: "One or more user/group accounts make use of one the synchronization settings you have selected. Proceed with deletion?",
            select: function (e) {
                e.close();
                if (e.button == Jade.UI.MessageDialogButton.yes) {
                    that._invokeServiceCall(ids, true);
                }
            }
        });

    },

    _invokeServiceCall: function (ids, confirmed) {

        var that = this;
        var service = new PDJS.Services.Security.ActiveDirectoryService();

        service.delete({
            ids: ids,
            confirmed: confirmed,
            success: (e) => {
                if (e.data.requiresConfirmation) {
                    that._showInUseConfirmationDialog(ids);
                }
                else {
                    PD.context.get_default().get_events().publish("sync/delete");
                }
            }
        });

    }

});