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
 * @author Emma Neary
 * @version 1.2, not yet merged with 1.1
 *
 *
 */
 /* Change log
      Emma Neary  1.1   1/XX/2015   Added arrows and click logic.
                                    *** Needs to be merged ***
      Fred Kral   1.2   2/1/2015    Message box.
                                    *** Needs proper dimensions ***
                                    Footer made larger.
 */
 
 
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;
      
      int xM = 0; // x Mouse
      int yM = 0; // y Mouse
      
      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
         // Originally 20 Top + 490 (7*70) boxes + 40 footer = 550.
      
         int LEFTHEADER = 50;
         int TOPHEADER = 20;
         // FK: Make a larger footer for the message box
         int FOOTEREXTRA = 12;
         //int FOOTER = 40;
         int PADDING = 4;
         int boxPixX = (BOARD_WIDTH-LEFTHEADER)/5;// 5 days/week
         int footerTempY = (BOARD_LENGTH - TOPHEADER)/(person.MAXNUMCOURSEARRAY+1);// 7 courses + footer
         //int boxPixY = (BOARD_LENGTH - TOPHEADER - FOOTER)/person.MAXNUMCOURSEARRAY;// 7 max courses
         int FOOTER = footerTempY + FOOTEREXTRA; // extra space
         int boxPixY = (BOARD_LENGTH - TOPHEADER - FOOTER)/(person.MAXNUMCOURSEARRAY);// 7 courses + footer
         // Verticals
         // FK needed fixing with new message box in the footer
         // but I don't know how it works (they were too long)
         for (int i = LEFTHEADER; i < BOARD_WIDTH -1; i+=boxPixX) {
            g.drawLine(i, 0, i, BOARD_LENGTH  - FOOTER -1 -4); // Why -5 ?????
         }
      
         // 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 - FOOTER -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);
         }
         
         boolean messageBoxInUse = false;// see if message box is in use; otherwise give help
         
         /* 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();
               
               /* Look for click in a day's HW box to set the Message box. */
               // FK: first version of message box
               // Size of message box needs to be fixed.
               if (hwBox.inBox(xM + 1, yM + 1)) { // fix these and perhaps inBox() method
                  messageBoxInUse = true;
                  HwBox messageBox = new HwBox(person, xDate, yCourse, g, 
                     LEFTHEADER + 1, BOARD_LENGTH - FOOTER + 1, // check these
                     4, 50);// Fix these
                  messageBox.draw();
               }
            }
         }
         
         if (! messageBoxInUse) {
            /* Message box instructions */
            g.setColor(new Color(150, 0, 200));
            g.drawString("Click on an assignment box to see more information",
               LEFTHEADER + 1 + 5, BOARD_LENGTH - FOOTER + 1 + 12);
            //g.drawOval(LEFTHEADER + 1 + 15, BOARD_LENGTH - FOOTER + 1 + 30, 15, 15);
         }
      
         /* 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)  
         xM = panel.getMouseX();
         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