package models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;

import play.Logger;
import utils.DataStore;
import utils.HashUtils;

import com.vividsolutions.jts.geom.Geometry;


public class User implements Serializable {

	private static final long serialVersionUID = 1L;

	static private DataStore<User> userData = new DataStore<User>("users");
	
	public String id;
	public String username;
	public String passwordHash;
	public String email;
	
	public Boolean active;
	public Boolean admin;
	
	public ArrayList<ProjectPermissions> projectPermissions;

	public User(String username, String password, String email) {
		
		this.username = username.toLowerCase();
		this.email = email;
		this.active = true;
		this.admin = false;
		
		try {
			
			byte[] bytesOfMessage = password.getBytes("UTF-8");	
			
			this.passwordHash = DigestUtils.shaHex(bytesOfMessage);
			
		}
		catch(Exception e) {
			
			this.active = false;
			this.passwordHash = "";
		}
		
	}
	
	public void save() {
		
		// assign id at save
		if(id == null || id.isEmpty()) {
			
			Date d = new Date();
			id = getUserId(this.username);
			
			Logger.info("created user u " + id);
		}
		
		userData.save(id, this);
		
		Logger.info("saved user u " +id);
	}
	
	public void delete() {
		userData.delete(id);
		
		Logger.info("delete user u " +id);
	}

	public Boolean checkPassword(String password) {	
		try {
			
			byte[] bytesOfMessage = password.getBytes("UTF-8");	
			
			String pHash = DigestUtils.shaHex(bytesOfMessage);
			
			return pHash.equals(this.passwordHash);
			
		}
		catch(Exception e) {
			
			return false;
		}
	}
	
	static public String getUserId(String username) {
		return HashUtils.hashString("u_" + username);
	}

	static public User getUser(String id) {
		
		return userData.getById(id);	
	}
	
	static public User getUserByUsername(String username) {
		
		return userData.getById(getUserId(username));	
	}
	
	static public Collection<User> getProjects() {
		
		return userData.getAll();
		
	}
	
	static class ProjectPermissions {
		
		String project_id;
		Boolean read;
		Boolean write;
		Boolean admin;
		
	}

}

@@ -216,16 +216,34 @@
 Logger;%0A
+import play.Play;%0A
 import u
@@ -766,16 +766,33 @@
 g email)
+ throws Exception
  %7B%0A%09%09%0A%09%09
@@ -831,16 +831,189 @@
 Case();%0A
+%09%09%0A%09%09// check for duplicate usernames%0A%09%09if(userData.getById(getUserId(this.username)) != null)%0A%09%09%09%09throw new Exception(%22Username %22 + this.username + %22 already exists%22); %0A%09%09%0A
 %09%09this.e
@@ -1108,32 +1108,33 @@
 OfMessage = 
+(
 password
 .getBytes(%22U
@@ -1113,32 +1113,102 @@
 sage = (password
+ + Play.application().configuration().getString(%22application.secret%22))
 .getBytes(%22UTF-8
@@ -1361,24 +1361,26 @@
 h = %22%22;%0A%09%09%7D%0A
+%09%0A
 %09%09%0A%09%7D%0A%09%0A%09pub
@@ -1822,24 +1822,25 @@
 ssage = 
+(
 password
 .getByte
@@ -1831,16 +1831,86 @@
 password
+ + Play.application().configuration().getString(%22application.secret%22))
 .getByte

update password hash