package com.mason.server.model;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(detachable = "true")
public class ProjectAccess {

	public enum Role {
		NONE, READ, WRITE, ADMIN, OWNER
	}

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	private String generatedKey;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
		this.generatedKey = KeyFactory.createKeyString(
				Project.class.getSimpleName(), id);
	}

	public String getGeneratedKey() {
		if (this.generatedKey == null && id != null) {
			this.generatedKey = KeyFactory.createKeyString(
					ProjectAccess.class.getSimpleName(), id);
		}
		return generatedKey;
	}

	public void setGeneratedKey(String generatedKey) {
		if (id == 0) {
			this.id = KeyFactory.stringToKey(this.generatedKey).getId();
		}
		this.generatedKey = generatedKey;
	}

	@Persistent
	private Long projectId;
	
	private String projectIdGenerated;

	public Long getProjectId() {
		if (projectId == null && projectIdGenerated != null) {
			this.projectId = KeyFactory.stringToKey(projectIdGenerated).getId();
		}
		return projectId;
	}

	public void setProjectId(Long projectId) {
		this.projectId = projectId;
		this.projectIdGenerated = KeyFactory.createKeyString(
				ProjectAccess.class.getSimpleName(), projectId);
	}

	public String getProjectIdGenerated() {
		if (projectId != null && projectIdGenerated != null) {
			this.projectIdGenerated = KeyFactory.createKeyString(
					Project.class.getSimpleName(), projectId);
		}
		return projectIdGenerated;
	}

	public void setProjectIdGenerated(String projectIdGenerated) {
		this.projectIdGenerated = projectIdGenerated;
		if (projectId == null && projectIdGenerated != null) {
			this.projectId = KeyFactory.stringToKey(projectIdGenerated).getId();
		}
	}

	@Persistent
	private Role role;

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public static Map<String, String> getAccessRoles() {
		Map<String, String> types = new LinkedHashMap<String, String>();
		for (Role r : Role.values()) {
			if (r != Role.ADMIN && r != Role.OWNER ) {
				types.put(
						r.toString(),
						r.toString()
								.substring(0, 1)
								.toUpperCase()
								.concat(r.toString().substring(1).toLowerCase()
										.replace("_", " ")));
			}
		}
		return types;
	}

	@Persistent
	private Long userId;
	
	private String userIdGenerated;

	public Long getUserId() {
		if (userId == null && userIdGenerated != null) {
			this.userId = KeyFactory.stringToKey(userIdGenerated).getId();
		}
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
		this.userIdGenerated = KeyFactory.createKeyString(
				DbUser.class.getSimpleName(), userId);
	}

	public String getUserIdGenerated() {
		if (userId != null && userIdGenerated != null) {
			this.userIdGenerated = KeyFactory.createKeyString(
					DbUser.class.getSimpleName(), userId);
		}
		return userIdGenerated;
	}

	public void setUserIdGenerated(String userIdGenerated) {
		this.userIdGenerated = userIdGenerated;
		if (userId == null && userIdGenerated != null) {
			this.userId = KeyFactory.stringToKey(userIdGenerated).getId();
		}
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	private String username;
	
	

}
