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

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

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

    _splitter: null,
    _listGrid: null,
    _itemGrid: null,

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

    onInit: function () {
        this.data("hasToolbar", false);
        this._listGrid_init();
        this._itemGrid_init();
    },

    onActivate: function (e) {
        this._splitter_activate();
    },

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

    prepare: function () {

        var that = this;

        PD.ajax({
            url: PD.Path.resolve("~/Manage/SelectionList"),
            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:10px;left:10px;right:10px;bottom:10px;box-sizing:border-box;'>",
                "<div data-id='splitter' class='-pd-minimal-splitter' style='height:100%;'></div>",
            "</div>"
        ];
    },

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

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

    _listGrid_init: function () {

        var that = this;

        this._listGrid = new PD.Manage.Modules.SelectionListGrid({
            insertAt: "[data-id=splitter]",
            viewStateId: this.data("viewStateId"),
            change: function (e) {

                var selectionList = e.selections.first();

                if (selectionList) {
                    that._itemGrid.load({
                        id: selectionList.id,
                        name: selectionList.Name
                    });
                }
                else {
                    that._itemGrid.load(null);
                }

            }
        });

        this.get_children().add(this._listGrid);


    },

    _itemGrid_init: function () {

        var that = this;

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

        this.get_children().add(this._itemGrid);

    },

    _splitter_activate: function () {

        var that = this;

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

    }

});