import Ember from 'ember';
import WidgetModel from 'ember-eureka/widget-model';

export default WidgetModel.extend({

    /** if false, display the save button
     */
    isEmbedded: Ember.computed.bool('config.hideControlButtons'),
    model: Ember.computed.alias('routeModel'),

    fieldNames: Ember.computed.alias('config.fields'),

    /** display only the fields specified in `config.fields`
     * If `config.fields` doesn't exists, display all model's fields
     */
    fields: function() {
        var fieldNames = this.get('fieldNames');
        var fields, field;
        if (fieldNames) {
            var model = this.get('model');
            fields = Ember.A();
            fieldNames.forEach(function(fieldName) {
                field = model.get(fieldName+'Field');
                fields.pushObject(field);
            });
        } else {
            fields = this.get('model._fields');
        }

        return fields;
    }.property('fieldNames.[]', 'model._fields'),

    label: Ember.computed.alias('config.label'),

    actions: {
        save: function() {
            var model = this.get('model');
            var that = this;
            var routePath = this.getWithDefault('config.actions.save.transitionTo', model.get('meta.modelIndexViewPath'));
            model.save().then(function(m) {
                var payload = {model: m, routePath: routePath};
                that.sendAction('toControllerAction', {name: 'refreshModel'});
                that.sendAction('toControllerAction', {name: 'transitionTo', payload: payload});
            });
        },
        cancel: function() {
            var model = this.get('model');
            model.rollback();
            var routePath = this.get('config.actions.cancel.transitionTo');
            if (!routePath) {
                if (model.get('_id')) {
                    routePath = model.get('meta.modelIndexViewPath');
                } else {
                    routePath = model.get('meta.collectionIndexViewPath');
                }
            }
            var payload = {model: model, routePath: routePath};
            this.sendAction('toControllerAction', {name: 'transitionTo', payload: payload});
        }
    },

    _focusOnFirstElement: function() {
        this.$('input:eq(0)').focus();
    }.on('didInsertElement'),

    _focusOnNextElement: function() {
        // XXX check li
        this.$().closest('li').next().find('input').focus();
    }.on('willDestroyElement')
});

@@ -480,16 +480,65 @@
  fields:
+ Ember.computed('fieldNames.%5B%5D', 'model._fields',
  functio
@@ -993,50 +993,8 @@
    %7D
-.property('fieldNames.%5B%5D', 'model._fields'
 ),%0A%0A
@@ -2215,24 +2215,27 @@
 %0A    %7D,%0A%0A   
+ //
  _focusOnFir
@@ -2236,32 +2236,61 @@
 sOnFirstElement:
+ Ember.on('didInsertElement',
  function() %7B%0A  
@@ -2286,24 +2286,27 @@
 tion() %7B%0A   
+ //
      this.$(
@@ -2337,38 +2337,22 @@
     
-%7D.on('didInsertElement'
+// %7D
 ),%0A%0A   
+ //
  _fo
@@ -2368,16 +2368,47 @@
 Element:
+ Ember.on('willDestroyElement',
  functio
@@ -2417,20 +2417,23 @@
 ) %7B%0A    
+//
     
+ 
 // XXX c
@@ -2439,24 +2439,27 @@
 check li%0A   
+ //
      this.$(
@@ -2512,33 +2512,12 @@
     
-%7D.on('willDestroyElement'
+// %7D
 )%0A%7D)

remove auto-focus