/// class ProspectDirector.Web.UI.FeatureSets.Editors.EntitySelectionEditor
/// using ProspectDirector.Web.UI.FeatureSets.Editors.CompositeEditor

ProspectDirector.Web.UI.FeatureSets.Editors.EntitySelectionEditor =
    ProspectDirector.Web.UI.FeatureSets.Editors.CompositeEditor.extend({

        //==============================
        // PROPERTIES
        //==============================

        get_input: function () {
            return this.get_element().find("INPUT[type=text]");
        },

        get_entityTypeId: function () {
            return this.get_data().parentId;
        },

        get_parentId: function () {
            return this.get_data().parentId;
        },

        get_parentName: function () {
            return this.get_data().parentName;
        },

        get_button: function () {
            return this.get_element().find("BUTTON");
        },

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

        _button_onClick: function () {

            PDJS.Shared.Entities.EntityBrowserDialog.open({
                entityTypeId: this.get_entityTypeId(),
                callbacks: {
                    ok: function (sender, e) {
                        this.setPrimaryValue(e.entityId);
                        this.get_input().val(e.entityBane);
                        this._publishEntityParentChanged();
                    }
                }
            });
            
        },

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

        updateUI: function (value) {
            this.get_input().val(value);
        },

        //==============================
        // OVERRIDDEN METHODS
        //==============================

        activate: function () {
            this._super();

            this.get_button().on("click", _.bind(this._button_onClick, this));
            this.get_input().val(this.get_parentName());

            Jade.Application
                .instance
                .contexts
                .current
                .events
                .subscribe("entity:parent:requested",
                    _.bind(this._publishEntityParentChanged, this));

        },

        render: function () {

            var definition = this.get_definition().get_data();
            var disabled = definition.disabled ? "disabled" : "";

            return "<div id='" + this.getId() + "' class='-pd-micro-editor-composite'>" +
                "<input type='text' style='padding-left:5px;' " + disabled + " readonly />" +
                "<input type='button' value='...' " + disabled + "/>" +
                    "</div>";
        },


        _publishEntityParentChanged: function () {

            Jade.Application
                .instance
                .contexts
                .current
                .events
                .publish("entity:parent:changed", {
                    entityId: this.getPrimaryValue(),
                    entityTypeId: this.get_data().entityTypeId
                });

        }

    });