import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.io.*;
/**
 * BillBoard is the grid of dates and courses with assignments
 *
 * <p>
 * Uses 
 * {@link DrawingPanel}
 *
 * @author Fred Kral
 * @version 1.0
 *
 *
 */
 
 
public class BillBoard {

   /** Width of the board with graphics */
   public static final int BOARD_WIDTH = 750;
   /* Length of the board with graphics */
   public static final int BOARD_LENGTH = 550; 
        
   Person person;
   Semester semester;
   /* Requested date */
   plainDate pDate;
   Week weekBegin;
   
   DrawingPanel panel;
   Graphics g;
   Color bkgColor = Color.WHITE;
   Color gridColor = Color.BLACK;
   public static final Font headerFont = new Font("Arial", Font.PLAIN, 12);


   public BillBoard (Person person, Semester semester, plainDate pDate) {
      this.person = person;
      this.semester = semester; 
      this.pDate = pDate;  
      
      /* get the assigned week */
      weekBegin = semester.getWeek(pDate);
      
      panel = new DrawingPanel(BOARD_WIDTH, BOARD_LENGTH, "ThinkAway BillBoard");
      //panel = new DrawingPanel(BOARD_WIDTH, BOARD_LENGTH);
      g = panel.getGraphics();  // paintbrushes
   
      panel.setBackground(bkgColor);
   }
   
   
   public void draw(){
   
   
      //System.out.println ("drawing goes here");
      
      boolean continueRun = true;
      
      Week week = weekBegin;
      
      while (continueRun) {
      
         /* Paint over the board. There is a more offical way to do it, I'm sure */
         g.setColor(bkgColor);
         g.fillRect(0, 0, BOARD_WIDTH, BOARD_LENGTH);
         
         g.setFont(headerFont);
         
         g.setColor(gridColor);
      
      
         //750x550
      
         // 50 for "left header" + 5 Weekdays * 140 = 750 pixels wide
      
         int LEFTHEADER = 50;
         int TOPHEADER = 20;
         int FOOTER = 40;
         int PADDING = 4;
         int boxPixX = (BOARD_WIDTH-LEFTHEADER)/5;// 5 days/week
         int boxPixY = (BOARD_LENGTH - TOPHEADER - FOOTER)/person.MAXNUMCOURSEARRAY;// 7 max courses
         // Verticals
         for (int i = LEFTHEADER; i < BOARD_WIDTH -1; i+=boxPixX) {
            g.drawLine(i, 0, i, BOARD_LENGTH  - FOOTER -1);
         }
      
         // 7 courses plus top header and bottom footer (with extra info)
         // 20+ 7*70 + 40 = 550 px
      
         // Horizontals
         for (int j = 20; j < BOARD_LENGTH -1; j+=boxPixY) {
            g.drawLine(0, j, BOARD_WIDTH-1, j);
         }
      
      
      
         /* Top left box of header */
         g.setColor(new Color(0, 200, 100));
         g.drawString("Wk " + week.getActiveWeekNum(), PADDING, TOPHEADER-PADDING);
         g.setColor(gridColor);
         
      
                  
         /* Print days of the week in the header */
         for (int i = 0; i < 5; i++) {
            plainDate aDate = week.getDateFromWeekday(i+1);
            g.drawString(aDate.getWeekdayName() + "    " + aDate.sysPrint(false), 
               LEFTHEADER + i*boxPixX + boxPixX/6 + PADDING,
               TOPHEADER - PADDING);
         }
      
         /* Number of active courses less than or equal to MAX */  
         
         // Active courses      
         int nCourses = person.getNumActiveCoursesInArray();
         
         /* Print course names in the left header */
         for (int j = 0; j < nCourses; j++) {
            //System.out.println(j);
            Course course = person.getCourse(j + 1);
            //System.out.println(course.getNickName());
            g.drawString(course.getNickName(),
               PADDING, 
               TOPHEADER + j*boxPixY + boxPixY/2 + PADDING);
         }
         
         
         /* Make boxes */
         for (int i = 0; i < 5; i++) {
            int x = LEFTHEADER + i*boxPixX;
            plainDate xDate = week.getDateFromWeekday(i+1);
            for (int j = 0; j < nCourses; j++) {
               int y = TOPHEADER + j*boxPixY;
               Course yCourse = person.getCourse(j + 1);
               
               HwBox hwBox = new HwBox(person, xDate, yCourse, g, x+1, y+1, boxPixX-1, boxPixY-1);
               hwBox.draw();
            }
         }
      
         /* Click to continue */
         System.out.println();     
         System.out.println("Click top to change weeks, lower right to quit"); 
         while (! panel.mousePressed() ) { } // wait for mouse press             
         while (panel.mousePressed() ) { } // wait for mouse release
         // condition for continuing the while loop (x and y positions of mouse)  
         int xM = panel.getMouseX();
         int yM = panel.getMouseY();       
         
         if (xM > BOARD_WIDTH / 2 && yM < TOPHEADER) {
            week = week.getNextWeek();
         }
         else if (xM <= BOARD_WIDTH / 2 && yM < TOPHEADER) {
            week = week.getPreviousWeek(); 
         }
         
         
         continueRun = xM < 9*BOARD_WIDTH/10 || yM < 9*BOARD_LENGTH/10;
      
      } // end while continue
      
    
      System.out.println("quitting");
      panel.close();
      System.exit(0);
      
   }


}// END class