/// class PD.Manage.Modules.ObjectModel.ProcessExplorerPane
// using PD.Web.UI.Controls.Control

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

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

    _tree: null,
    _toolbar: null,

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

    onInit: function () {

        var that = this;

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

        this.subscriptions.add("process-node/delete", function (e) {
            that._tree.removeNode(e.nodeId);
        });

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

    },

    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'>Process 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 = [0],
            localData = this.get_options()["preloadNodes"],
            onChangeHandler = this.get_options()["onChange"],
            service = new PD.Manage.Services.ProcessModelService();

        // Must set root node ID to `0` since process and process nodes can have an ID collision.
        // This will not be an issue once we convert integer IDs to GUIDs.
        localData[0].id = 0;

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

                    var item = e.item;

                    if (item) {
                        service.getNodes(item.processId, item.role === "Process" ? null : item.id, function (result) {
                            e.success(result);
                        });
                    }

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

                var node = e.item || {};

                PD.context.get_default().get_commands().setState({
                    "pd/process-model": {
                        processId: node.processId,
                        nodeId: node.id,
                        nodeName: node.name,
                        nodeRole: node.role,
                        nodeType: node.type
                    }
                });

                onChangeHandler(e);

            },
            onSelect: function (e) {
                PD.monitor.execute(e.commit);
            },
            onDrop: function (e) {

                PD.context.get_default().get_commands().setState({
                    "pd/process-model": {
                        moveNode: {
                            src: e.src,
                            dst: e.dst,
                            position: e.position,
                            commit: e.commit
                        }
                    }
                });

                PD.context.get_default().get_commands().execute("pd/process-model:process-node/move");

            },
            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.ObjectModel.ProcessExplorerToolbar()
            ]
        });

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

    }

});