App.LoginCredentials = Ember.Object.extend(Ember.Validations.Mixin, {
  username: "guest",
  password: "guest",
  validations: {
    username: {
      presence: true,
      length: { minimum: 4 }
    },
    password: {
      presence: true,
      length: { minimum: 5 }
    }
  }
});

App.RegistrationCredentials = Ember.Object.extend(Ember.Validations.Mixin, {
  username: null,
  password: null,
  passwordConfirmation: null,
  email: null,
  emailConfirmation: null,
  firstname: null,
  lastname: null,
  role: null,
    serialized: function(){
	var self = this;
	return ['username','password','email','firstname','lastname','role'].reduce(function(acc,name){
	    acc[name] = self.get(name);
	    return acc;
	},{});	    
    }.property('username','password','email','firstname','lastname','role'),

    auth: null,
    save: function(){
	var self = this;
	return new Ember.RSVP.Promise(function(resolve,reject){
	    Ember.$
		.ajax(App.RegistrationCredentials.url,{data:self.get('serialized'),type:"POST",dataType:"JSON"}).done(function(data){
		    resolve(data.user);
		}).fail(function(jq){
		    reject(jq.responseJSON);
		});
	});
    },
  
  validations: {
    username: {
      presence: true,
      length: { minimum: 4 }
    },
    password: {
      presence: true,
      confirmation: true,
      length: { minimum: 8 }
    },
    email: {
      presence: true,
      confirmation: true,
      format: { 
        with: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, 
        allowBlank: false,
        message: Ember.I18n.translations.errors.invalid_email
      }
    },
    firstname: {
      presence: true
    },
    lastname: {
      presence: true
    }
  }
});

App.RegistrationCredentials.url = "http://localhost:8888/register";

App.EmailCredentialsObject = Ember.Object.extend(Ember.Validations.Mixin, {
  username: "",
  password: "",
  email: "",
  emailConfirmation: "",
  
  validations: {
    username: {
      presence: true,
      length: { minimum: 4 }
    },
    password: {
      presence: true
    },
    email: {
      presence: true,
      confirmation: true,
      format: { 
        with: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, 
        allowBlank: false,
        message: Ember.I18n.translations.errors.invalid_email
      }
    }
  }
});

App.NewPasswordCredentialsObject = Ember.Object.extend(Ember.Validations.Mixin, {
  username: "",
  password: "",
  new_password: "",

  validations: {
    username: {
      presence: true,
      length: { minimum: 4 }
    },
    password: {
      presence: true,
      length: { minimum: 5 }
    },
    new_password: {
      presence: true,
      confirmation: true,
      length: { minimum: 5 }
    }
  }
});
