package com.example.model;
import java.util.*;


/**
 * @author Eric Andersen
 * @author Steven R. Brennan
 * @author Jason Grafinger
 * @version 2.1
 */

/**
 * Purpose:  This class takes student objects and collects the free times
 * and places them into this free schedule.
 */
public class Schedule {
 
    ///////
    // Data
    ///////
 
    private List <Student> arrayStudent;
    private int[][] freeSchedule;
    private int numberOfStudents;

    ///////
    // Methods
    ///////

    /**
     * Purpose: Constructor class sets up the variables for the Schedule class.
     * @param numOfStudents number of students inputted from user interface.
     */   
    public Schedule(int numOfStudents){
        System.out.println("Initialzing Schedule");
        System.out.println("Setting arrayOfStuents: " + numOfStudents );
        arrayStudent = new ArrayList<Student>();
        
        for( int x = 0; x < numOfStudents; x++ )
            arrayStudent.add( x, new Student() );
        
        freeSchedule = new int[5][9];
	numberOfStudents = numOfStudents;
//Set all hours of the day to 0
        for(int day = 0; day < 5; day++){
          for(int hour = 0; hour < 9; hour++){
            freeSchedule[day][hour] = 0;
           }
	}
   }
    /**
     * Purpose: setFreeSchedule takes no parameters, and runs a check on the first user.  If the first
     * has a busy time, we skip to the next hour.  If the first user is free, we will check the
     * next user to see if they are free.  If every student is free at that hour, than
     * select that time slot to be free at that time.
     *
     */   
   public void setFreeSchedule()
   {
	  for(int day = 0; day < 5; day++){
             for(int hour = 0; hour < 9; hour++){
                 if( (arrayStudent.get(0).getFreeHour(day, hour)) == 1 )
                   {
                    //increase student to check
                   int check = 1;
                   int i = 1;
                   while(check == 1 && i < numberOfStudents)
                      {
                       if( (arrayStudent.get(i).getFreeHour(day, hour)) == 1 )
                        {
                        i++;
                        
                        freeSchedule[day][hour] = 1;
                        }
                        else
                          {
                          check = 0;
                          freeSchedule[day][hour] = 0;
                          }
                        }
                     }
                   else
                     {
                     freeSchedule[day][hour]=0;
                     }
                  }
               }
	  }
    /**
     * Purpose: getFreeSchedule is a simple function that gets the availability at that
     * hour and day.
     * @param hour Hour of the day.
     * @param day Day of the week.
     * @return freeSchedule availability at the given input.
     */   
   public int getFreeSchedule(int hour, int day ){
       if(day >= 5 || hour >= 9)
	   {
	       return -1;
	   }

       return freeSchedule[day][hour];
   }
    /**
     * Purpose: Takes no input, and prints out the final output in XHTML form.
     * @version2.1 Updated to now make a valid XHTML 1.1 webpage
     */   
   public String printFreeSchedule(){
      
      String returnString = new String();
      returnString = "<html>\n  <head>\n    <title>TEST A08 OUTPUT</title>\n  </head>\n  <body>\n    <table>";
      returnString = returnString.concat("\n      <tr>\n        <th></th><th>Monday</th>\n        <th>Tuesday</th>" + 
					 "\n        <th>Wednesday</th>\n        <th>Thursday</th>\n        <th>Friday</th>\n      <tr>");
      for(int hour = 0; hour<9; hour++)
      {
	  int hourStr = (hour + 8) % 12;
	  returnString = returnString.concat("\n      <tr>\n        <td height=\"5\">" + hourStr + "</td>");
         for(int day = 0; day<5; day++)
         {
	     returnString = returnString.concat("\n        <td height=\"5\"bgcolor=\"");
            if(freeSchedule[day][hour] == 1)
            {
		returnString = returnString.concat("green\">\n");
            }
	    else
		{
		    returnString = returnString.concat("red\">\n");
		}
         }
	 returnString = returnString.concat("\n      </tr>");
      }
      returnString = returnString.concat("\n    </table>\n  </body>\n</html>");
      return returnString;
    }

     public int setSchedule(int studentNumber, int day, int hour)
    //calls set hour as free on student number
       {
            return arrayStudent.get(studentNumber).setHourAsFree(day, hour);
       }
}





