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

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

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

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

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

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

    get_extensionId: function () {
        return this._extensionId;
    },

    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-column/create", function (e) {
            that.get_grid().refresh({
                selections: [e.id]
            });
        });

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

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

        this.subscriptions.add("extension/open", function (e) {
            that._load(e);
        });

    },

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

    template: function () {
        return [
            "<div>",
                "<div style='position:absolute;top:0;left:0;right:0;height:30px;' class='-pd-title-minimal'>Columns</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: "multiple",
            viewStateId: this.get_options()["viewStateId"],
            defaultSelectionEnabled: true,
            selectOnRightClick: true,
            selection: {
                map: function (e) {
                    return _.map(e.selections.toArray(), function (item, index) {
                        return {
                            id: item.ObjectId,
                            name: item.ColumnName
                        };
                    });
                }
            },
            change: function (e) {

                var extensionColumns = e.selections.toArray();

                PD.context.get_default().get_commands().setState({
                    "pd/data-model": {
                        extensionColumns: extensionColumns
                    }
                });

            },
            dblclick: function (e) {
                PD.context.get_default().get_commands().execute("pd/data-model:extension-column/properties-dialog");
            },
            needContext: function (e) {
                that._addExtensionIdFilter(e);
                that._addExtensionTypeFilter(e);
            },
            contextMenu: {
                type: "PD.Manage.Modules.DataModelColumnsContextMenu",
                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.DataModelColumnsToolbar(function (e) {
                    return {
                        grid: grid,
                        extensionId: that.get_extensionId(),
                        isList: that.get_isList()
                    }
                })
            ]
        });

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

    },

    _addExtensionIdFilter: function (e) {

        e.filters[e.filters.length] = {
            id: "Extension",
            params: {
                extensionId: this._extensionId ? this._extensionId : -1
            }
        };

    },

    _addExtensionTypeFilter: function (e) {

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

    },

    _load: function (extension) {

        var extensionName = extension ? extension.name : null;

        this._extensionId = extension ? extension.id : null;

        this._grid.refresh({
            clear: true
        });

        if (extensionName) {
            this.get_node().find(".-pd-title-minimal").html("Columns for <span style='color:#007acc;'>" + extensionName + "</span>")
        }
        else {
            this.get_node().find(".-pd-title-minimal").html("Columns");
        }

    }

});