package loginModule;
import java.util.*;
import java.io.*;
import java.net.*;
import utility.*;


public class LoginServerHandler extends ServerHandler
{

	Vector serverInfo = new Vector();
	Vector agentInfo = new Vector();
	Query query = new Query();
  	public LoginServerHandler()
	{
		setServerInfo();
	}


	public boolean authenticate (LoginInfo logInfo)
	{
		boolean accept= false;
		String loginId = logInfo.getLoginId();
		String loginPassword = logInfo.getLoginPassword();

		ResultSelect rs = null;
		rs = query.selectQuery("select * from loginInfo where LoginId='"+loginId+"' and LoginPassword='"+loginPassword+"'");
		if (rs.rowVector.size()!=1)
		{
			System.out.println("bad login");
			accept=false;
			return accept;
		}
		if ( loginId.equals (((java.util.Vector)rs.rowVector.elementAt(0)).elementAt(0)) && loginPassword.equals(((java.util.Vector)rs.rowVector.elementAt(0)).elementAt(1)))
			accept=true;
		return accept;
	}
	public void doThis(Request req,AgentIdentity id )
	{
		if(req.getRequestType().compareTo("LoginInfo") == 0)
		{
			LoginInfo logInfo = (LoginInfo)req.getObject();
			System.out.println("Got login Info");
			boolean accept = authenticate(logInfo);
			boolean success = false;
			if(accept)
			{

				id.setLoginName(logInfo.getLoginId());
				if(query.updateQuery("INSERT INTO loginStatus values (  '"+logInfo.getLoginId()+"' ,"+id.getId()+")") ==0)
					accept = false ;
				if(accept)
				{
					System.out.println("name :"+id.getLoginName());
					LoginResponse logRes = new LoginResponse(id);
					Response res = new Response(logRes,"LoginAccepted");
					ServerHeader toBeSent = new ServerHeader(res);
					try
					{
						success =sendResponse(toBeSent,id.getId());
					}
					catch (Exception e)
					{
						success = false;
					}
					if(success)
						System.out.println("Login Response sent to "+id.getLoginName());
					else
						System.out.println("cudn't sent reply to"+id.getLoginName());
					res = new Response(serverInfo,"ServerInfo");
					toBeSent = new ServerHeader(res);
					try
					{
						success =sendResponse(toBeSent,id.getId());
					}
					catch (Exception e)
					{
						success = false;
					}
					if(success)
						System.out.println("Server Info  sent to "+id.getLoginName());
					else
						System.out.println("cudn't sent reply to"+id.getLoginName());

					res = new Response(id.getLoginName()+" : Logged In","Allert");
					toBeSent = new ServerHeader(res);
					try
					{
						broadcast(toBeSent);
					}
					catch (Exception e)
					{
						success = false;
					}
					setAgentInfo();
					res = new Response(agentInfo,"AgentInfo");
					toBeSent = new ServerHeader(res);
					try
					{
						success =broadcast(toBeSent);
					}
					catch (Exception e)
					{
						success = false;
					}
					if(success)
						System.out.println("AgentInfo Info  sent to "+id.getLoginName());
					else
						System.out.println("cudn't sent reply to"+id.getLoginName());

				}
			}
			if(! accept)
			{
				Response res = new Response(null,"LoginRejected");
				ServerHeader toBeSent = new ServerHeader(res);
				try
				{
					success = sendResponse(toBeSent,id.getId());
				}
				catch (Exception e)
				{
					success = false;
				}
				if(success)
					System.out.println("Response sent to "+id.getLoginName());
				else
					System.out.println("cudn't sent reply to"+id.getLoginName());
				removeConnection(id.getId());
			}
		}
		else
		if(req.getRequestType().compareTo("RemoveConnection") == 0)
		{
			RemoveConnection rmc = (RemoveConnection) req.getObject();
			System.out.println("got req of logOff from : "+rmc.id.getLoginName());
			removeConnection(rmc.id);
			setAgentInfo();
			Response res = new Response(agentInfo,"AgentInfo");
			ServerHeader toBeSent = new ServerHeader(res);
			boolean success = false;
			try
			{
				success =broadcast(toBeSent);
			}
			catch (Exception e)
			{
				success = false;
			}
			if(success)
				System.out.println("AgentInfo Info  sent to "+id.getLoginName());
			else
				System.out.println("cudn't sent reply to"+id.getLoginName());
			
			res = new Response(id.getLoginName()+" : Logged Off","Allert");
			toBeSent = new ServerHeader(res);
			try
   			{
				broadcast(toBeSent);
			}
			catch (Exception e)
			{
				success = false;
			}
		}

		else
			System.out.println(" in doThis method: unknown request :"+req.getRequestType());
	}


	void setAgentInfo()
	{
		ResultSelect rs = query.selectQuery("SELECT  LoginName FROM loginStatus");
		int i =0;
		int j =rs.rowVector.size();
		agentInfo.removeAllElements();
		for (i =0;i<j;++i)
		{
			agentInfo.addElement((String)((Vector)rs.rowVector.elementAt(i)).elementAt(0));
		}
	}


	void setServerInfo()
	{
		Properties prop[] = new Properties[7];
		prop[0] = new Properties();
		prop[0].setProperty("serverName","chatServer");
		prop[0].setProperty("serverIP","172.19.0.1");
		prop[0].setProperty("portNo","8000");
		serverInfo.addElement(prop[0]);
		prop[1] = new Properties();
		prop[1].setProperty("serverName","wbServer");
		prop[1].setProperty("serverIP","172.19.0.1");
		prop[1].setProperty("portNo","9000");
		serverInfo.addElement(prop[1]);
		prop[2] = new Properties();
		prop[2].setProperty("serverName","nbServer");
		prop[2].setProperty("serverIP","172.19.0.1");
		prop[2].setProperty("portNo","6050");
		serverInfo.addElement(prop[2]);
		prop[3] = new Properties();
		prop[3].setProperty("serverName","nbDataServer");
		prop[3].setProperty("serverIP","172.19.0.1");
		prop[3].setProperty("portNo","7050");
		serverInfo.addElement(prop[3]);
		prop[4] = new Properties();
		prop[4].setProperty("serverName","fileServer");
		prop[4].setProperty("serverIP","172.19.0.1");
		prop[4].setProperty("portNo","8050");
		serverInfo.addElement(prop[4]);
		prop[5] = new Properties();
		prop[5].setProperty("serverName","fsDataServer");
		prop[5].setProperty("serverIP","172.19.0.1");
		prop[5].setProperty("portNo","9050");
		serverInfo.addElement(prop[5]);
		prop[6] = new Properties();
		prop[6].setProperty("serverName","schServer");
		prop[6].setProperty("serverIP","172.19.0.1");
		prop[6].setProperty("portNo","9075");
		serverInfo.addElement(prop[6]);
	}

	synchronized public boolean removeConnection(AgentIdentity id)
	{
    		boolean success = false;
    		ConnectionHandler connHandler = (ConnectionHandler) connectionHandlers.remove(new Integer(id.getId()));
		Connection conn = (Connection)connections.remove(new Integer(id.getId()));
		if (connHandler != null && conn != null)
		{
      			connHandler.stop= true;
      			success = true;
    			/* if  some problem abt closing conn comment code below*/
			try
			{
				conn.in.close();
				conn.out.close();

			}
			catch(Exception e)
			{
				System.out.println("cudn't close connection");
				success = false;
			}
		}
		query.updateQuery("delete from loginStatus where LoginName = '"+id.getLoginName()+"'");
		return success;
	}

}
