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

public class CornerService implements Service
{

	public CornerService()
	{
	}
	
	/* implemented method from Service interface */

	public void execute(HttpServletRequest request, HttpServletResponse response, ServletContext context)
		throws Exception 
	{

		HttpSession session = request.getSession();
		TravelCorner myTravelCorner = (TravelCorner)session.getAttribute("myTravelCorner");
		if ( myTravelCorner == null ) // check if created or not
		{
			// if no travel plan corner was found create one and add
			// to the HttpSession
			myTravelCorner = new TravelCorner();
			session.setAttribute("myTravelCorner", myTravelCorner);
		}
	
		Object checkbox = request.getParameterValues("checkbox");
		if (checkbox != null)
		{
			boolean successful;			// successfully add in the corner or not
			boolean add;				// if the current process is add or remove
			String mode;				// mode of process
			String[] records = (String[])request.getParameterValues("checkbox");
		
			String flightID;
			String departDate;
			String departCity;
			String arrivalCity;
			String departTime;
			String arrivalTime;
			int durationTime;
			int price;
		
			int i;

			String[] success;
			String[] unsuccess;
			int successIndex;
			int unsuccessIndex;

			success = new String[records.length];
			unsuccess = new String[records.length];
			successIndex = 0;
			unsuccessIndex = 0;		

			// find an existing travel plan corner
			

			mode = (String)request.getParameter("mode");
			if(mode.equals("add"))
			{
				add = true;
			}
			else
			{
				add = false;
			}
			if(add)
			{
				System.out.println("111");
				for(i = 0; i < records.length; i++)
				{
					// add the record into the travel plan corner
					System.out.println(records[i]);			
					flightID = records[i].substring(0, 5);
					departDate = records[i].substring(5, 13);
					departCity = records[i].substring(13, 23);
					arrivalCity = records[i].substring(23, 33);
					departTime = records[i].substring(33, 37);
					arrivalTime = records[i].substring(37, 41);
					durationTime = Integer.parseInt(records[i].substring(41, 45).trim());
					price = Integer.parseInt(records[i].substring(45, 50).trim());
					
					successful = myTravelCorner.addItem(flightID, departDate, departCity,
						arrivalCity, departTime, arrivalTime, durationTime, price);

					if(successful)
					{
						success[successIndex] = new String(records[i]);
		
						successIndex++;
					}
					else
					{
						unsuccess[unsuccessIndex] = new String(records[i]);
						unsuccessIndex++;
					}
				}
				System.out.println("999");
				request.setAttribute("success", success);
				request.setAttribute("unsuccess", unsuccess);
				request.setAttribute("successNum", Integer.toString(successIndex));
				request.setAttribute("unsuccessNum", Integer.toString(unsuccessIndex));
				request.setAttribute("mode", "add");
				System.out.println(Integer.toString(successIndex));
				System.out.println(Integer.toString(unsuccessIndex));
			}
			else	//remove
			{
	                	for(i = 0; i < records.length; i++)
        	        	{
                	        	// remove the record into the travel plan corner

		                        flightID = records[i].substring(0, 5);
        		                departDate = records[i].substring(5, 13);
                
                		        myTravelCorner.removeItem(flightID, departDate);
                 
                		        success[successIndex] = new String(records[i]); 
                       	 		successIndex++;
                		}
           		request.setAttribute("success", success);
				request.setAttribute("successNum", Integer.toString(successIndex));
				request.setAttribute("mode", "remove");
			
			}
		}
		else
		{
			String mode;				// mode of process
	                String[] success;
        	        String[] unsuccess;
               	 	int successIndex;
                	int unsuccessIndex;
                
                	success = new String[1];
                	unsuccess = new String[1];
                	successIndex = 0; 
                	unsuccessIndex = 0;
			
			mode = (String)request.getParameter("mode");

                        request.setAttribute("success", success);
       	                request.setAttribute("unsuccess", unsuccess);
               	        request.setAttribute("successNum", Integer.toString(successIndex));
                       	request.setAttribute("unsuccessNum", Integer.toString(unsuccessIndex));
                        request.setAttribute("mode", mode);   
		}
		
		int tpcNum = myTravelCorner.getNumOfItems();
		String tpc[][] = new String[tpcNum][8];
		Enumeration enum = myTravelCorner.getEnumeration();
		int i = 0;
		while(enum.hasMoreElements())
		{
			tpc[i] = (String[])enum.nextElement();
			i++;
		}
		request.setAttribute("tpc", tpc);
		request.setAttribute("tpcNum", Integer.toString(tpcNum));	

		int tPrice = myTravelCorner.getPrice();
		int tTime = myTravelCorner.getTime();

//		int tPrice = 777;
//		int tTime = 999;
		request.setAttribute("tPrice", Integer.toString(tPrice));
		request.setAttribute("tTime", Integer.toString(tTime));
	}
}
