package com.example.web;
import com.example.model.*;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

/**
 * Purpose: This is the controller servlet for the A08 webapp.  It
 * handles the request from our form and responds with the
 * output from our Schedule class, which will be html code.
 *
 * @author Jason Grafinger
 * @version 1.0
 */
public class ScheduleServlet extends HttpServlet
{
    /**
     * Purpose: doPost() handles the post request from the form.
     * It creates a new Parser object and passes the five
     * form text boxes as the array of Strings needed to
     * successfully create a filled Schedule
     *
     * @param req This is the request to the Servlet
     * @param resp This is the response from the Servlet
     * @author Jason Grafinger
     */

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
    {
     
	resp.setContentType("text/html");
	PrintWriter out = resp.getWriter();

	//Initiate data needed to parse
	Parser servletParser = new Parser();
	String[] freeTimes = new String[5];
	freeTimes[0] = req.getParameter("monday");
        freeTimes[1] = req.getParameter("tuesday");
        freeTimes[2] = req.getParameter("wednesday");
        freeTimes[3] = req.getParameter("thursday");
        freeTimes[4] = req.getParameter("friday");

	//Build the filled schedule
	Schedule servletSchedule = servletParser.createSchedule(freeTimes);
	servletSchedule.setFreeSchedule();

	//Create formatted output from printFreeSchedule() function
	String scheduleOutput = servletSchedule.printFreeSchedule();

	//Put formatted output to the response
	out.print(scheduleOutput);
    }
}









