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

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

    var 

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

    COMPONENTS = {

        editor: null

    },

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

    PRIVATE = {

        init: function () {

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

        }

    },

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

    PUBLIC = {
    };

    PRIVATE.init();

    return PRIVATE;

});

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

    /* constructor */

    init: function (options) {

        this._super(options);

        this._operator_onChange();

        this.dateText().on("keypress", $.proxy(this._onkeypress, this));
        this.daysText().on("keypress", $.proxy(this._onkeypress, this));
        this.operator().on("keypress", $.proxy(this._onkeypress, this));
        this.dateRadio().on("click", $.proxy(this._radio_onClick, this));
        this.daysRadio().on("click", $.proxy(this._radio_onClick, this));
        this.validation = new ProspectDirector.Types.Core.FormValidation({
            container: this.get_dialog(),
            validators: [
                { input: this.dateText(), validator: ProspectDirector.Types.Core.Validators.date },
                { input: this.daysText(), validator: ProspectDirector.Types.Core.Validators.integer }
            ]
        });

        if (this._isSlidingScale()) {
            this.daysText().select();
            this.daysText().focus();
        }
        else {
            this.dateText().select();
            this.dateText().focus();
        }

    },

    /* control handles */

    dateText: function () {
        return this.get_dialog().find("input[type=text][data-id=date]");
    },

    daysText: function () {
        return this.get_dialog().find("input[type=text][data-id=days]");
    },

    dateRadio: function () {
        return this.get_dialog().find("input[type=radio][data-command=date]");
    },

    daysRadio: function () {
        return this.get_dialog().find("input[type=radio][data-command=days]");
    },

    /* control event handlers */

    _okButton_onClick: function () {

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

        switch (true) {

            case this.isValueOperator() && this._isSlidingScale():
                errors = this.validation.validate([this.daysText()]).errors;
                break;

            case this.isValueOperator() && !this._isSlidingScale():
                errors = this.validation.validate([this.dateText()]).errors;
                break;

        }

        if (errors.length === 0) {

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

        }

    },

    _operator_onChange: function () {

        var enabled = this.isValueOperator();

        this._enable(this.dateRadio(), enabled);
        this._enable(this.dateText(), enabled);
        this._enable(this.daysRadio(), enabled);
        this._enable(this.daysText(), enabled);

        if (enabled) {
            this._radio_onClick();
        }

    },

    _radio_onClick: function () {

        this._enable(this.dateText(), !this._isSlidingScale());
        this._enable(this.daysText(), this._isSlidingScale());

    },

    /* private methods */

    _isSlidingScale: function () {
        return this.get_dialog().find("input[type=radio][data-command=days]:checked").length > 0;
    },

    _getValue: function () {

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

        if (this._isSlidingScale()) {
            return this.daysText().val();
        }
        else {
            return this.dateText().val();
        };

    },

    _getVerse: function () {

        if (this._isSlidingScale()) {
            return this.daysText().val() + " days from today";
        }
        else {
            return null;
        };

    },

    _enable: function (element, enabled) {

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

    },

    _onkeypress: function (e) {
        if (e !== undefined
            && e !== null
            && e.keyCode === 13) {
            e.preventDefault();
            this._okButton_onClick();
        };
    },

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

});