/// class PD.Manage.Modules.EntityTypes
/// using PD.Manage.Module

PD.Manage.Modules.EntityTypes =
PD.Manage.Module.extend({

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

    _grid: null,

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

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

    prepare: function () {

        var that = this;

        PD.ajax({
            url: PD.Path.resolve("~/Manage/EntityTypes"),
            success: function (data) {
                that.data("viewStateId", data.viewStateId);
                that.ready();
            },
            error: function (e) {
                that.error();
                e.errorHandled = true;
            }
        });

    },

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

    template: function (e) {
        return [
            "<div style='position:absolute;top:0;left:0;bottom:0;right:0;overflow:hidden;'>",
                "<div data-id='toolbar' style='position:absolute;top:10px;left:10px;right:10px;height:30px;'></div>",
                "<div data-id='grid' style='position:absolute;top:50px;left:10px;right:10px;bottom:10px;border:1px solid #DDDDDD;'></div>",
            "</div>"
        ];
    },

    focus: function () {
        this._super();
        this._grid.redraw();
    },

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

    _initGrid: function () {

        var that = this;

        this._grid = new PD.Web.UI.Controls.ViewGrid({
            insertAt: "[data-id=grid]",
            viewStateId: this.data("viewStateId"),
            selectionMode: "single",
            selectOnRightClick: true,
            change: function (e) {

                var selection = e.selections.first();

                PD.context.get_default().get_commands().setState({
                    "pd/entity-types": {
                        entityTypeId: selection ? selection.id : null,
                        entityTypeName: selection ? selection.Name : null
                    }
                });

            },
            dblclick: function (e) {
                PD.context.get_default().get_commands().execute("pd/entity-types:entity-type/open");
            },
            contextMenu: {
                type: "PD.Manage.Modules.EntityTypesContextMenu",
                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: "[data-id=toolbar]",
            segments: [
               new PD.Manage.Modules.EntityTypesToolbar(function (e) {
                   return {
                       grid: grid,
                       listId: that._listId
                   }
               })
            ]
        });

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

    },

    _subscribeToEvents: function () {

        var grid = this._grid;

        PD.context.get_default().get_events().subscribe("entity-type/create", function (e) {
            grid.refresh({
                clear: true,
                selections: [e.id]
            });
        });

        PD.context.get_default().get_events().subscribe("entity-type/update", function (e) {
            grid.refresh();
        });

        PD.context.get_default().get_events().subscribe("entity-type/delete", function (e) {
            grid.refresh({
                clear: true
            });
        });

    }

});