package com.blunk.communication;



import com.blunk.Log;

import com.blunk.net.TcpConnection;

public class CommunicationHandler
{
	public final int clientID;
	private final TcpConnection connection;
	private final ClientRequestHandlerManager requestManager;
	
	public ClientMessage request;
	public ServerMessage response;
	public String keyEncrypted;
	

	
	
	public CommunicationHandler(TcpConnection conn)
	{
		this.clientID = conn.clientID;
		this.connection = conn;
		this.response = new ServerMessage();
		this.requestManager = new ClientRequestHandlerManager(this);
	}
	
	/**
	 * Tries to send the current ServerMessage (response) in the CommunicationHandler to the
	 * underlying TcpConnection.
	 * 
	 * @see sendMessage(ServerMessage)
	 */
	public void sendResponse()
	{
		sendMessage(this.response);
	}
	
	/**
	 * Tries to send a given ServerMessage object to the underlying TcpConnection.
	 * 
	 * @param msg The ServerMessage object representing the message to send.
	 */
	public void sendMessage(ServerMessage msg)
	{
		if (msg != null && connection != null)
			connection.sendChars(msg.getResult());
	}
	
	public void handleClientMessage(ClientMessage msg)
	{
		// Log message
		Log.info(this.connection.toString() + " <-- " + msg.toString());
		
		// Handle message
		requestManager.handleRequest(msg);
	}
	
	public void Login(String name, String password)
	{
		
	}
	
	
	
	
	
	public TcpConnection getConnection()
	{
		return this.connection;
	}
}
