/// class PD.Shared.Dialogs.EffectivePermissions
/// using PD.Web.UI.Dialog

PD.Shared.Dialogs.EffectivePermissions = PD.Web.UI.Dialog.extend({

    //===================================
    // FIELDS
    //===================================

    _grid: null,
    _viewStateId: null,
    _identitySelector: null,
    _identity: null,
    
    //===================================
    // PROPERTIES
    //===================================
    
    get_closeButton: function () {
        return this.get_node().find("BUTTON[data-id=button-close]");
    },
    
    //===================================
    // PROTECTED EVENT HANDLERS
    //===================================

    getDefaultOptions: function () {
    
        return {
            permissions: [],
            entityId: null,
            identity: null,
            config: {
                actions: [],
                height: "575px",
                minHeight: "300px",
                width: "500px",
                minWidth: "300px",
                title: "Effective Permissions",
                resizable: true
            }
        };

    },

    template: function (options) {
        return [
            "<div class='-pd-effective-permissions'>",
                "<div data-role='identity-selector-label'>Show Effective Permissions For:</div>",
                "<div data-role='identity-selector-container'></div>",
                "<div data-role='permissions-grid-container'></div>",
                "<div data-role='sources-grid-container'></div>",
                "<div data-role='button-deck'>",
                    "<button ",
                        "data-id='button-close' ",
                        "class='-pdjs-dialog-button'>",
                        "Close",
                    "</button>",
                "</div>",
            "</div>"
        ];
    },
    
    onComplete: function () {
        this.effectivePermissionsGrid_init();
        this.identitySelector_init();
        this.effectivePermissionsSourcesGrid_init();
        this.get_closeButton().on("click", _.bind(this._closeButton_onClick, this))
    },

    //===================================
    // PRIVATE EVENT HANDLERS
    //===================================

    identitySelector_init: function () {

        var that = this;
        var identity = this.get_options()["identity"];

        this._identitySelector = new PD.Web.UI.Controls.SelectionBox({
            insertAt: "[data-role=identity-selector-container]",
            change: _.bind(this.identitySelector_onChange, this),
            callback: function (e) {
                
                PDJS.Shared.Security.IdentityBrowserDialog.open({
                    selectionMode: PD.Web.UI.Controls.GridSelectionMode.single,
                    title: "Select User or Group",
                    filters: [
                        PDJS.Core.Views.IdentityFilter.excludeAllAdmins()
                    ],
                    callbacks: {
                        ok: function (sender, e) {
                            var item = _.first(e);
                            that._identity = item;
                            that._identitySelector.setSelection(item.id, item.name);
                        }
                    }
                });

            }
        });

        this.get_children().add(this._identitySelector);

        if (identity != null) {
            this._identitySelector.setSelection(identity.id, identity.displayName || identity.name);
        }
        
    },

    identitySelector_onChange: function (e) {

        var that = this;
        var entityId = this.get_options().entityId;
        var acl = this.get_options().acl;

        PD.ajax({
            type: "POST",
            contentType: "application/json",
            url: PD.Path.resolve("~/Shared/Security/GetEffectivePermissions"),
            data: JSON.stringify({
                entityId: entityId,
                identityId: e.value,
                aces: JSON.stringify(acl)
            }),
            success: function (e) {
                that._effectivePermissionsGrid.load(e);
            }
        });

    },

    effectivePermissionsGrid_init: function () {

        var that = this;

        this._effectivePermissionsGrid = new PD.Web.UI.Controls.Security.EffectivePermissionsGrid({
            insertAt: "[data-role=permissions-grid-container]",
            change: function (e) {
                if (e.selection != null) {
                    that._effectivePermissionsSourcesGrid.load(e.selection.sources.toJSON());
                }
            }
        });

        this.get_children().add(this._effectivePermissionsGrid);

    },
    
    effectivePermissionsSourcesGrid_init: function () {

        var that = this;

        this._effectivePermissionsSourcesGrid = new PD.Web.UI.Controls.Security.EffectivePermissionsSourcesGrid({
            insertAt: "[data-role=sources-grid-container]"
        });

        this.get_children().add(this._effectivePermissionsSourcesGrid);

    },
    
    _closeButton_onClick: function () {
        this.close({
            command: "close",
            data: {
                identity: this._identity
            }
        });
    },
    
});

PD.Shared.Dialogs.EffectivePermissions.open = function (options) {
    return new PD.Shared.Dialogs.EffectivePermissions(options);
};