package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database.Database;

/**
 * Servlet implementation class AddMeeting
 */
@WebServlet("/AddMeeting")
public class AddMeeting extends HttpServlet {
	private static final long serialVersionUID = 1L;
         
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		System.out.println("in servlet");
		String meetingName = request.getParameter("meetingName");
		String startDateString = request.getParameter("startDate");
		
		//TODO pass in start time as a single integer please
		String startTime = request.getParameter("startTime");
		startTime = "9";
		
		//TODO comment this to eliminate hard coding
		int numDays = 6; //request.getParameter("numDays");
		int numHoursPerDay = 5; //request.getParameter("numHoursPerDay");
		
		//TODO cannot get hostId from newMeetingPage.jsp
		String hostIdString = (String)request.getAttribute("hostId");
		if(hostIdString == null) {
			hostIdString = "1";
			System.out.println("hostIdString is null");
		}
		int noOfParticipants = Integer.valueOf(request.getParameter("noOfParticipants"));
		
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
		Date startDate = new Date();
		try {
			startDate = (Date)formatter.parse(startDateString);
			System.out.println(startDate.getDate() + " " + startDate.getMonth() + " " + startDate.getYear());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
        Database db_meeting = new Database("MeetingInfo", "localhost", 3306);
        try {
			db_meeting.getConnection();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}        
        boolean addedNewMeeting = db_meeting.addMeeting(meetingName, noOfParticipants, numDays, numHoursPerDay, Integer.valueOf(hostIdString), startDate, (int)Integer.valueOf(startTime));
		System.out.println(addedNewMeeting);
		if(addedNewMeeting) {
//			request.setAttribute("noOfDays", numDays);
//			request.setAttribute("startDay", startDate.getDate());
//			request.setAttribute("startMonth", 1+startDate.getMonth());
//			request.setAttribute("startYear", 1900+startDate.getYear());
//			
//			request.setAttribute("startHour", 8);
//			request.setAttribute("startTimeOfDay", "am");
//			request.setAttribute("meetingId", 1);
			
			//TODO get meeting id from database
			int meetingId = 1;
			
			System.out.println("I am directing back");
			PrintWriter out = response.getWriter();
			out.println(String.valueOf(meetingId));
			out.flush();
//            RequestDispatcher rs = request.getRequestDispatcher("selectTimes.jsp");
//            rs.forward(request, response);
		}
		
		//TODO No alert was printed at newMeetingPage.jsp
		else {
			System.out.println("failed");
			PrintWriter out = response.getWriter();
			out.println("invalid");
			out.flush();
		}
	}

}

@@ -2261,31 +2261,21 @@
     
-boolean addedNewM
+int m
 eeting
+Id
  = d
@@ -2452,31 +2452,25 @@
 tln(
-addedNewM
+m
 eeting
+Id
 );%0A%09%09if(
 adde
@@ -2469,23 +2469,23 @@
 %09if(
-addedNewMeeting
+meetingId != -1
 ) %7B%0A
@@ -2874,69 +2874,8 @@
 %09%09%09%0A
-%09%09%09//TODO get meeting id from database%0A%09%09%09int meetingId = 1;%0A
 %09%09%09%0A

debug add new meeting