package jondo;

import jondo.AbstractPasswordReader.DummyPasswordReader;
import anon.infoservice.ImmutableProxyInterface;
import anon.infoservice.ProxyInterface;
import anon.util.AbstractMemorizingPasswordReader;
import anon.util.IPasswordReader;

public class SimpleProxyInterface 
{
	public static final int PROTOCOL_TYPE_HTTP = ProxyInterface.PROTOCOL_TYPE_HTTP;
	public static final int PROTOCOL_TYPE_SOCKS = ProxyInterface.PROTOCOL_TYPE_SOCKS;
	
	private ProxyInterface m_proxyInterface;
	
	public SimpleProxyInterface(String a_strProxyHost, int a_proxyPort, 
			int a_protocol, final AbstractPasswordReader a_passwordReader)
	{

		IPasswordReader passwordReader = null;
		
		if (a_passwordReader != null)
		{
			passwordReader = new IPasswordReader()
			{
				public String readPassword(ImmutableProxyInterface a_proxyInterface)
				{
					AbstractMemorizingPasswordReader passwordReader = a_passwordReader.getPasswordReader();
					synchronized (passwordReader)
					{
						passwordReader.reset();
						// TODO translate
						return passwordReader.readPassword(
								"Please enter the authentication password for the proxy " +
								a_proxyInterface.getHost() + ":" + a_proxyInterface.getPort()  +
								" with user name '" + a_proxyInterface.getAuthenticationUserID() + "'.");
					}
				}
			};
		}
		
		m_proxyInterface = 
			new ProxyInterface(a_strProxyHost, a_proxyPort, a_protocol, passwordReader);
	}
	
	public SimpleProxyInterface(String a_strProxyHost, int a_proxyPort, 
			int a_protocol, String a_proxyPassword)
	{
		this(a_strProxyHost, a_proxyPort, a_protocol, 
				(a_proxyPassword == null || a_proxyPassword.trim().length() == 0? null :
			new AbstractPasswordReader.DummyPasswordReader(a_proxyPassword)));
	}
	
	public String getAuthenticationUserID()
	{
		return m_proxyInterface.getAuthenticationUserID();
	}
	
	public boolean isAuthenticationUsed()
	{
		return m_proxyInterface.isAuthenticationUsed();
	}
	
	public boolean setUseAuthentication(boolean a_bUseAuthentication)
	{
		return m_proxyInterface.setUseAuthentication(a_bUseAuthentication);
	}
	
	public void setAuthenticationUserID(String a_authenticationUserID)
	{
		m_proxyInterface.setAuthenticationUserID(a_authenticationUserID);
	}
	
	protected final ImmutableProxyInterface getProxyInterface()
	{
		return m_proxyInterface;
	}
	
	public final int getPort()
	{
		return m_proxyInterface.getPort();
	}
	
	public final String getProtocolAsString()
	{
		return m_proxyInterface.getProtocolAsString();
	}
	
	public final int getProtocol()
	{
		return m_proxyInterface.getProtocol();
	}
	
	public final String getHost()
	{
		return m_proxyInterface.getHost();
	}
	
	public final String toString()
	{	
		return m_proxyInterface.toString();
	}
}
