//Author: Charley Ruggiero

package com.example.test;

import com.example.model.*;
import junit.framework.*;
import java.lang.*;
import java.util.*;
import java.io.*;

public class ScheduleTest extends TestCase {

    public ScheduleTest(String name) {
	super(name);
    }

    public void testSchedule() {
	String freeTimes;
	String constraints = "MTWRF 8:00 AM-5:00 PM";
	String busyTimes = "M 7:00 AM-4:00 PM";
	String expected = "M 4:00 PM-5:00 PM" + '\n' +
			  "T 8:00 AM-5:00 PM" + '\n' +
			  "W 8:00 AM-5:00 PM" + '\n' +
			  "R 8:00 AM-5:00 PM" + '\n' +
			  "F 8:00 AM-5:00 PM";

        freeTimes = setupScheduleTest(constraints, busyTimes);                  
	Assert.assertTrue(expected.equals(freeTimes));
    } 

    public void testSchedule1() {
	String freeTimes;
	String constraints = "MTWRF 8:00 AM-5:00 PM";
	String busyTimes = "M 10:00 AM-4:00 PM";
	String expected = "M 8:00 AM-10:00 AM, 4:00 PM-5:00 PM" + '\n' +
			  "T 8:00 AM-5:00 PM" + '\n' +
			  "W 8:00 AM-5:00 PM" + '\n' +
			  "R 8:00 AM-5:00 PM" + '\n' +
			  "F 8:00 AM-5:00 PM";

        freeTimes = setupScheduleTest(constraints, busyTimes);                  
	Assert.assertTrue(expected.equals(freeTimes));
    } 

    public void testSchedule2() {
	Schedule sched = new Schedule();
	String freeTimes;
	String constraints = "MTWRF 8:00 AM-5:00 PM";
	String busyTimes = "M 10:00 AM-4:00 PM";
	String expected = "M 8:00 AM-5:00 PM" + '\n' +
			  "T 8:00 AM-5:00 PM" + '\n' +
			  "W 8:00 AM-5:00 PM" + '\n' +
			  "R 8:00 AM-5:00 PM" + '\n' +
			  "F 8:00 AM-5:00 PM";
	try {         
            sched.setFreeTimeConstraints(constraints);
	}
	catch (FreeTimesAlreadySetException e) {
	    freeTimes = e.getMessage();
	}

	freeTimes = sched.getFreeTimes();
	Assert.assertTrue(expected.equals(freeTimes));
	Assert.assertTrue(sched.isConsistent()); 
    } 

    public void testFreeTimesNotSetException() {
	Schedule sched = new Schedule();
	String freeTimes = "";
	String busyTimes = "TR 9:30 AM-12:00 PM";
	String expected = "ERROR: Cannot add busy times.  " +
			  "Must set free time constraints first.\n";

	try { 
            sched.setBusyTimes(busyTimes); 
        }        
        catch (FreeTimesNotSetException e) { 
            freeTimes = e.getMessage(); 
	    Assert.assertTrue(expected.equals(freeTimes));
        } 
    }

    public void testFreeTimesAlreadySetException() {
	Schedule sched = new Schedule();
	String freeTimes = "";
	String constraints1 = "MTWRF 8:00 AM-5:00 PM";
	String constraints2 = "TR 9:30 AM-12:00 PM";
	String expected = "ERROR: Free time constraints already set.  " +
			  "Add busy times.\n";

	try { 
            sched.setFreeTimeConstraints(constraints1); 
        }        
        catch (FreeTimesAlreadySetException e) { 
            freeTimes = e.getMessage(); 
	    Assert.assertTrue(expected.equals(freeTimes));
        } 

	try { 
            sched.setFreeTimeConstraints(constraints2); 
        }        
        catch (FreeTimesAlreadySetException e) { 
            freeTimes = e.getMessage(); 
	    Assert.assertTrue(expected.equals(freeTimes));
        } 
    }

    public void testSetMinFreeTimeSpan() {
	Schedule sched = new Schedule();

	String freeTimeSpan = "60";
	int expected = 60;

	sched.setMinFreeTimeSpan(freeTimeSpan);

	Assert.assertTrue(expected == sched.getMinFreeTimeSpan());
    }

    private String setupScheduleTest(String constraints, String busyTimes) {
	Schedule sched = new Schedule();
	String freeTimes = ""; 
         
	try {         
            sched.setFreeTimeConstraints(constraints);
	}
	catch (FreeTimesAlreadySetException e) {
	    freeTimes = e.getMessage();
            return freeTimes;
	}
             
        try { 
            sched.setBusyTimes(busyTimes); 
        }        
        catch (FreeTimesNotSetException e) { 
            freeTimes = e.getMessage(); 
	    return freeTimes;
        } 

	return sched.getFreeTimes();
    }

}
	
