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

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

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

    get_featureEditor: function () {
        return this.get_node().find("[data-role=featuredobject]").data("featuredObjectEditor");
    },

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

    onInit: function () {
        this._initToolbar();
    },

    load: function (id, entityTypeId, role) {
        
        switch (role) {
            case "entitytype":
                this._loadEntityTypeFeatures(entityTypeId);
                break;
            case "folder":
                this._loadExplorerFolderFeatures(id);
                break;
        }

    },

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

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

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

    _initToolbar: function () {

        var that = this,
            tree = this._tree;

        this._toolbar = new PD.Web.UI.Controls.Toolbar({
            insertAt: "[data-id=toolbar]",
            segments: [
               new PD.Manage.Modules.EntityTypeFeaturesToolbar(function (e) {
                   return {
                       tree: tree,
                       saveFeaturesCallback: $.proxy(that._saveFeatures, that)
                   }
               })
            ]
        });

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

    },

    _loadEntityTypeFeatures: function (id) {

        PD.ajax({
            type: "GET",
            dataType: "html",
            url: PD.Path.resolve("~/EntityType/Properties/" + id + "?isDialog=false"),
            success: $.proxy(this._loadFeatures, this)
        });

    },

    _loadExplorerFolderFeatures: function (id) {
    
        PD.ajax({
            type: "GET",
            dataType: "html",
            url: PD.Path.resolve("~/ExplorerFolder/Properties/" + id),
            success: $.proxy(this._loadFeatures, this)
        });

    },

    _loadFeatures: function (e) {

        var $featuresNode = this.get_node().children("[data-id='features']"),
            $observedForm = null;

        $featuresNode.empty();
        $featuresNode.html(e);

        // until such time as it can be replaced, disbale the old change tracker
        $observedForm = $featuresNode.find(".observedForm");
        if ($observedForm.length > 0) {
            $observedForm.observedForm("destroy");
        }

        PD.context.get_default().get_commands().setState({
            "pd/entity-type": {
                editor: this.get_featureEditor()
            }
        });

    }

});