package com.gmscheduler.unittests;

import java.util.LinkedList;
import com.gmscheduler.model.Person;
import com.gmscheduler.model.Schedule;
import com.gmscheduler.model.Timeframe;
import com.gmscheduler.model.ScheduleUtil;
import com.gmscheduler.model.DatabaseManager;

import junit.framework.TestCase;
import junit.framework.Assert;


public class ScheduleUtilTest extends TestCase {
	private Person p1;
	private Timeframe t1;
	private Schedule s1,sn;
	private Timeframe t2;
	private Schedule s2;
	private Person p3;
	private Timeframe t3;
	private Schedule s3;
	private Timeframe t4;
	private LinkedList lt4;
	private LinkedList schedlist;
	private LinkedList lt5;
	private Schedule s4;
	private String input;
	private ScheduleUtil schedut = new ScheduleUtil();
	private String name;
	private Schedule freetime;
	private Schedule result;
	private DatabaseManager dm = new DatabaseManager();
	
	protected void setUp() throws Exception {
		name = "generic";
		p1 = new Person(name);
		t1 = new Timeframe("T","11:00","12:15");
		lt4 = new LinkedList();
		schedlist = new LinkedList();
		lt5 = new LinkedList();
		lt4.add(t1);
		t2 = new Timeframe(t1);
		t2.setDay("R");
		lt4.add(t2);
		t3 = new Timeframe(t1);
		t3.setDay("W");
		t3.setStart("8:00");
		t3.setEnd("17:00");
		lt4.add(t3);
		t4 = new Timeframe(t3);
		t4.setDay("M");
		lt4.add(t4);
		s1 = new Schedule(p1,lt4);
		schedlist.add(s1);
		input = "TR 11:00am-12:15pm, W 8:00am-5:00pm,              M 8:00am-5:00pm,";
		//t1 = new Timeframe("M","8:00","17:00");
		//lt5.add(t1);
		t1 = new Timeframe("T","8:00","11:00");
		lt5.add(t1);
		t1 = new Timeframe("T","12:15","17:00");
		lt5.add(t1);
		//t1 = new Timeframe("W","8:00","17:00");
		//lt5.add(t1);
		t1 = new Timeframe("R","8:00","11:00");
		lt5.add(t1);
		t1 = new Timeframe("R","12:15","17:00");
		lt5.add(t1);
		t1 = new Timeframe("F","8:00","17:00");
		lt5.add(t1);
		
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	/*
	 * Test method for 'com.gmscheduler.model.ScheduleUtil.parseSchedule(String)'
	 */
	public void testParseSchedule() {
		s4 = new Schedule(schedut.parseSchedule(input));
		s4.setPerson(p1);
		Assert.assertEquals(s1.toString(),s4.toString());
	}

	/*
	 * Test method for 'com.gmscheduler.model.ScheduleUtil.findTime(Timeframe, List)'
	 */
	public void testFindTime() {
		s3 = new Schedule(schedut.parseSchedule("MTWRF 8:00am-5:00pm"));
		s3.setPerson(new Person("FreeTime"));
		s2 = new Schedule(new Person("FreeTime"),lt5);
		result = schedut.findTime(s3,schedlist);
		System.out.println(result.toString());
		Assert.assertEquals(s2.toString(),result.toString());
	}

	public void testFindNullFreeTime() {
		Assert.assertEquals(new Schedule().toString(), schedut.findTime(new Schedule(), schedlist).toString());
	}

	public void testRemoveTimeframe() {
		p1 = dm.storePerson(p1);
		s1.setPerson(p1);
		s1 = dm.storeSchedule(s1);
		sn = new Schedule(s1);
		
		s1 = schedut.removeTimeframe(s1,t3);
		sn = dm.loadScheduleById(s1.getId());
		
		Assert.assertEquals(s1.toString(),sn.toString());
	}
	
}
