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

/**
@class FormatEditor
@namespace Web.UI.FeatureSets.Editors
@extends Web.UI.FeatureSets.Editors.CompositeEditor
@constructor
**/
ProspectDirector.Web.UI.FeatureSets.Editors.FormatEditor =
ProspectDirector.Web.UI.FeatureSets.Editors.CompositeEditor.extend({

    //==============================
    // FIELDS
    //==============================

    _scale: null,

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

    get_input: function () {
        return this.get_element().find("INPUT");
    },

    get_sample: function () {
        return this.get_element().find("SPAN");
    },

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

    _input_onKeyUp: function (e) {
        this.get_sample().text(this._getSampleValueFormatted());
    },

    _input_onBlur: function (e) {
        this.getPrimaryValue()["format"] = this.get_input().val();
    },

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

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

        var that = this;

        this.get_input().on("keyup", _.bind(this._input_onKeyUp, this));
        this.get_input().on("blur", _.bind(this._input_onBlur, this));

        this._scale = this._getFormatValue("scale");
        this.get_input().val(this.getPrimaryValue()["format"]);
        this.get_sample().text(this._getSampleValueFormatted());

        this.get_featureSet().on("change", function (sender, e) {
            if (e.key === "decimalPlaces") { // TODO: hardcoding this key needs to be changed to something more data driven
                that._scale = e.newValue == "" ? 0 : e.newValue;
                that.get_sample().text(that._getSampleValueFormatted());
            }
        });

    },

    updateUI: function (value) {
        that.get_sample().text(that._getSampleValueFormatted());
    },

    render: function () {
        return "<div id='" + this.getId() + "' class='-pd-editor-format'>" +
                    "<span>0000000000000000</span>" +
                    "<input type='text'>" +
                "</div>";
    },

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

    _getSampleValue: function () {

        var type = this._getFormatValue("type");
        var scale = this._scale;
        var result = null;

        switch (type) {

            case 0: //datetime
                result = new Date(2010, 9, 31);
                break;

            case 1: //decimal

                if (scale > 16) {
                    scale = 16;
                }

                if (scale >= 0) {
                    result = ("999999999".substr(0, 16 - scale)) * 1;
                }

                break;

            case 2: //integer
                result = 1750000;
                break;

        }

        return result;

    },

    _getSampleValueFormatted: function () {

        var sample = this._getSampleValue();
        var format = this.get_input().val();
        var type = this._getFormatValue("type");
        var scale = this._scale;
        var result = null;

        switch (type) {

            case 0: //datetime
                format = (!format || format === "" ? "d" : format);
                break;
            case 1: //decimal
                format = (!format || format === "" || format === "g" ? "n" + scale : format);
                break;
            case 2: //integer
                format = (!format || format === "" || format === "g" ? "n0" : format);
                break;

        }

        try {
            result = kendo.toString(sample, format.toString());
        }
        catch (ex) {
            result = null;
        }

        if (!result || result === "") {
            this._showError("Invalid Format");
            return;
        }
        else {
            this._hideError();
        }

        return result;

    },

    _showError: function (message) {
        this.get_sample().text(message);
        this.get_sample().css({
            color: "white",
            backgroundColor: "darkred"
        });
    },

    _hideError: function (message) {
        this.get_sample().css({
            color: "black",
            backgroundColor: "#dbffce"
        });
    },

    _getFormatValue: function (key) {
        return this.getPrimaryValue()[key];
    }

});