/**
  * Class: Parser
  * @author Jason Grafinger
  * @dateCreated 2006-03-19
  * @purpose Takes an array of strings and returns a Schedule object
  */
package com.example.model;
import java.util.*;
import java.io.*;


public class Parser
{
    //////////////////////////////////////////////
    // Private Data                             //
    //////////////////////////////////////////////

    //NONE

    //////////////////////////////////////////////
    // Member Functions                         //
    //////////////////////////////////////////////
    /**
     * Purpose: Parses each entry field from the user interface.  Each
     * user is separated by a ";".
     * @return returnSchedule Returns a Schedule object.
     * @param arrayOfStrings Takes inputs from the user interface.
     */

    public static Schedule createSchedule(String[] arrayOfStrings)
    {

	//week[i] = a day of the week
	//week[0] = monday
	//week[4] = friday, etc.
	ArrayList week[] = new ArrayList[5];
	week[0] = new ArrayList<String>(100);
	week[1] = new ArrayList<String>(100);
	week[2] = new ArrayList<String>(100);
	week[3] = new ArrayList<String>(100);
	week[4] = new ArrayList<String>(100);

	int i = 0;
	for(String dayString: arrayOfStrings)
	{
	    String[] splitString = dayString.split(";");
	    for(String individualSched: splitString)
	    {
		week[i].add(individualSched);
	    }
	    i++;
	}
	    
	//Now, go through each ArrayList and sift through the
	//different students

	ArrayList studentNumbers = new ArrayList<String>(100);
	for(int day = 0; day < 5; day++)
	{
	    for(Iterator<String> dayIt = week[day].iterator(); dayIt.hasNext(); )
	    {
		String[] tempSplit = dayIt.next().split(":");
		if(!studentNumbers.contains(tempSplit[0]))
		{
		    studentNumbers.add(tempSplit[0]);
		}
	    }
	}


	//Now, every number used to represent a student from
	//the inputs is in the ArrayList studentNumbers.

	Schedule returnSchedule = new Schedule(studentNumbers.size());
	int student = 0;
	for(Iterator<String> numbersIt = studentNumbers.iterator(); numbersIt.hasNext(); )
	{
	    String currStudentNum = numbersIt.next();

	    for(int day2 = 0; day2 < 5; day2++)
	    {
		for(Iterator<String> dayIt = week[day2].iterator(); dayIt.hasNext(); )
		{
		    String[] tempSplit = dayIt.next().split(":");

		    if(currStudentNum.equalsIgnoreCase(tempSplit[0]))
		    {
			//Meaning, this input entry is about
			//the current student (add to schedule)

			String[] toAndFrom = tempSplit[1].split("-");
			Integer from = new Integer(toAndFrom[0]);
			Integer to = new Integer(toAndFrom[1]);

			if(from.intValue() < 8)
			{
			    from = new Integer(from.intValue() + 12);
			}

			if(to.intValue() < 8)
			{
			    to = new Integer(to.intValue() + 12);
			}


			if(to.intValue() < from.intValue())
			{
			    System.out.println("Parse Error: Incorrect Range of Free Times");

			    return new Schedule(0);
			}

			for(int scheduleCount = from-8; scheduleCount <= to-8; scheduleCount++)
			{
			    returnSchedule.setSchedule(student, day2, scheduleCount);
			}
		    }

		}//Go to the next entry in week[day2]

	    }//Go to the next day

	    //Go to the next student
	    student++;
	}

	return returnSchedule;
    }
}





