/// class ProspectDirector.Web.UI.FeatureSets.MicroEditorBase
/// using ProspectDirector.Web.UI.FeatureSets.EditorBase

/**
[NEEDS DOCUMENTATION]
@constructor
@class MicroEditorBase
@namespace Web.UI.FeatureSets
@extends Web.UI.FeatureSets.EditorBase
**/
ProspectDirector.Web.UI.FeatureSets.MicroEditorBase = ProspectDirector.Web.UI.FeatureSets.EditorBase.extend({

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

    get_element: function () {
        return $("#" + this.getId());
    },

    /**
    @public
    @property get_label
    @type {jQuery}
    **/
    get_label: function () {
        return this.get_element().closest("TR[data-uid]").find(".-pd-micro-editor-label");
    },

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

    /**
    Sets the value identified using the `key` supplied by the {{#crossLink "Web.UI.FeatureSets.EditorDefinition"}}{{/crossLink}} {{#crossLink "Web.UI.FeatureSets.EditorDefinition/get_name:property"}}{{/crossLink}} property.
    @public
    @method setPrimaryValue
    @param value {Object}
    **/
    setPrimaryValue: function (value) {
        this.setValue(this.get_definition().get_name(), value);
    },

    /**
    Gets the value associated with the `key` supplied by the {{#crossLink "Web.UI.FeatureSets.EditorDefinition"}}{{/crossLink}} {{#crossLink "Web.UI.FeatureSets.EditorDefinition/get_name:property"}}{{/crossLink}} property. If the `key` is not found `undefined` is returned.
    @public
    @method getPrimaryValue
    @return {Object}
    **/
    getPrimaryValue: function () {
        return this.getValue(this.get_definition().get_name());
    },

    /**
    @public
    @property get_label
    @type {jQuery}
    **/
    validate: function (errors) {
        return true;
    },

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

    render: function () {
        
        var value = this.getPrimaryValue();

        return "<input type\"text\" class=\"-pd-micro-editor-textbox\" id=\"" + this.getId() + "\" value=\"" + (value === null ? "" : value) + "\"  />";
    
    },

    activate: function () {

        var that = this;

        this.get_element().on("blur", $.proxy(function (e) {

            var value = $(e.target).val();

            if (that.validate(value)) {
                that.setPrimaryValue(value);
            }

        }, this));

    },

    setValidationErrors: function (validationErrors) {

        var that = this,
            featureType = this.get_definition().get_type(),
            propertyName = this.get_definition().get_name(),
            errors = validationErrors.getByFeatureTypeAndPropertyName(featureType, propertyName);

        this.get_label().removeClass("-pd-micro-editor-val-error");

        Tipped.remove(this.get_label());

        if (errors.count() > 0) {

            errors.each(function (index, error) {
                that.get_label().addClass("-pd-micro-editor-val-error");
                Tipped.create(that.get_label(), error.get_message(), { skin: "validation-error" });
            });

        }

    }

});