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

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

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

    _isList: null,
    _grid: null,
    _toolbar: null,

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

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

    get_isList: function () {
        return this._isList;
    },

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

    onInit: function () {

        var that = this;

        this._isList = this.get_options()["isList"];
        this._initGrid();
        this._initToolbar();

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

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

        this.subscriptions.add("extension/update", function (e) {
            that.get_grid().refresh();
        });


    },

    //===================================
    // 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;' class='-pd-title-minimal'>Extensions</div>",
                "<div style='position:absolute;top:40px;left:0;right:0;bottom:0;' 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;

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

                var extension = _.first(e.selections.toArray());

                PD.context.get_default().get_commands().setState({
                    "pd/data-model": {
                        extension: extension,
                        isList: that._isList
                    }
                });

                PD.context.get_default().get_events().publish("extension/open", extension);

            },
            dblclick: function (e) {
                PD.context.get_default().get_commands().execute("pd/data-model:extension/properties-dialog");
            },
            needContext: function (e) {
                that._addIsListFilter(e);
            },
            contextMenu: {
                type: "PD.Manage.Modules.DataModelExtensionsContextMenu",
                context: function () {
                    return {
                        grid: that._grid
                    };
                }
            }
        });

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

    },

    _initToolbar: function () {

        var that = this,
            grid = this._grid;

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

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

    },

    _addIsListFilter: function (e) {

        e.filters[e.filters.length] = {
            id: "IsList",
            params: {
                isList: this._isList ? 1 : 0
            }
        };

    }

});