package com.example.web;

import com.example.util.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import com.example.model.*;
/**
 * 
 * @author Brian Hermann
 *
 */
public class Scheduler extends HttpServlet
{
	private Driver driver = null;
    private String driverName = null;
	private String jdbcURL = null;
	private String user = null;
	private String pass = null;
	/**
	 * runs on servlet intialization
	 * intializes database driver to be used in DatabaseUtil methods
	 */
	public void init()
	{
		ServletContext context = getServletContext();
        driverName = context.getInitParameter("gb.driver");
        jdbcURL = context.getInitParameter("gb.jdbcURL");
        user = context.getInitParameter("gb.user");
        pass = context.getInitParameter("gb.pw");
		try
		{
			driver = (Driver)Class.forName(driverName).newInstance();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * @param request
	 * @param response
	 * overloaded method doPost determines which button was clicked
	 * based on the vlaue of a hidden html flag, the correct action is then taken
	 * depending on the value
	 */
	public void doPost(HttpServletRequest request, 
                        HttpServletResponse response)
                        throws IOException, ServletException{
		
		Properties connection = new Properties();
		connection.put("jdbcUrl", jdbcURL);
		connection.put("user", user);
		connection.put("pass", pass);
		
		if(request.getParameter("formflag") != null &&
				request.getParameter("formflag").equals("1"))
		{
			// login
			System.out.println("FIRST");
			String user = request.getParameter("login");
			String pass = request.getParameter("pw");
			if(DatabaseUtil.authenticateUser(connection, user, pass))
			{
				request.setAttribute("user", user);	
				RequestDispatcher view = request.getRequestDispatcher("form.jsp");
			    view.forward(request, response);
			}
			else
			{
				request.setAttribute("error", false);
				RequestDispatcher view = request.getRequestDispatcher("login2.html");
			    view.forward(request, response);
			}
		}
		else if(request.getParameter("formflag") != null &&
				request.getParameter("formflag").equals("2"))
		{
			//new user
			RequestDispatcher view = request.getRequestDispatcher("new_user.html");
			view.forward(request, response);
		}		
		else if(request.getParameter("formflag") != null &&
				request.getParameter("formflag").equals("3"))
		{
			//signup
			String user = request.getParameter("login");
			String pw = request.getParameter("pw");
			String cpw = request.getParameter("cpw");
			String message = null;
			if(pw.equals(cpw))
			{
				if(DatabaseUtil.addUser(connection, user, pw))
				{
					RequestDispatcher view = request.getRequestDispatcher("login.html");
					view.forward(request, response);
				}
				else
				{
					message = new String("Login name already exists.");
					request.setAttribute("message", message);
					RequestDispatcher view = request.getRequestDispatcher("new_user.jsp");
					view.forward(request, response);
				}
			}
			else
			{
				message = new String("Password does not match confirmation");
				request.setAttribute("message", message);
				RequestDispatcher view = request.getRequestDispatcher("new_user.jsp");
				view.forward(request, response);
			}			
		}
		else if(request.getParameter("formflag") != null &&
				request.getParameter("formflag").equals("4"))
		{
			//logout
			RequestDispatcher view = request.getRequestDispatcher("login.html");
			view.forward(request, response);
		}
		else if(request.getParameter("formflag") != null &&
				request.getParameter("formflag").equals("5"))
		{
			//add event to user's schedule
			String user = request.getParameter("user");
			String daysOfWeek[] = request.getParameterValues("daysofweek");
			int sAM = Integer.valueOf(request.getParameter("SAM"));
			int eAM = Integer.valueOf(request.getParameter("EAM"));
			int day = 0;
			int shour = Integer.valueOf(request.getParameter("Shours"));
			int smin = Integer.valueOf(request.getParameter("Sminutes"));
			int ehour = Integer.valueOf(request.getParameter("Ehours"));
			int emin = Integer.valueOf(request.getParameter("Eminutes"));
			shour *= 100;
			ehour *= 100;
			if(sAM == 1)
			{
				if(shour == 12)
					shour = 0;
			}
			if(sAM == 2)
			{
				if(shour < 12)
					shour += 1200;
			}
			if(eAM == 1)
			{
				if(ehour == 12)
					ehour = 0;
			}
			if(eAM == 2)
			{
				if(ehour < 12)
					ehour += 1200;
			}
			for(int i = 0; i < daysOfWeek.length; i++)
			{
				day = Integer.valueOf((daysOfWeek[i]));
				DatabaseUtil.addEvent(connection, user, day, (shour+smin), (ehour+emin));
		    }
			ArrayList events = DatabaseUtil.getSchedule(connection, user);
			request.setAttribute("user", user);
			request.setAttribute("events", events);
			RequestDispatcher view = request.getRequestDispatcher("form2.jsp");
			view.forward(request, response);
		}
		else if(request.getParameter("formflag") != null &&
				request.getParameter("formflag").equals("6"))
		{
			// clear your schedule
			String user = request.getParameter("user");
			request.setAttribute("user", user);
			DatabaseUtil.ClearSchedule(connection, user);
			RequestDispatcher view = request.getRequestDispatcher("form.jsp");
		    view.forward(request, response);
		}
		else if(request.getParameter("formflag") != null &&
				request.getParameter("formflag").equals("7"))
		{
			// view other user's schedules page
			String user = request.getParameter("user");
			request.setAttribute("user", user);
			ArrayList users = DatabaseUtil.getUsers(connection);
			request.setAttribute("users", users);
			RequestDispatcher view = request.getRequestDispatcher("lookupSchedule2.jsp");
		    view.forward(request, response);		
		}
		else if(request.getParameter("formflag") != null &&
				request.getParameter("formflag").equals("8"))
		{
			// get a user's schedule
			String user = request.getParameter("user");
			request.setAttribute("user", user);
			String username = request.getParameter("username");
			request.setAttribute("username", username);
			ArrayList users = DatabaseUtil.getUsers(connection);
			request.setAttribute("users", users);
			ArrayList events = DatabaseUtil.getSchedule(connection, username);
			request.setAttribute("events", events);
			RequestDispatcher view = request.getRequestDispatcher("lookupSchedule.jsp");
		    view.forward(request, response);			
		}
		else if(request.getParameter("formflag") != null &&
				request.getParameter("formflag").equals("9"))
		{
			// return to main page
			String user = request.getParameter("user");
			request.setAttribute("user", user);
			RequestDispatcher view = request.getRequestDispatcher("form.jsp");
		    view.forward(request, response);
		}
		else if(request.getParameter("formflag") != null &&
				request.getParameter("formflag").equals("10"))
		{
			// go to find free time page
			String user = request.getParameter("user");
			request.setAttribute("user", user);
			ArrayList users = DatabaseUtil.getUsers(connection);
			request.setAttribute("users", users);
			RequestDispatcher view = request.getRequestDispatcher("find_free.jsp");
		    view.forward(request, response);	
		}
		else if(request.getParameter("formflag") != null &&
				request.getParameter("formflag").equals("11"))
		{
			// find free times
			String user = request.getParameter("user");
			request.setAttribute("user", user);
			ArrayList users = DatabaseUtil.getUsers(connection);
			request.setAttribute("users", users);
			String selectedUsers[] = request.getParameterValues("username");
			ArrayList usersSelected = new ArrayList();
			for(int i = 0; i < selectedUsers.length; i++)
			{
				usersSelected.add(selectedUsers[i]);
			}
			request.setAttribute("selected", usersSelected);
			ArrayList allSchedules = new ArrayList();
			allSchedules = DatabaseUtil.getMultipleSchedules(connection, selectedUsers);
			Schedule freeTimes = new Schedule();
			for(int i = 0; i < allSchedules.size(); i++)
			{
				freeTimes.addEvent((Event)allSchedules.get(i));
			}
			freeTimes.formatFreeTimes();
			String answer[] = freeTimes.getFormatedTime();
			ArrayList finalFreeTimes = new ArrayList(7);
			for(int i = 0; i < 7; i++)
			{
				System.out.println(answer[i]);
				finalFreeTimes.add(i, answer[i]);
			}
			request.setAttribute("freetimes", finalFreeTimes);
			RequestDispatcher view = request.getRequestDispatcher("find_free2.jsp");
		    view.forward(request, response);	
		}
	}
}
