package de.thomasjacob.improve.logic.user;

import de.thomasjacob.improve.logic.issue.Issue;
import de.thomasjacob.improve.logic.project.Project;
import de.thomasjacob.webcore.logic.Model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class User extends Model
{
	private boolean administrator = false;

	private String email = "";

	private boolean enabled = false;

	private boolean guest = false;

	private String name = "";

	private String password = "";

	private Map roles = new HashMap();

	public String getEmail()
	{
		return this.email;
	}

	public String getName()
	{
		return this.name;
	}

	public String getPassword()
	{
		return this.password;
	}

	public Role getRole(Project project)
	{
		return (Role) roles.get(project);
	}

	public Map getRoles()
	{
		return this.roles;
	}

	public String getTypeName()
	{
		if (administrator)
		{
			return "Administrator";
		}
		else if (isManager())
		{
			return "Manager";
		}
		else if (isDeveloper())
		{
			return "Developer";
		}
		else if (guest)
		{
			return "Guest";
		}
		else if (isReporter())
		{
			return "Reporter";
		}
		else
		{
			return "-";
		}
	}

	public boolean isAdministrator()
	{
		return this.administrator;
	}

	public boolean isDeveloper()
	{
		Iterator iterator = roles.values().iterator();
		while (iterator.hasNext())
		{
			Role role = (Role) iterator.next();
			if (role.isDeveloper())
			{
				return true;
			}
		}

		return false;
	}

	public boolean isEnabled()
	{
		return this.enabled;
	}

	public boolean isGuest()
	{
		return this.guest;
	}

	public boolean isManager()
	{
		Iterator iterator = roles.values().iterator();
		while (iterator.hasNext())
		{
			Role role = (Role) iterator.next();
			if (role.isManager())
			{
				return true;
			}
		}

		return false;
	}

	public boolean isReporter()
	{
		Iterator iterator = roles.values().iterator();
		while (iterator.hasNext())
		{
			Role role = (Role) iterator.next();
			if (role.isReporter())
			{
				return true;
			}
		}

		return false;
	}

	public boolean mayEditIssue(Project project)
	{
		Role role = getRole(project);
		if (role == null)
		{
			return false;
		}

		return role.isManager();
	}

	public boolean mayProcess(Issue issue, boolean assumeAssigned)
	{
		if (issue == null || issue.getProject() == null)
		{
			return false;
		}

		Role role = getRole(issue.getProject());
		if (role == null)
		{
			return false;
		}

		if (role.isManager())
		{
			return true;
		}

		if (role.isDeveloper())
		{
			return (issue.getDeveloper() == this || assumeAssigned) && issue.getStatus() == Issue.STATUS_OPEN;
		}

		return false;
	}

	public boolean mayReportIssue(Project project)
	{
		Role role = getRole(project);
		if (role == null)
		{
			return false;
		}

		return role.isReporter();
	}

	public void setAdministrator(boolean administrator)
	{
		this.administrator = administrator;
	}

	public void setEmail(String email)
	{
		if (email == null)
		{
			email = "";
		}

		this.email = email;
	}

	public void setEnabled(boolean enabled)
	{
		this.enabled = enabled;
	}

	public void setGuest(boolean guest)
	{
		this.guest = guest;
	}

	public void setName(String name)
	{
		if (name == null)
		{
			name = "";
		}

		this.name = name;
	}

	public void setPassword(String password)
	{
		if (password == null)
		{
			password = "";
		}

		this.password = password;
	}

	public void setRoles(Map roles)
	{
		this.roles = roles;
	}
}