/// class PD.Manage.Modules.OverviewGrid
// using PD.Web.UI.Controls.Control

PD.Manage.Modules.OverviewGrid =
PD.Web.UI.Controls.Control.extend({

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

    _grid: null,

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

    onInit: function (e) {
        this._initGrid();
    },

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

    load: function (data) {

        var transformedData = [];

        _.each(data, function (area) {
            _.each(area.tiles, function (tile) {
                var transformedItem = {};
                transformedItem = _.extend({
                    id: tile.moduleScripts.length > 0 ? tile.moduleScripts[0].id : "",
                    area: area.name
                }, tile);
                transformedData[transformedData.length] = transformedItem;
            });
        });

        this._grid.get_dataSource().get_instance().data(transformedData);
        
    },

    hide: function () {
        this.get_node().hide();
    },

    show: function () {
        this.get_node().show();
        this._grid.redraw();
        this._publishSelection();
    },

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

    template: function () {
        return "<div class='-pd-overview-area-grid-container'></div>";
    },

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

    _initGrid: function () {

        var that = this;

        this._grid = new PD.Web.UI.Controls.Grid({
            pk: "id",
            resizable: true,
            sortable: true,
            footer: {
                visible: false
            },
            dataSource: {
                data: []
            },
            change: function (e) {
                that._publishSelection();
            },
            dblclick: function (e) {
                window.location.hash = "/module/" + e.selection.id;
            },
            columns: [
                new PD.Web.UI.Controls.GridIconColumn({
                    field: "icon",
                    iconDirectory: "~/Areas/Shared/Assets/Internal/Icons/16x16/"
                }),
                new PD.Web.UI.Controls.GridColumn({
                    field: "label",
                    title: "Module",
                    width: "200px",
                    attributes: {
                        style: "font-weight:bold;"
                    }
                }),
                new PD.Web.UI.Controls.GridColumn({
                    field: "area",
                    title: "Area",
                    width: "200px"
                }),
                new PD.Web.UI.Controls.GridFillColumn()
            ]
        });

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

    },

    _publishSelection: function () {

        var selection = _.first(this._grid.getSelections().toArray()),
            selectFn = this.get_options().select;

        if (_.isFunction(selectFn)) {
            selectFn({
                module: selection
            });
        }

    }

});