/// class PD.Manage.Modules.ObjectModel.Process
/// using PD.Manage.Module

PD.Manage.Modules.ObjectModel.Process =
PD.Manage.Module.extend({

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

    _splitter: null,
    _explorerPane: null,
    _featuresPane: null,
    _selectedNodeId: null,

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

    onPrepare: function () {

        var that = this;

        PD.ajax({
            url: PD.Path.resolve("~/Manage/Process/Index/" + this.data("targetId")),
            success: function (data) {
                that.data("processId", data.processId);
                that.data("processName", data.processName);
                that.data("expandPath", data.expandPath);
                that.data("preloadNodes", data.preloadNodes);
                that.data("label", data.processName);
                that.data("tooltip", `<div class="-pd-module-tab-tooltip-resources">Process</div>`);
                that.ready();
            },
            error: function (e) {
                that.error();
                e.errorHandled = true;
            }
        });

    },

    onInit: function (e) {

        this.subscriptions.add("process/update", function (data) {
            this.data("label", data.name);
        }, this);

        this.subscriptions.add("process-node/update", function (data) {
            this._explorerPane.update(data);
        }, this);

        this.subscriptions.add("process-node/delete", function (data) {
            //ensure that monitor is cleared if current node is in dirty state and is being deleted.
            if (data.nodeId === this._selectedNodeId) {
                PD.monitor.getActiveSource().setClean();
            }
        }, this);

        this._initExplorerPane();
        this._initFeaturesPane();

    },

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

    onComplete: function () {
        this._addChangeMonitor();
    },

    onFocus: function () {
        this._splitter.resize();
        PD.monitor.setActiveSource(this.data("id"));
    },

    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 style='position:absolute;top:10px;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.ObjectModel.ProcessExplorerPane({
            insertAt: "[data-id=splitter]",
            entityTypeId: this.data("entityTypeId"),
            expandPath: this.data("expandPath"),
            preloadNodes: this.data("preloadNodes"),
            onChange: function (e) {
                if (e.item != null) {
                    that._featuresPane.load(e.item.id, e.item.processId, e.item.role);
                    that._selectedNodeId = e.item.id;
                }
            }
        });

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

    },

    _initFeaturesPane: function () {

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

        this.get_children().add(this._featuresPane);

    },

    _addChangeMonitor: function () {

        var that = this;

        PD.monitor.addSource({
            id: this.data("id"),
            name: function () {
                return that.data("label");
            },
            monitors: [new PD.Web.UI.InputMonitor({
                selector: this.get_node().find("[data-id=features]")
            })],
            resolve: function (success, error) {
                PD.context.get_default().get_commands().execute("pd/process-model:process-node/update", {
                    success: success,
                    error: error
                });
            },
            onClean: function () {
                that.data("hasChanges", false);
            },
            onDirty: function () {
                that.data("hasChanges", true);
            }
        });

    }

});