/// class PD.Manage.Modules.Resources.Explorer
// using PD.Web.UI.Controls.Control

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

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

    _tree: null,

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

    onInit: function () {

        var that = this;

        this._tree_Init();

        this.subscriptions.add("folder/add", function (e) {
            that._tree.reload();
        });

        this.subscriptions.add("resource/move", function (e) {
            if (e.folderIds.length > 0) {
                _.each(e.folderIds, function (folderId) {
                    that._tree.removeNode(folderId);
                });
                that._tree.reload(e.destinationId);
            }
        });

        this.subscriptions.add("folder/edit", function (e) {
            that._tree.update(e.folderId, {
                name: e.folderName
            });
        });

        this.subscriptions.add("resources/delete", function (e) {
            _.each(e.folderIds, function (id, index) {
                that._tree.removeNode(id);
            });
        });

        this.subscriptions.add("location/changed", function (e) {
            if (e.source != "explorer") {
                that._tree.expandPath(e.path);
            }
        });

        this.subscriptions.add("resources/add", function (e) {
            that._tree.reload(e.folderId);
        });


    },

    onComplete: function () {

        var that = this,
            localData = this.get_options()["preloadNodes"];

        delete localData[0].items
        this._tree.addNode(localData[0]);
        this._tree.expandPath(["00000000-0000-0000-0000-000000000000"]);

    },

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

    template: function () {
        return [
            "<div>",
                "<div data-id='tree' class='-pd-resources-explorer' style='position:absolute;top:0;left:0;bottom:0;right:0;border:1px solid #DDDDDD;padding:5px;'></div>",
            "</div>"
        ];
    },

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

    _tree_Init: function () {

        var that = this,
            expandPath = [0],
            localData = this.get_options()["preloadNodes"],
            service = new PD.Manage.Services.ResourcesService();

        this._tree = new PD.Web.UI.Controls.TreeView({
            insertAt: "[data-id=tree]",
            dragAndDrop: false,
            remoteData: {
                read: function (e) {

                    var item = e.item;

                    if (item) {
                        service.getFolders(item.id, function (result) {
                            e.success(result);
                        });
                    }

                }
            },
            contextMenu: {
                type: "PD.Manage.Modules.Resources.ExplorerContextMenu",
                context: function () {
                    return {
                        tree: that._tree
                    };
                }
            },
            onChange: function (e) {

                var item = e.item || {};
                
                PD.context
                    .get_default()
                    .get_events()
                    .publish("location/change", {
                        folderId: item.id,
                        source: "explorer"
                    });

            },
            modules: [
                "PD.Web.UI.Controls.TreeViewSelectionModule",
                "PD.Web.UI.Controls.TreeViewContextMenuModule"
            ],
            css: {
                position: "absolute",
                top: 0,
                left: 0,
                bottom: 0,
                right: 0
            }
        });

        this.get_children().add(this._tree);

    }

});