App.EditCurrencyComponent = Ember.Component.extend
  classNames: ['edit-currency-container', 'row', 'collapse']
  classNameBindings: ['has_error:error']
  currencySymbol: '€'
  has_error: false
  placeholder: '0.00'
  validatePresence: false

  inputValue: Ember.computed "value",
    get: ->
      return_value = @get "value"
      @set 'has_error', true if @validatePresence and !return_value
      return_value = return_value.toFixed(2) if typeof return_value is 'number'
      return_value
    set: (key, value, previousValue)->
      if value
        # if typeof value is 'number'
        #   return_value = value.toPrecision()
        #   @set key, value
        if typeof value is 'string' and value.match(/^[+-]?\d+(\.?\d?\d)?$/)
          @set 'has_error', false
          @set "value", parseFloat(value)
        else
          @set 'has_error', true

      else
        @set "value", 0.0 # empty
        @set 'has_error', false
      value

  actions:
    action: ->
      @sendAction('action') if @action
  #didInsertElement: ->
    #@addObserver "model.#{@attribute}", (attribute)=>
      #if value = @get("model.#{@attribute}")
        #@set 'value', value.toFixed(2) if parseFloat(@get('value')) isnt value
      #else
        #@set 'value', 0
      ## @set('value', @get("model.#{@attribute}").toFixed)
    ##   # dynamically observe the model's attribute
    ##   # if this changes outside the component's context, it is not
    ##   # observed by the computed property. model.@attribute is not (yet) working :)
    ##   current_value = @get 'value'
    ##   if value = @get("model.#{@attribute}")
    ##     string_value = value.toPrecision()
    ##     @set('value', string_value) if parseFloat(current_value) isnt @get("model.#{@attribute}")
    #@addObserver "model.errors.#{@attribute}.length", (attribute)=>
      #@set 'has_error', !!@get("model.errors.#{@attribute}.length")
    #@model.validate().then =>
      #@set 'has_error', !!@get("model.errors.#{@attribute}.length")

