/// class ProspectDirector.Modules.Views.FilterEditorBasic
/// using ProspectDirector.Types.View.FilterEditor

/**
The module for the View Filter Basic Editor Dialog.
@class FilterEditorBasic
@namespace ProspectDirector.Modules.Views
*/
ProspectDirector.Modules.Views.FilterEditorBasic = (function (options) {

    var 

    //---------------------------
    //--- COMPONENTS
    //---------------------------

    COMPONENTS = {

        editor: null

    },

    //---------------------------
    //--- PRIVATE MEMBERS
    //---------------------------

    PRIVATE = {

        init: function () {

            COMPONENTS.editor = new ProspectDirector.Modules.Views.FilterEditorBasic.Editor(options);

        }

    },

    //---------------------------
    //--- PUBLIC MEMBERS
    //---------------------------

    PUBLIC = {
    };

    PRIVATE.init();

    return PRIVATE;

});

/**
@class Editor
@namespace ProspectDirector.Modules.ViewFilterDialogBasic
**/
ProspectDirector.Modules.Views.FilterEditorBasic.Editor = ProspectDirector.Types.View.FilterEditor.extend({

    /**
    @method init
    @private
    **/
    init: function (options) {
        this._super(options);
        this.value().on("keypress", $.proxy(this._value_onkeypress, this));
        this._enable();
        this.value().select();
        this.value().focus();
        this._addValidation();
    },

    //---------------------------
    //--- PROPERTIES
    //---------------------------

    /**
    @property value
    @private
    **/
    value: function () {
        return this.get_dialog().find("input[data-role=value]");
    },

    //---------------------------
    //--- EVENT HANDLERS
    //---------------------------

    /**
    @method _okButton_onClick
    @private
    **/
    _okButton_onClick: function () {

        var filter = {
            enabled: this.enabled().val() !== "0",
            id: this.filterId().val(),
            operator: parseInt(this.operator().val()),
            value: this._getValue(),
            verse: null
        };

        if (!this.isValueOperator() || this.validation.validate().errors.length === 0) {

            this.get_dialog().data("dialog").hide({
                data: filter,
                command: "ok"
            });

        }

    },

    /**
    @method _value_onkeypress
    @private
    **/
    _value_onkeypress: function (e) {

        if (e.keyCode === 13) {
            e.preventDefault();
            this._okButton_onClick();
        };

    },

    _operator_onChange: function () {
        this._enable();
    },

    _getValue: function () {

        if (!this.isValueOperator()) {
            return;
        }

        return this.value().val();

    },

    _enable: function () {

        var enabled = this.isValueOperator();

        if (enabled) {
            this.value().removeAttr("disabled");
            this.value().css("backgroundColor", "");
        }
        else {
            this.value().attr("disabled", "disabled");
            this.value().css("backgroundColor", "#EEEEEE");
        }

    },

    _addValidation: function () {

        var type = this.options.dataType,
            validators = [];

        switch (true) {
            case (type === "Int32" || type === "Int64"):
                validators[validators.length] = { input: this.value(), validator: ProspectDirector.Types.Core.Validators.integer };
            case (type === "Decimal"):
                validators[validators.length] = { input: this.value(), validator: ProspectDirector.Types.Core.Validators.decimal };
        }

        this.validation = new ProspectDirector.Types.Core.FormValidation({
            container: this.get_dialog(),
            validators: validators
        });

    },

    isValueOperator: function () {
        return ProspectDirector.Types.View.FilterOperator.isValueOperator(parseInt(this.operator().val()))
    }

});