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

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

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

    _grid: null,
    
    //===========================
    // CONSTRUCTORS
    //===========================

    init: function (options) {
        this._super(options);
    },
    
    //===========================
    // EVENT HANDLERS
    //===========================

    onActivate: function () {
        this._grid_init();
    },

    //===========================
    // PUBLIC METHODS
    //===========================
    
    load: function (sources) {
        this._grid.get_dataSource().data(sources);
    },

    //===========================
    // PROTECTED METHODS
    //===========================

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

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

    load: function (sources) {

        var data = [];

        if (_.isArray(sources)) {
            data = sources;
        }
        
        this._grid.get_dataSource().data(data);
        
    },

    //===========================
    // 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]",
            selectionMode: "none",
            dataSource: {
                data: []
            },
            messaging: {
                noRecordsMessage: "No Sources Found"
            },
            footer: {
                visible: false
            },
            change: _.bind(change, this),
            columns: [
                new PD.Web.UI.Controls.GridIconColumn({
                    title: " ",
                    field: "isUser",
                    transform: function (e) {
                        return e.isUser ? "USER.PNG" : "USERS.PNG"
                    }
                }),
                new PD.Web.UI.Controls.GridColumn({
                    title: "Source",
                    field: "identityName"
                }),
                new PD.Web.UI.Controls.GridColumn({
                    title: "Inherited",
                    field: "inherited",
                    width: "100px",
                    template: function (e) {
                        return (e.inherited ? "<b>Yes</b>" : "No");
                    }
                })
            ]
        });

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

    }

});