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

public class WbServerHandler extends ServerHandler
{

	Query query = new Query();
	public WbServerHandler()
	{

	}

	boolean authenticate(AgentIdentity id)
	{
		boolean accept = false;
		String loginName = id.getLoginName();
		int connectionId = id.getId();

		ResultSelect rs = null;
		rs = query.selectQuery("select * from loginStatus where LoginName='"+loginName+"' and ConnectionId="+connectionId);
		if (rs.rowVector.size()!=1)
		{
			accept=false;
			return accept;
		}
		try
		{
			if ( loginName.equals (((java.util.Vector)rs.rowVector.elementAt(0)).elementAt(0)) && connectionId==(Integer.parseInt((String)((java.util.Vector)rs.rowVector.elementAt(0)).elementAt(1))))
				accept=true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return accept;
	}


	public void doThis(Request req,  AgentIdentity id)
	{

		if(req.getRequestType().compareTo("Add") == 0)
		{
			AgentIdentity aid = (AgentIdentity)req.getObject();
			boolean accept = authenticate(aid);
			if(accept)
			{
				id= aid;
				Response res = new Response(id,"AddAccepted");
				ServerHeader toBeSent = new ServerHeader(res);
				try
				{
					sendResponse(toBeSent,id.getId());
				}
				catch(Exception e)
				{
					System.out.println("couldn't sent "+ e);
				}
			}
			else
			{
				Response res = new Response(null,"AddRejected");
				ServerHeader toBeSent  = new ServerHeader(res);
				try
				{
					sendResponse(toBeSent,id.getId());
				}
				catch(Exception e)
				{
					System.out.println("couldn't sent "+ e);
				}
			}
		}
		else

		if(req.getRequestType().compareTo("StartPoint") == 0)
		{
			WbPoint pt = (WbPoint)req.getObject();
			//System.out.println("Start Point  from :" + pt.id.getLoginName());
			Response res = new Response(req.getObject(),"StartPoint");
			ServerHeader sh = new ServerHeader(res);
			try
			{
				broadcast(sh);
			}
			catch(Exception e)
			{
				System.out.println("couldn't sent :"+e);
			}

		}
		else
		if(req.getRequestType().compareTo("DragPoint") == 0)
		{
			WbPoint pt = (WbPoint)req.getObject();
			//System.out.println("Drag Point  from :" + pt.id.getLoginName());
			Response res = new Response(req.getObject(),"DragPoint");
			ServerHeader sh = new ServerHeader(res);
			try
			{
				broadcast(sh);
			}
			catch(Exception e)
			{
				System.out.println("couldn't sent :"+e);
			}

		}
		else
		if(req.getRequestType().compareTo("EndPoint") == 0)
		{
			WbPoint pt = (WbPoint)req.getObject();
			//System.out.println("End Point  from :" + pt.id.getLoginName());
			Response res = new Response(req.getObject(),"EndPoint");
			ServerHeader sh = new ServerHeader(res);
			try
			{
				broadcast(sh);
			}
			catch(Exception e)
			{
				System.out.println("couldn't sent :"+e);
			}

		}

		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);
		}

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