import Ember from 'ember'
const {
  Component,
  computed
} = Ember
const {
  readOnly
} = computed
import layout from '../templates/components/frost-radio-button'

export default Component.extend({
  attributeBindings: [
    'tabindex'
  ],
  classNames: [
    'frost-radio-button'
  ],
  classNameBindings: [
    'checked',
    'disabled',
    'required'
  ],
  layout,
  // TODO PropTypes
  required: false,
  disabled: false,

  groupId: readOnly('parentView.id'),
  groupValue: readOnly('parentView.value'),
  onChange: readOnly('parentView.onChange'),

  checked: computed('groupValue', 'value', function () {
    return this.get('groupValue') === this.get('value')
  }),

  tabindex: Ember.computed('disabled', function () {
    return this.get('disabled') ? -1 : 0
  }),

  init () {
    this._super(...arguments)
    Ember.assert(
      `${this.toString()} must be initialized in the yield block of 'frost-radio-group'`,
      /frost-radio-group/.test(this.parentView.toString()))
    Ember.assert(
      `${this.toString()} must be initialized with a 'value' property`,
      this.get('value')
    )
  },
  keyPress (e) {
    e.preventDefault()
    e.stopPropagation()
    
    let event = Ember.$.Event(null, e)
    let target = Ember.$.clone(Ember.$(e.target).find('input')[0])
    let change = this.get('onChange')
   
    if (e.keyCode === 13) {
      if (!this.get('disabled')) {
        this.set('parentView.value', this.get('value'))
        target.id = this.get('groupId')
        event.target = target
        if (change && typeof change === 'function') {
          change(event)
        }
      }
    }
    return false
  }
})

@@ -1172,20 +1172,116 @@
 ation()%0A
+%0A
     
+if (this.get('disabled') %7C%7C this.get('groupValue') === this.get('value')) %7B%0A      return%0A    %7D%0A
 %0A    let
@@ -1421,12 +1421,9 @@
 e')%0A
-   
 %0A
+
     
@@ -1450,45 +1450,8 @@
 ) %7B%0A
-      if (!this.get('disabled')) %7B%0A  
     
@@ -1500,18 +1500,16 @@
 alue'))%0A
-  
       ta
@@ -1544,18 +1544,16 @@
 )%0A      
-  
 event.ta
@@ -1572,18 +1572,16 @@
 t%0A      
- 
- 
 if (chan
@@ -1626,18 +1626,16 @@
         
-  
 change(e
@@ -1644,18 +1644,8 @@
 nt)%0A
-        %7D%0A
     

check to prevent multiple firing