import java.util.*;
/*************************************************************
 * VSDriver is the Driver for the Virtual Student application.
 *
 * @author Nancy Harris
 * @version V2
 *************************************************************/
public class VSDriver
{
// Display and menu Strings
   private static final String DEF_TITLE = "VirtualStudent Schedule"; 
   private static final String COMMAND = "Command";
   private static final String EVENT = "Event";
	private static final String DISPLAY = "display";
	private static final String QUIT = "quit";
	private static final String CREATE = "create";
//	Error messages
	private static final String BAD_COMMAND = "Invalid Command";
	private static final String NO_SCHED = "No Scheduled Events";
	private static final String CONFLICT = "Conflict";
	private static final String MISSING = "Missing Information";
	private static final String BAD = "Bad Information";
//	General output format
	private static final String FORMAT = "%10s: ";
   
	/******************************************************
	 * main entry point to the application
	 *
	 * @param args Command line arguments; args[0] contains
	 *             the title of the schedule if present
	 *****************************************************/
   public static void main(String [] args)
   {
      final int TRUNCATE = 5;
      final int MAX_SIZE = 100;
 
      String title;
      Schedule sched;
      Scanner kb;
      String command;
      String sEvent;
      Event event;
 
      kb = new Scanner(System.in);
      
      // get title from command line
      if (args.length > 0)
         title = args[0];
      else 
         title = DEF_TITLE;
		
		// set up the schedule
      ScheduledTime.setTruncationValue(TRUNCATE);
      sched = new Schedule(title, MAX_SIZE);
   
 		// switchboard loop  
	   do 
      {
         System.out.printf(FORMAT, COMMAND);
         command = kb.nextLine();
         if(command.equals(CREATE))
         {
            System.out.printf(FORMAT, EVENT);
            sEvent = kb.nextLine();
            event = createEvent(sEvent);
            if (event != null)
               addEvent(event, sched);
            
         }
         else if (command.equals(DISPLAY))
         {
            displayEvents(sched);
         }
         else if (!command.equals(QUIT))
         {
            System.out.println(BAD_COMMAND);
         }
      }while(!command.equals(QUIT));
   }
	/******************************************************
	 * displayEvents displays the current schedule
	 *
	 * @param sched The schedule to display
	 *****************************************************/
    public static void displayEvents(Schedule sched)
  {   
  		Event [] events;
		events = sched.getEvents();            
      System.out.println(sched.getTitle());

      if (sched.size() == 0)
			System.out.println(NO_SCHED);
      else
      	for (int ii = 0; ii < sched.size(); ii++)
			  	System.out.println(events[ii]);
      
  }
  	/******************************************************
	 * addEvent adds an event to the schedule as long as 
	 * there are no conflicts
	 *
	 * @param eve The event to add
	 * @param sched The schedule to which to add the event
	 * @return True if the event was added and false otherwise
	 *****************************************************/
  public static boolean addEvent(Event eve, Schedule sched)
  {
      boolean olap;
		Event[] events;
		
      olap = false;
  		events = sched.getEvents();
		    
      if (eve != null)
      {
         for (int ii = 0; ii < sched.size(); ii++)
         {
            if (events[ii].conflicts(eve))
               olap = true;
         }
         if (!olap)
            sched.add(eve);
         else
            System.out.println(CONFLICT);
      }
		else
			olap = true; // can't add a null event
			
      return !olap;
  }
  	/******************************************************
	 * createEvent creates an event object
	 *
	 * @param sEvent The String representation of an event
	 * @return The new event object or null if there is a parse 
	 *         problem
	 *****************************************************/
  public static Event createEvent(String sEvent)
  {
      Event result = null;
      StringTokenizer lineScan;
      String description = null;
      String days = null;
      String time = null;
      lineScan = new StringTokenizer(sEvent, ";");
      boolean okay = true;
      
      if(lineScan.countTokens() < 3)
      {
         System.out.println(MISSING);
         okay = false;
      }
      else
      {
         description = lineScan.nextToken();
         days = lineScan.nextToken();
         time = lineScan.nextToken();
			if (description.length() == 0 || days.length() == 0 || time.length() == 0)
			{
				System.out.println(MISSING);
				okay = false;
			}
      }
      try
      {
         if (okay)  
            result = new Event(description, days, time);
      }
      catch (IllegalArgumentException iae)
      {
         System.out.println(BAD);
      }
      return result;
  }
}     
