package scheduleGenerator;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Class that contains a Config object.
 *
 * @author maurerja.
 *         Created Oct 28, 2012.
 */
public class Config {
	Map<String,HashMap<String,ArrayList<String>>> dates;
	ArrayList<String> allJobs;

	/**
	 * Object that holds dates and jobs.
	 *
	 */
	public Config(){
		this.dates= new HashMap<String,HashMap<String,ArrayList<String>>>();
		this.allJobs = new ArrayList<String>();
	}
	
	/**
	 * Reads through config file to find which dates and jobs exist.
	 *
	 * @param filename
	 */
	public void  parseConfig(String filename) {
		ArrayList<Config> config = new ArrayList<Config>();
		Scanner sc = null;
		try {
			sc = new Scanner(new File(filename));
		} catch (FileNotFoundException exception) {
			exception.printStackTrace();
		}
		String line = "";
		String date = "";
		String t="";
		HashMap<String,ArrayList<String>> time= new HashMap<String,ArrayList<String>>();
		ArrayList<String> jobs=new ArrayList<String>();
		while (sc.hasNextLine()) {
			line = sc.nextLine();
			if (line.startsWith("+")) {
				if (!t.equals("")&&jobs.size()>0){
					time.put(t,jobs);
					this.dates.put(date,time);
				}
				else t="";
				date = line.substring(1,line.length());
				time = new HashMap<String,ArrayList<String>>();
				jobs=new ArrayList<String>();
			}
			else if(line.startsWith("*")){
				if (jobs.size()>0){
					time.put(t,jobs);
				}
				t=line.substring(1,line.length());
				jobs=new ArrayList<String>();
			}
			else if(line.startsWith(":"))jobs.add(line.substring(1,line.length()));
			else if(line.startsWith("%")){
				String l = line.substring(1,line.length());
				while(l.contains(",")){
					this.allJobs.add(l.substring(0,l.indexOf(",")));
					l=l.substring(l.indexOf(",")+1);
				}
				this.allJobs.add(l);
			}
		}
		time.put(t,jobs);
		this.dates.put(date,time);
	}
}
