/// class PD.Manage.Modules.EntityTypeExplorerPane
// using PD.Web.UI.Controls.Control

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

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

    _tree: null,
    _toolbar: null,

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

    onInit: function () {

        var that = this;

        this.subscriptions.add("explorer-folder/delete", function (e) {
            that._tree.removeNode(e.folderId);
        });

        this.subscriptions.add("explorer-folder/create", function (e) {
            that._tree.addNode(e.node, e.parentId);
        });

        this._tree_Init();
        this._toolbar_Init();

    },

    onComplete: function () {

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

        delete localData[0].items
        this._tree.addNode(localData[0]);

    },

    //===================================
    // PUBLIC METHODS
    //===================================

    update: function (data) {
        this._tree.update(data.id, data);
    },

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

    template: function () {
        return [
            "<div>",
                "<div class='-pd-title-minimal'>Explorer Model</div>",
                "<div data-id='toolbar' style='position:absolute;height:30px;top:40px;left:0;right:0;'></div>",
                "<div data-id='tree' style='position:absolute;top:80px;left:0;bottom:0;right:0;border:1px solid #DDDDDD;padding:5px;'></div>",
            "</div>"
        ];
    },

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

    _tree_Init: function () {

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

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

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

                }
            },
            contextMenu: {
                type: "PD.Manage.Modules.EntityTypeContextMenu",
                context: function () {
                    return {
                        tree: that._tree
                    };
                }
            },
            ready: function () {
                this.expandPath(expandPath);
            },
            onChange: function (e) {

                var folder = e.item || {};

                PD.context.get_default().get_commands().setState({
                    "pd/entity-type": {
                        folderId: folder.id,
                        folderName: folder.name,
                        folderRole: folder.role
                    }
                });

                onChangeHandler(e);

            },
            onSelect: function (e) {
                PD.monitor.execute(e.commit);
            },
            onDrop: function (e) {
                if (e.src != null && e.dst != null) {
                    service.moveFolder(e.src.id, e.dst.id, e.position, function (result) {
                        e.commit();
                    });
                }
            },
            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);

    },

    _toolbar_Init: function () {

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

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

    }

});