/// class PD.Web.UI.Controls.Security.EffectivePermissionsGrid
/// using PD.Web.UI.Controls.Control

PD.Web.UI.Controls.Security.EffectivePermissionsGrid =
PD.Web.UI.Controls.Control.extend({

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

    _grid: null,
    _permissions: null,

    //===========================
    // CONSTRUCTORS
    //===========================

    init: function (options) {
        this._super(_.extend({
            change: function () {}
        }, options));
    },

    //===========================
    // PROPERTIES
    //===========================
    
    //===========================
    // EVENT HANDLERS
    //===========================

    onActivate: function () {
        this._grid_init();
    },
    
    //===========================
    // PUBLIC METHODS
    //===========================
   
    load: function (permissions) {
        this._grid.getSelections().clear();
        this._grid.get_dataSource().data(permissions);
    },
    
    //===========================
    // PROTECTED METHODS
    //===========================

    template: function (options) {
        return [
            "<div class='-pd-effective-permissions-grid'>",
                "<div data-role='label'>",
                    "Permissions",
                "</div>",
                "<div data-role='grid'></div>",
            "</div>"
        ];
    },

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

    _grid_init: function () {

        var change = this.get_options()["change"];

        if (!_.isFunction(change)) {
            change = function () { };
        }

        this._grid = new PD.Web.UI.Controls.Grid({
            insertAt: "div[data-role=grid]",
            pk: "expandedName",
            selectionMode: "single",
            defaultSelectionEnabled: true,
            dataSource: {
                data: []
            },
            messaging: {
                noRecordsMessage: "No Permissions Defined"
            },
            footer: {
                visible: false
            },
            change: _.bind(change, this),
            columns: [
                new PD.Web.UI.Controls.GridColumn({
                    title: "Permission",
                    field: "expandedName",
                    width: "150px"
                }),
                new PD.Web.UI.Controls.GridColumn({
                    title: "Basic Group",
                    field: "basicName"
                }),
                new PD.Web.UI.Controls.GridCheckboxColumn({
                    title: "Allow",
                    field: "allowed",
                    width: "40px",
                    readOnly: true,
                    attributes: {
                        class: "permission-check allow"
                    }
                }),
                new PD.Web.UI.Controls.GridCheckboxColumn({
                    title: "Deny",
                    field: "denied",
                    width: "40px",
                    readOnly: true,
                    attributes: {
                        class: "permission-check deny"
                    }
                })
            ]
        });

        this.get_children().add(this._grid);

    }
    
});