/// class PD.Manage.Modules.Resources.ResourcesModule
/// using PD.Manage.Module

PD.Manage.Modules.Resources.ResourcesModule =
PD.Manage.Module.extend({

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

    _splitter: null,
    _explorerPane: null,
    _browserPane: null,
    _toolbar: null,
    _monitor: null,

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

    onPrepare: function () {

        var that = this;

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

    },

    onInit: function (e) {
        this._initExplorerPane();
        this._initBrowserPane();
        this._initToolbar();
        this._initLocationMonitor();
    },

    onActivate: function () {
        this._activateSplitter();
    },

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

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

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

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

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

    _activateSplitter: function () {

        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");

    },

    _initExplorerPane: function () {

        var that = this;

        this._explorerPane = new PD.Manage.Modules.Resources.Explorer({
            insertAt: "[data-id=splitter]",
            preloadNodes: this.data("preloadNodes")
        });

        this.get_children().add(this._explorerPane);

    },

    _initBrowserPane: function () {

        this._browserPane = new PD.Manage.Modules.Resources.Browser({
            insertAt: "[data-id=splitter]",
            viewStateId: this.data("viewStateId")
        });

        this.get_children().add(this._browserPane);

    },

    _initToolbar: function () {

        var that = this;

        this._toolbar = new PD.Web.UI.Controls.Toolbar({
            insertAt: "[data-id=toolbar]",
            segments: [
               new PD.Manage.Modules.Resources.ResourcesToolbar()
            ]
        });

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

    },

    _initLocationMonitor: function () {

        this._monitor = new PD.Manage.Modules.Resources.LocationMonitor({});

        this.get_children().add(this._monitor);

    }

});