/**
 * SchedSelect.java
 * This is the view.  It takes the input from the Sched class and outputs
 *  it to a webpage using JSP.
 */

package com.example.web;

import com.example.model.*;
import javax.servlet.ServletContext;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.lang.*;

public class SchedSelect extends HttpServlet {
    
    /**
     *  Executes when a post form is submitted.
     *
     *  @param request
     *  @param response
     */
    public void doPost(HttpServletRequest request,
		       HttpServletResponse response)
	throws IOException, ServletException {
	
	Sched group3 = new Sched();

	RequestDispatcher view = request.getRequestDispatcher("default.html");
	
	String tag = request.getParameter("tag");
	
	//Login Page
	if(tag.equals("0"))
	    {
		String user = request.getParameter("user");
		String pass = request.getParameter("pass");
		
		Cookie username = new Cookie("username", user);
		response.addCookie(username);
		
		boolean temp;

		try
		    {
			temp = group3.login(user, pass);
		    }
		catch(Exception e)
		    {
			throw new ServletException("Unable to login: " + e.getMessage());
		    }
		
		if(temp == true)
		    {
			view = request.getRequestDispatcher("menu.html");
		    }
		else
		    {
			view = request.getRequestDispatcher("failLogin.html");
		    }
		
	    }
	
	//Add Times Page
	else if (tag.equals("1"))
	    {
		String m = request.getParameter("M");
		String t = request.getParameter("T");
		String w = request.getParameter("W");
		String r = request.getParameter("R");
		String f = request.getParameter("F");
		
		Cookie[] cookies = request.getCookies();
		String user="";
		
		for(int i=0; i<cookies.length; i++)
		    {
			if(cookies[i].getName().equals("username"))
			    {
				user = cookies[i].getValue();
			    }
		    }
		
		try
		    {
			group3.addSchedule(user, m, t, w, r, f);
		    }
		catch(Exception e)
		    {
			throw new ServletException("Unable to add schedule: " + e.getMessage());
		    }
		
		view = request.getRequestDispatcher("menu.html");
	    }
	
	//Get Free Times Page
	else if(tag.equals("2"))
	    {
		Cookie[] cookies = request.getCookies();
		String user1="";
		
		for(int i=0; i<cookies.length; i++)
		    {
			if(cookies[i].getName().equals("username"))
			    {
				user1 = cookies[i].getValue();
			    }
		    }
		
		String user2 = request.getParameter("user2");
		String user3 = request.getParameter("user3");
		String user4 = request.getParameter("user4");
		
		String[] u1 = new String[5];
		String[] u2 = new String[5];
		String[] u3 = new String[5];
		String[] u4 = new String[5];
		try
		    {
			u1 = group3.lookupSchedule(user1);
			u2 = group3.lookupSchedule(user2);
			u3 = group3.lookupSchedule(user3);
			u4 = group3.lookupSchedule(user4);
		    }
		catch(Exception e)
		    {
			throw new ServletException("Unable to lookup schedule: " + e.getMessage());
		    }
		
		String res = request.getParameter("restrictions");
		
		group3.putSched(0, 0, u1[0]);
		group3.putSched(0, 1, u1[1]);
		group3.putSched(0, 2, u1[2]);
		group3.putSched(0, 3, u1[3]);
		group3.putSched(0, 4, u1[4]);
		
		group3.putSched(1, 0, u2[0]);
		group3.putSched(1, 1, u2[1]);
		group3.putSched(1, 2, u2[2]);
		group3.putSched(1, 3, u2[3]);
		group3.putSched(1, 4, u2[4]);
		
		group3.putSched(2, 0, u3[0]);
		group3.putSched(2, 1, u3[1]);
		group3.putSched(2, 2, u3[2]);
		group3.putSched(2, 3, u3[3]);
		group3.putSched(2, 4, u3[4]);
		
		group3.putSched(3, 0, u4[0]);
		group3.putSched(3, 1, u4[1]);
		group3.putSched(3, 2, u4[2]);
		group3.putSched(3, 3, u4[3]);
		group3.putSched(3, 4, u4[4]);
		
		group3.putRestrictions(res);
		
		String[] result = new String[5];
		result = group3.getFreeTimes();
		
		List<String> final_result = new ArrayList<String>();
		
		for (int x = 0; x < 5; x++)
		    final_result.add(result[x]);
		
		request.setAttribute("available", final_result);
		
		view = request.getRequestDispatcher("result.jsp");
	    }
	
	//Lookup User Page
	else if (tag.equals("3"))
	    {
		String user = request.getParameter("user");
		
		String[] r = new String[5];
		
		try
		    {
			r = group3.lookupSchedule(user);
		    }
		catch(Exception e)
		    {
			throw new ServletException("Unable to login: " + e.getMessage());
		    }
		
		List<String> final_r = new ArrayList<String>();
		
		for (int x = 0; x < 5; x++)
		    final_r.add(r[x]);
		
		request.setAttribute("available", final_r);
		request.setAttribute("user", user);
		
		view = request.getRequestDispatcher("lookup.jsp");
	    }
	
	//Add New user Page
	else if (tag.equals("4"))
	    {
		String user = request.getParameter("user");
		String pass1 = request.getParameter("pass1");
		String pass2 = request.getParameter("pass2");
		String email = request.getParameter("email");
		boolean temp;
		
		if(pass1.equals(pass2))
		    {
			try
			    {
				temp = group3.createAccount(user, pass1, email);
			    }
			catch(Exception e)
			    {
				throw new ServletException("Unable to login: " + e.getMessage());
			    }
			if(temp == true)
			    {
				view = request.getRequestDispatcher("login.html");
			    }
			else
			    {
				request.setAttribute("flag", "1");
				
				view = request.getRequestDispatcher("addedUserFail.jsp");
			    }
		    }
		else
		    {
			request.setAttribute("flag", "2");
			
			view = request.getRequestDispatcher("addedUserFail.jsp");
		    }		
	    }
	view.forward(request, response);
    }
}  
