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


/** This class is the web interface for the "Free Time Servlet."
 *  The data from the web-form will be sent to this class for processing.
 *  Once proper data is received from the user, the needed variables will be
 *  initialized and passed to Scheduler.class   
 *   @author Adam Christian
 *   @param Proper input on the form.html
 */  
public class SchedulerSelect extends HttpServlet {

    public void doPost(HttpServletRequest request,
		       HttpServletResponse response) 
	               throws IOException, ServletException {
   	
  	/*Get the parameters from the Form*/
  	String inputFromForm = "";
  	String userInput = "";
  	String userNames = "";
  	String start = "";
  	String finish = "";
  	
  	/* Store the start and finish constraints */
  	start = request.getParameter("start");   
  	finish = request.getParameter("finish");
      	
  	/* Store all the user names */
  	userInput = request.getParameter("User1");
  	if(userInput != "")
  	   userNames += userInput + ", ";
  	
  	userInput = request.getParameter("User2");
  	if(userInput != "")
  	   userNames += userInput + ", ";
      
    userInput = request.getParameter("User3");   
  	if(userInput != "")
  	   userNames += userInput + ", ";
       
    userInput = request.getParameter("User4");   
  	if(userInput != "")
  	   userNames += userInput;                	
  	
  	
  	/* Store the busy times */
  	userInput = request.getParameter("User1Times");
  	if(userInput != "")
  		inputFromForm += userInput + ",";

  	userInput = request.getParameter("User2Times");
  	if(userInput != "")
  		inputFromForm += userInput + ",";
  		
  	userInput = request.getParameter("User3Times");
  	if(userInput != "")
  		inputFromForm += userInput + ",";
    
    userInput = request.getParameter("User4Times");
  	if(userInput != "")
  		inputFromForm += userInput + ",";
  
  	/* Call covert to Minutes on the inputFromForm String*/
    convertToMinutes convertMinutes = new convertToMinutes();
  	String[] convertedString = new String[5];
  	convertedString = convertMinutes.toMinutes(inputFromForm);
  
    Scheduler getTimes = new Scheduler();
    String[] finalFreeTimes = new String[5];
  	finalFreeTimes = getTimes.getFreeTimes(convertedString, start, finish);
  	
  	/* Forward the result */
  	forwardRequest(start, finish, userNames, finalFreeTimes, request, response);	
   } 

  /* Forward data */
  private void forwardRequest(String start, String finish, String userNames,
                              String[] finalFreeTimes, HttpServletRequest 
                              request, HttpServletResponse response) 
  	                          throws IOException, ServletException {
  	
  	/* Set the attributes so the data can be sent to the users */
    request.setAttribute("times", finalFreeTimes);
  	request.setAttribute("names", userNames);
  	request.setAttribute("start", start);
  	request.setAttribute("finish", finish);
     
  	RequestDispatcher view = 
  	    request.getRequestDispatcher("result.jsp");
  	
  	view.forward(request, response);
    }
}

