/// class PD.Manage.Modules.EntityManagement

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

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

    _splitter: null,
    _grid: null,
    _list: null,
    _managedTypes: null,

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

    onInit: function () {

        var that = this;

        this._list_init()
        this._grid_init();

    },

    onActivate: function (e) {

        this._toolbar_init();

        var that = this;

        this._splitter = this.get_node().find("[data-id=splitter]").kendoSplitter({
            orientation: "horizontal",
            panes: [
                { size: "250px" },
                { }
            ],
            resize: PD.BCL.Util.debounce(function () {
                that._splitter._resize();
            }, 100)
        }).data("kendoSplitter");

    },

    onRemove: function () {
        if (this._splitter != null) {
            this._splitter.destroy();
        }
    },

    prepare: function () {

        var that = this;

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

    },

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

    template: function (e) {
        return [
            "<div style='position:absolute;top:10px;left:10px;right:10px;bottom:10px;box-sizing:border-box;'>",
                "<div data-id='toolbar' style='position:absolute;height:30px;top:0;left:0;right:0;'>",
                "</div>",
                "<div style='position:absolute;top:40px;left:0;bottom:0;right:0;'>",
                    "<div data-id='splitter' class='-pd-content-splitter' style='height:100%;'></div>",
                "</div>",
            "</div>"
        ];
    },

    onFocus: function () {
        this._super();
        this._splitter.resize();
    },

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

    _grid_init: function () {

        var that = this;

        this._grid = new PD.Manage.Modules.EntityManagementGrid({
            insertAt: "[data-id=splitter]"
        });

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

    },

    _list_init: function () {

        var that = this;

        this._managedTypes = this._managedTypes || [];

        this._list = new PD.Manage.Modules.EntityManagementList({
            insertAt: "[data-id=splitter]",
            data: this._managedTypes,
            select: function (e) {

                if (e.selection != null) {
                    that._grid.load(e.selection);
                }

                PD.context.get_default().get_commands().setState({
                    "pd/entity-management": {
                        entityType: e.selection
                    }
                });

            }
        });

        this.get_children().add(this._list);

    },

    _toolbar_init: function () {

        var grid = this._grid.get_grid(),
            list = this._list;

        this._toolbar = new PD.Web.UI.Controls.Toolbar({
            insertAt: "[data-id=toolbar]",
            segments: [
              new PD.Manage.Modules.EntityManagementToolbar(function (e) {
                  var itemData = list.select().data;
                  return {
                      viewGrid: grid,
                      entityTypeId: itemData != null ? itemData.id : null
                  }
              })
            ]
        });

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

    }

});
