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

public class SchServerHandler extends ServerHandler
{

	Vector  members = new Vector();
	Query query = new Query();
	public SchServerHandler()
	{
		setMembers();
	}

	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);
				}
				
				ScheduleInfo info = new ScheduleInfo(members);
				Response resInfo = new Response(info,"ScheduleInfo");
				ServerHeader toBeSentInfo = new ServerHeader(resInfo);
				try
				{
					sendResponse(toBeSentInfo,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("SchRequest") == 0)
		{
			SchRequest sch = (SchRequest)req.getObject();
			String memberName = sch.agentName;
			ResultSelect rs = query.selectQuery("select OnDate , Time, Schedule from schedule WHERE User = '"+memberName+"'  order by OnDate,time");
			Vector schedule = rs.getRowVector();
			ScheduleResponse sr = new ScheduleResponse(schedule,memberName);
			Response res = new Response(sr,"ScheduleResponse");
			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("ScheduleAdd") == 0)
		{
			ScheduleAdd sa = (ScheduleAdd)req.getObject();
			int i =query.updateQuery("INSERT INTO schedule values (null,'"+sa.user+"' ,'"+sa.date+"', '"+sa.time+"' ,'"+sa.schedule+"')");
			if(i==1)
			{
				Response res = new Response ("Schedule added by "+sa.user,"Allert");
				ServerHeader toBeSent = new ServerHeader(res);
				try
				{
					broadcast(toBeSent);
				}
				catch(Exception e)
				{
					System.out.println("cudn't send allert message");
				}
			}
		}
		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());
	}
	
	void setMembers()
	{

		int k =query.updateQuery("DELETE FROM schedule WHERE OnDate < CURRENT_DATE");
		System.out.println("No. of Schedules deleted :"+k);
		ResultSelect rs = query.selectQuery("SELECT  LoginId FROM loginInfo");
		int i =0;
		int j =rs.rowVector.size();
		members.removeAllElements();
		for (i =0;i<j;++i)
		{
			//System.out.println((String)((Vector)rs.rowVector.elementAt(i)).elementAt(0));
			members.addElement((String)((Vector)rs.rowVector.elementAt(i)).elementAt(0));
		}
	}
}
