package jondo;

import anon.infoservice.Database;
import anon.infoservice.MixCascade;
import anon.infoservice.StatusInfo;

public class MixServiceSecurity 
{
	public static int SECURITY_DISTRIBUTION_MAX = MixCascade.DISTRIBUTION_MAX;
	public static int SECURITY_DISTRIBUTION_MIN = MixCascade.DISTRIBUTION_MIN;
	public static int SECURITY_USERS_MAX = StatusInfo.ANON_LEVEL_MAX;
	public static int SECURITY_USERS_MIN = StatusInfo.ANON_LEVEL_MIN;
	
	private MixServiceInfo m_service;
	
	protected MixServiceSecurity(MixServiceInfo a_service)
	{
		if (a_service == null)
		{
			throw new NullPointerException();
		}
		m_service = a_service;
	}
	
	/**
	 * The user level of a cascade. This is a security attribute. The higher 
	 * the user level, the more secure this cascade is.
	 * @return the distribution of a cascade; ranges from SECURITY_USERS_MIN (worst value) 
	 * to SECURITY_USERS_MAX (best value); if it is < SECURITY_USERS_MIN, than no information is available
	 */
	public int getSecurityUsers()
	{
		StatusInfo status = (StatusInfo)Database.getInstance(StatusInfo.class).getEntryById(
				m_service.getMixCascade().getId());
		if (status == null)
		{
			return SECURITY_USERS_MIN - 1;
		}
		return status.getAnonLevel();
	}
	
	/**
	 * The distribution points of a Cascade. This is a security attribute. The higher 
	 * the distribution, the more secure this cascade is.
	 * @return the distribution of a cascade; ranges from SECURITY_DISTRIBUTION_MIN (worst value) 
	 * to SECURITY_DISTRIBUTION_MAX (best value)
	 */
	public int getSecurityDistribution()
	{
		return m_service.getMixCascade().getDistribution();
	}
	
	public MixServiceInfo getServiceInfo()
	{
		return m_service;
	}
	
	/**
	 * Writes the security values to a single string, that might be presented in a user interface.
	 * @return the security values to a single string
	 */
	public String toString()
	{
		int securityUsers = getSecurityUsers();
		int securityDistribution = getSecurityDistribution();
		String strSecurity = "";
		
		if (securityDistribution > SECURITY_DISTRIBUTION_MIN)
		{		
			strSecurity += getSecurityDistribution() + ",";
			
			if (securityUsers >= SECURITY_USERS_MIN)
			{
				strSecurity += securityUsers;
			}
			else
			{
				strSecurity += "?";
			}
			strSecurity += "/" + SECURITY_DISTRIBUTION_MAX + "," + SECURITY_USERS_MAX;
			
			return strSecurity;
		}
		
		return null;
	}
}
