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

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

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

    _grid: null,
    _toolbar: null,

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

    get_grid: function () {
        return this._grid;
    },

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

    onInit: function () {

        var that = this;

        this.subscriptions.add("selection-list/create", function (e) {
            that.get_grid().refresh({
                selections: [e.id]
            });
        });

        this.subscriptions.add("selection-list/edit", function (e) {
            that.get_grid().refresh();
        });

        this.subscriptions.add("selection-list/delete", function (e) {
            that.get_grid().refresh({
                clear: true
            });
        });

        this._initGrid();
        this._initToolbar();

    },

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

    refresh: function (options) {
        this.get_grid().refresh(options);
    },

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

    template: function () {
        return [
            "<div>",
                "<div style='position:absolute;top:0;left:0;right:0;height:30px;background-color:transparent;' class='-pd-title-minimal'>Selection Lists</div>",
                "<div style='position:absolute;top:40px;left:0;right:0;bottom:0;background-color:transparent;' data-id='toolbar'></div>",
                "<div style='position:absolute;top:80px;left:0;right:0;bottom:0;border:1px solid #DDDDDD;' data-id='grid'></div>",
            "</div>"
        ];
    },

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

    _initGrid: function () {

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

        this._grid = new PD.Web.UI.Controls.ViewGrid({
            insertAt: "[data-id='grid']",
            selectionMode: "single",
            toolbar: false,
            viewStateId: this.get_options()["viewStateId"],
            selectOnRightClick: true,
            selection: {
                map: function (e) {
                    return _.map(e.selections.toArray(), function (item, index) {
                        return {
                            id: item.ObjectId,
                            name: item.ListName
                        };
                    });
                }
            },
            change: function (e) {

                var selectionList = e.selections.first();

                PD.context.get_default().get_commands().setState({
                    "pd/selection-list": {
                        selectionList: selectionList
                    }
                });

                if (changeFn) {
                    changeFn(e);
                }

            },
            dblclick: function (e) {
                PD.context.get_default().get_commands().execute("pd/selection-list:selection-list/properties-dialog");
            },
            contextMenu: {
                type: "PD.Manage.Modules.SelectionListContextMenu",
                context: function () {
                    return {
                        grid: that._grid
                    };
                }
            }
        });

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

    },

    _initToolbar: function () {

        var grid = this._grid;

        this._toolbar = new PD.Web.UI.Controls.Toolbar({
            insertAt: "DIV[data-id=toolbar]",
            segments: [
               new PD.Manage.Modules.SelectionListToolbar(function (e) {
                   return {
                       grid: grid
                   }
               })
            ]
        });

        this.get_children().add(this._toolbar);

    }

});