import Ember from 'ember';

import _validate from 'ember-simple-validate/utils/validate';
import Validator from 'ember-simple-validate/lib/validator';

var get = Ember.get;
var set = Ember.set;

var validValidator = function(validator) {
  return validator instanceof Validator;
};

var appendErrors = function(obj, key, errors) {
  var keyParts = key.split('.');
  var path = 'validationErrors';

  if(errors && errors.length) {
    keyParts.forEach(function(key, i) {
      path += '.' + key;

      if(!get(obj, path)) {
        var blank = i === keyParts.length - 1 ? Ember.A() : {};

        set(obj, path, blank);
      }
    });

    errors.forEach(function(error) {
      get(obj, 'validationErrors.' + key).pushObject(error);
    });
  }
};

export default Ember.Mixin.create({
  init: function() {
    this._super();
    set(this, 'isValid', undefined);
  },

  validate: function() {
    set(this, 'validationErrors', {});

    if(!this.canValidate()) {
      set(this, 'isValid', true);
    } else {
      var self = this;

      var validators = get(this, 'validators');

      set(this, 'isValid', _validate(this, validators));

      Ember.keys(validators).forEach(function(key) {
        Ember.makeArray(validators[key]).forEach(function(validator) {
          appendErrors(self, key, get(validator, 'errors'));
        });


      });
    }

    return this.get('isValid');
  },

  canValidate: function() {
    var validators = get(this, 'validators') || {};
    var canValidate = true;

    var keys = Ember.keys(validators);

    if(keys && keys.length) {
      keys.forEach(function(key) {
        var validatorList = Ember.makeArray(validators[key]);

        validatorList.forEach(function(validator) {
          if(!validValidator(validator)) {
            canValidate = false;
            return false;
          }
        });
      });
    } else {
      canValidate = false;
    }

    return canValidate;
  }
});

@@ -280,476 +280,8 @@
 %7D;%0A%0A
-var appendErrors = function(obj, key, errors) %7B%0A  var keyParts = key.split('.');%0A  var path = 'validationErrors';%0A%0A  if(errors && errors.length) %7B%0A    keyParts.forEach(function(key, i) %7B%0A      path += '.' + key;%0A%0A      if(!get(obj, path)) %7B%0A        var blank = i === keyParts.length - 1 ? Ember.A() : %7B%7D;%0A%0A        set(obj, path, blank);%0A      %7D%0A    %7D);%0A%0A    errors.forEach(function(error) %7B%0A      get(obj, 'validationErrors.' + key).pushObject(error);%0A    %7D);%0A  %7D%0A%7D;%0A%0A
 expo
@@ -806,21 +806,23 @@
     
-appen
+self.ad
 dErrors(
 self
@@ -821,14 +821,8 @@
 ors(
-self, 
 key,
@@ -1422,32 +1422,32 @@
 = false;%0A    %7D%0A%0A
-
     return canVa
@@ -1454,16 +1454,579 @@
 lidate;%0A
+  %7D,%0A%0A  addErrors: function(key, errors) %7B%0A    var self = this;%0A%0A    Ember.makeArray(errors).forEach(function(error) %7B%0A      self.addError(key, error);%0A    %7D);%0A  %7D,%0A%0A  addError: function(key, error) %7B%0A    var self = this;%0A%0A    var keyParts = key.split('.');%0A    var path = 'validationErrors';%0A%0A    keyParts.forEach(function(key, i) %7B%0A      path += '.' + key;%0A%0A      if(!get(self, path)) %7B%0A        var blank = i === keyParts.length - 1 ? Ember.A() : %7B%7D;%0A%0A        set(self, path, blank);%0A      %7D%0A    %7D);%0A%0A    get(this, 'validationErrors.' + key).pushObject(error);%0A
   %7D%0A%7D);%0A

Add error adding functions to the mixin API