/**
 * Semester.
 *  - Dates and week numbers
 *
 * @author Fred Kral
 * @version 1.1
 *
 */
public class Semester {
   String sName = "";
   int sNumber = 0;
   plainDate firstDate;
   plainDate lastDate;
   plainDate week1Monday;
   plainDate weekNSunday;
   int numDays = 0; // first to last inclusive
   int numWeeks = 0; // including partial weeks (counting breaks as weeks)
   Week [] weeks;  // Array is 0 to N-1 but the week numbers are 1 to N.
   
   /** Semester constructor builds the academic calendar dates */
   public Semester(String sName, int sNumber, plainDate firstDate, plainDate lastDate) {
      this.sName = sName;
      this.sNumber = sNumber;
      this.firstDate = firstDate;
      this.lastDate = lastDate;  
      numDays = firstDate.differenceInDaysInclusive( lastDate );
      
      // first day in week 1. last day in week N (like week 16)
      int n = firstDate.getDayOfWeekFromMonday(); 
      week1Monday = firstDate.addDays(1-n);
      int m = lastDate.getDayOfWeekFromMonday(); 
      weekNSunday = lastDate.addDays(7-m);  
      
      
      // number of weeks from Monday to Sunday
      numWeeks = (int) Math.ceil( 
                 (double) week1Monday.differenceInDaysInclusive( weekNSunday )/7.0);
                 
      /* Array of Weeks */           
      weeks = new Week [numWeeks];
       
      /* Create all the weeks in the Semester */
      for (int i = 0; i < numWeeks; i++) {
         weeks[i] = new Week(i+1, week1Monday.addDays(i*7) );
      }
      
      /* print test */
      //for (int i = 0; i < numWeeks; i++) weeks[i].sysPrint();
      //System.out.println(weeks[1].inWeek(new plainDate(10, "Sep", 2014)));
      //getWeek(new plainDate(10, "Sep", 2014)).sysPrint();
         
         
   }

   public String getSemesterName (){
      return sName;
   }
   
   public int getSemesterNumber (){
      return sNumber;
   }
   
   
   public plainDate getFirstDate () {
      return firstDate;
   }
   public plainDate getLastDate () {
      return lastDate;
   }
   
   
   public Week [] getWeeks () {
      return weeks;
   }
   
   public int whichWeek(plainDate pDate) { // returns 1, 2, 3, ... (or -1)
      int week = -1;
      for (int i = 0; i < numWeeks; i++) {
         if (weeks[i].inWeek(pDate)) {
            week = i + 1;
            break;
         }
      }
      return week;
   }

   public Week getWeek(plainDate pDate) { 
      Week week = null;
      for (int i = 0; i < numWeeks; i++) {
         if (weeks[i].inWeek(pDate)) {
            week = weeks[i];
            break;
         }
      }
      return week;
   }


   public void sysPrint(){
      boolean pShort = true;
      if (pShort) {
      
         System.out.printf( "%s %d %s %s %d to %s %s %d  %d days, %d weeks\n", 
            sName, sNumber, 
            firstDate.getWeekdayName(), 
            firstDate.sysPrint(false),
            firstDate.getYear(),
            lastDate.getWeekdayName(), 
            lastDate.sysPrint(false),
            lastDate.getYear(),
            numDays, numWeeks
            );
      
      
      }
      else{
         System.out.printf( "%s %d %s %s %d to %s %s %d  %d days, %d weeks Mon-Sun: %s to %s\n", 
            sName, sNumber, 
            firstDate.getWeekdayName(), 
            firstDate.sysPrint(false),
            firstDate.getYear(),
            lastDate.getWeekdayName(), 
            lastDate.sysPrint(false),
            lastDate.getYear(),
            numDays, numWeeks,
            week1Monday.sysPrint(false),
            weekNSunday.sysPrint(false)
            );
      }
   
   
   }

}// END class