/**
 * Week. Turns dates into numbered weeks.  Works with plainDate.
 *
 * <p>
 * Uses 
 * {@link plainDate}
 *
 * @author Fred Kral
 * @version 1.2
 *
 *
 */
 
 
public class Week {


   int weekNum=0;
   int activeWeekNum = 0;
   plainDate pMonday;
   boolean activeWeek = true; // Breaks should be made inactive
   plainDate [] days;


   public Week (int weekNum, plainDate pMonday) {
      this.weekNum = weekNum; // 1 to N
      activeWeekNum = weekNum; // for now, numbers are the same, until subtract break weeks
      this.pMonday = pMonday;
                
      days = new plainDate [7];
      
      for (int i = 0; i < 7; i++) {
         days[i] = this.pMonday.addDays(i);
      }
      
   }
   
   /** See if a given date is in this particular week */
   public boolean inWeek(plainDate pDate){
      boolean test = false;
      for (int i = 0; i < 7; i++) {
         if (days[i].equals(pDate)) {
            test = true;
            break;
         }
      }
      return test;
   }
      /** find the weekday of a give date, if it is in this week */
   public int whichDay(plainDate pDate){ // return 1, 2, 3,  (or -1 if not found)
      int test = -1;
      for (int i = 0; i < 7; i++) {
         if (days[i].equals(pDate)) {
            test = i + 1;
            break;
         }
      }
      return test;
   }
   
   public plainDate getDateFromWeekday(int weekdayNumber) {
      return days[weekdayNumber - 1];
   }
   
   public int getWeekNum () {
      return weekNum;
   }   
   
   public int getActiveWeekNum () {
      return activeWeekNum;
   }
   
   public boolean getActiveWeek () {
      return activeWeek;
   }
   
   
   public Week getNextWeek () {
      
      Week nextWeek = null;
      for (int i = 1; i <= 52; i++) {
         Week trialWeek = new Week( weekNum + i, pMonday.addDays(i*7) );
         if (trialWeek.getActiveWeek()) {
            nextWeek = trialWeek;
            break;
         }
      }
      return nextWeek;
   }
   
   public Week getPreviousWeek () {
      
      Week prevWeek = null;
      for (int i = 1; i <= 52; i++) {
         Week trialWeek = new Week( weekNum - i, pMonday.addDays(-i*7) );
         if (trialWeek.getActiveWeek()) {
            prevWeek = trialWeek;
            break;
         }
      }
      return prevWeek;
   }
   
   public plainDate [] getDays() {
      return days;
   }

   public void sysPrint(){
      System.out.printf("Week %2d Mon-Sun", weekNum);
      for (int i = 0; i < 7; i++) 
         System.out.printf(" %s ", days[i].sysPrint(false));
      System.out.println();
   }


}// END class