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 UmlModel {

	enum Type {
		MIND_MAP, UML_MODEL;
	}

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

	private String generatedKey;

	public Long getId() {
		if (id == null && generatedKey != null) {
			this.id = KeyFactory.stringToKey(generatedKey).getId();
		}
		return id;
	}

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

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

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

	@Persistent
	private String slug;

	public String getSlug() {
		return slug;
	}

	public void setSlug(String slug) {
		this.slug = slug;
	}

	@Persistent
	private String description;

	@Persistent
	private Long projectId;

	@Persistent
	private Type type;

	private String projectIdGenerated;

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

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

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

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

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

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public static Map<String, String> getModelTypes() {
		Map<String, String> types = new LinkedHashMap<String, String>();
		for (Type t : Type.values()) {
			types.put(
					t.toString(),
					t.toString()
							.substring(0, 1)
							.toUpperCase()
							.concat(t.toString().substring(1).toLowerCase()
									.replace("_", " ")));
		}
		return types;
	}

}
