/// class PD.Manage.Modules.EntityType
/// using PD.Manage.Module

PD.Manage.Modules.EntityType =
PD.Manage.Module.extend({

    //===================================
    // FIELDS
    //===================================
    
    _splitter: null,
    _explorerPane: null,
    _featuresPane: null,
    _selectedExplorerFolderId: null,

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

    onPrepare: function () {

        var that = this;

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

    },

    onInit: function (e) {

        this.subscriptions.add("entity-type/update", function (data) {
            this.data("label", data.name);
        }, this);
        
        this.subscriptions.add("explorer-folder/update", function (data) {
            this._explorerPane.update(data);
        }, this);

        this.subscriptions.add("explorer-folder/delete", function (data) {
            //ensure that monitor is cleared if current folder is in dirty state and is being deleted.
            if (data.folderId === this._selectedExplorerFolderId) {
                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.EntityTypeExplorerPane({
            insertAt: "[data-id=splitter]",
            entityTypeId: this.data("entityTypeId"),
            expandPath: this.data("expandPath"),
            preloadNodes: this.data("preloadNodes"),
            contextMenuFolders: this.data("contextMenuFolders"),
            onChange: function (e) {
                if (e.item != null) {
                    that._featuresPane.load(e.item.id, e.item.entityTypeId, e.item.role);
                    that._selectedExplorerFolderId = e.item.id;
                }
            }
        });

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

    },
    
    _initFeaturesPane: function () {

        this._featuresPane = new PD.Manage.Modules.EntityTypeFeaturesPane({
            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/entity-type:explorer-folder/update", {
                        success: success,
                        error: error
                    });
            },
            onClean: function () {
                that.data("hasChanges", false);
            },
            onDirty: function () {
                that.data("hasChanges", true);
            }
        });

    }

});