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

/**
@class BooleanTagEditor
@namespace Web.UI.FeatureSets.Editors
@extends Web.UI.FeatureSets.Editors.CompositeEditor
@constructor
**/

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

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

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

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

    get_ddl: function () {
        return this.get_element().find("SELECT");
    },

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

    _ddl_onChange: function () {
        this.commitValue();
    },

    _input_onBlur: function (e) {
        this.commitValue();
    },

    _button_onClick: function () {
        this.enableTagMode(this.get_ddl().parent().is(":visible"));
    },

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

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

        var definition = this.get_definition().get_data();

        this.get_input().on("blur", $.proxy(this._input_onBlur, this));
        this.get_ddl().on("change", $.proxy(this._ddl_onChange, this));

        this.get_element().prop("readOnly", definition.readOnly);
        this.get_element().prop("disabled", definition.disabled);

        this.ensureInitialValue();
        this.enableTagMode(this.isTagMode());

    },

    render: function () {

        return [
            "<div id='" + this.getId() + "' class='-pd-micro-editor-composite -pd-boolean-tag-editor'>",
                "<div>",
                    "<select>",
                        "<option>True</option>",
                        "<option>False</option>",
                    "</select>",
                "</div>",
                "<div>",
                    "<input type='text'>",
                "</div>",
                "<input type='button' value='...' />" +
            "</div>"
        ].join("");

    },

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

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

        if (value == null || (value.value == null && value.text == null)) {
            value = { text: null, value: true };
        }

        if (value.value == null) {
            value.value = true;
        }

        this.setPrimaryValue(value);

        if (value.text != null) {
            this.get_input().val(value.text);
        }

        this.get_ddl().val(value.value ? "True" : "False");

    },

    isTagMode: function () {

        var value = this.getPrimaryValue();

        return value.text != null;

    },

    getDdlValue: function () {
        var value = this.get_ddl().children("option:selected").val();
        return value == "True" ? true : false;
    },

    getTagValue: function () {

        if (this.get_ddl().parent().is(":visible")) {
            return null;
        }

        var value = this.get_input().val();
        return _.isNullOrWhitespace(value) ? null : value;
    },

    enableTagMode: function (enable) {

        if (enable) {
            this.get_ddl().parent().hide();
            this.get_input().parent().show();
            this.get_button().val("T");
            this.get_button().prop("title", "Switch to Fixed Value");
        }
        else {
            this.get_ddl().parent().show();
            this.get_input().parent().hide();
            this.get_button().val("F");
            this.get_button().prop("title", "Switch to Tag Value");
        }

        this.commitValue();
    },

    commitValue: function () {

        this.setPrimaryValue({
            text: this.getTagValue(),
            value: this.getDdlValue()
        });
    }

});