package scheduleGenerator;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;

/**
 * TODO Put here a description of what this class does.
 * 
 * @author saslavns. Created Oct 23, 2012.
 */
public class ScheduleParser {

	private String filename;

	/**
	 * Holds name of file that it eventually uses to read in information to create person objects.
	 * 
	 * @param filename
	 */
	public ScheduleParser(String filename) {
		this.filename = filename;
	}

	/**
	 * This parses through the schedule and creates Person objects based on the read in information.
	 * 
	 * @return Returns an ArrayList of people.
	 */
	public ArrayList<Person> parseSchedule() {
		ArrayList<Person> people = new ArrayList<Person>();
		Scanner sc = null;
		try {
			sc = new Scanner(new File(this.filename));
		} catch (FileNotFoundException exception) {
			exception.printStackTrace();
		}
		Person p = null;
		boolean callNextLine = true;
		boolean lastLineWasAMPM = false;
		String line = "";
		String date = "";
		while (sc.hasNextLine()) {
			line = sc.nextLine();
			if (line.startsWith(":")) {
				p = new Person(line.substring(1, line.length()));
			} else if ((line.startsWith("+") || line.startsWith("!"))
					&& lastLineWasAMPM) {
				p.addAvailability(date);
				date = "";
				p.clearTimes();
				lastLineWasAMPM = false;
			}
			if (line.startsWith("+")) {
				date = line.substring(1, line.length());
			} else if (line.startsWith("~")) {
				p.addTimeOfDay(line.substring(1,line.length()));
				lastLineWasAMPM = true;
			} else if (line.startsWith("!")) {
				p.addJob(line.substring(1, line.length()));
				callNextLine = true;
			}
			
			if (line.startsWith("%")) {
				@SuppressWarnings("unused")
				int one = 1;
				people.add(p);
			}
		}
		@SuppressWarnings("unused")
		int one = 1;
		return people;
	}
}