import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.io.*;
import java.util.List;
import java.util.ArrayList;
/**
 * HwBox is used to display one box worth of data, e.g. a list of assignments
 *
 * <p>
 * Uses 
 * {@link DrawingPanel}
 *
 * @author Fred Kral
 * @version 1.4
 *
 */
 /* Change log
      Emma Neary  1.3               Long assignment name.
      Fred Kral   1.4   2/1/2015    Added inBox method.
 */
 /* Help for (future) line wrap:
    http://stackoverflow.com/questions/4413132/problems-with-newline-in-graphics2d-drawstring
    http://docs.oracle.com/javase/tutorial/2d/text/drawmulstring.html
 */
 
public class HwBox {
      // Another way to do this: give only the list of assignments for the box (not person/course/pDate)

   /** This is the person (teacher/student) that is getting a BillBoard */
   Person person;
   Course courseBox;
   plainDate pDate;
   Graphics g;
   /** These are the coordinates and dimensions of the HwBox */
   int x =  0; // from upper left
   int y =  0; //from upper left
   int dx = 0; // x extent
   int dy = 0; // y extent
   
   boolean today = false;
   
   Color boxColor = new Color(253, 253, 253);
   Color boxColorToday = new Color(240, 240, 220);

   Color defaultColor = new Color(0, 100, 250);


   public HwBox (Person person, plainDate pDate, Course courseBox, 
                  Graphics g, int x, int y, int dx, int dy) {
      this.person = person;
      this.courseBox = courseBox;
      this.pDate = pDate;
      this.g = g;
      this.x = x;
      this.y = y;
      this.dx = dx;
      this.dy = dy;
      g.setFont(BillBoard.headerFont);
      
      /* Today. Strip out the hour and minute information just to be sure. */
      plainDate tDate = new plainDate();
      tDate = tDate.getToday(false); // full today date
      plainDate todayDate = new plainDate( tDate.getDay(), tDate.getMonth(), tDate.getYear() );
      today = pDate.equals(todayDate);
   
      
   }
   
   /** inBox checks to see if given coordinates are inside this Box */
   // FK added this
   public boolean inBox (int xIn, int yIn) {
      // Find if coordinates are in this box
      // NOTE:
      // not entirely clear whether it's x + dx or x + dx - 1 and same for y 
      //   depends on how we treat lines between boxes. Technically should not use the lines
      //   perhaps the calling method should figure this out instead of this routine
      boolean PRINTINBOX = false;
      boolean isInBox = (xIn >= x && xIn <= x + dx) && (yIn >= y && yIn <= y + dy);
      if (PRINTINBOX) System.out.printf("isInBox %b xIn %3d, yIn %3d, x %3d, y %3d, dx %3d, dy %3d\n", 
                        isInBox, xIn, yIn, x, y, dx, dy);
      return isInBox;
   }
   
   
   public void clear () {
      if (today) g.setColor(boxColorToday);
      else g.setColor(boxColor);
      
      g.fillRect(x, y, dx, dy);
   }

   public void draw () {
   
      clear();
      
      g.setColor(defaultColor);
   
      
      /* Need list of assignments for this student */
      
      /* Multiple assignments per course per day ? */
      int numAssignments = 0;
      
      /* Loop over each assignment until find the one(s) needed for this HW Box */
      for ( Course course : person.getCourses() ) {
         /* Is it the same course as for this box ? */
         if ( course.equals(courseBox) ) {
         /* Active course */
            if ( course.getActive() ) {
            /* Assignments in the course */
               for ( Assignment assignment : course.getAssignments() ) {
               /* Active assignment */
                  if (assignment.getActiveAssignment()) {
                  /* Date match? */
                     if ( assignment.getPlainDate().equals(pDate) ) {
                       // Date matches
                       
                        assignment.sysPrintValue();
                      
                        /* Color depends on the status of the assignment */
                        if ( ! assignment.getResult().equals("") ) {
                           if ( assignment.getResult().equals("M") )      // missing
                              g.setColor(new Color(200, 80, 80));
                           else if ( assignment.getResult().equals("E") ) // excused
                              g.setColor(new Color(0, 70, 20));
                           else                               // graded or a mistake
                              g.setColor(new Color(50, 180, 80));
                        }
                        else {                                // ungraded or no info
                           g.setColor(new Color(50, 50, 200));
                        }
                     
                     
                        // there can be more than one assigment per course per day
                        // EN: Add full name
                        // FK: Add Value and Result
                        g.drawString(
                           numAssignments + 1 + ") " + 
                           assignment.getAssignmentName() + "  " +
                           assignment.getValue() + "  " + 
                           assignment.getResult() + "  ",
                           x + 4, y + 12 + numAssignments*16);
                        
                        numAssignments++;
                        
                     }
                  }
               }
            }
         }
      }
   }


}// END class