import java.util.Scanner;
import java.io.*;

/**
 * ThinkAway is a main program for reading assignment information.
 * It will take the info and output it in a more student friendly way.
 *
 * @author Fred Kral
 * @version 1.5
 *
 */
 
 /* Change Log
     Fred Kral     1.3   1/13/2015   First version with readAssignments2.
     Fred Kral     1.4   1/14/2015   Default semester info when no file.
     Fred Kral     1.5   1/19/2015   Mending raw data in readAssignments2.java
                                     Handle previous semester case (for debugging).
 */
 
 /* Help: how to generate javadoc
    javadoc *.java -d javadoc -author -version
 */

public class ThinkAway {

   /** Main main :-) */
   public static void main (String [] args) throws FileNotFoundException {
      read();
   }//main
   
   /** read can be called from main from any class. It is the beginning of the program */
   public static void read() throws FileNotFoundException {
   
      /* Hello and date printed */
      System.out.println ("Hello!");
      plainDate dummyDate = new plainDate();
      plainDate todayDate = dummyDate.getToday(false); // Print today's date
   
      /* Semester */
      Semester semester = readSemesterInfo("SemesterInfo.txt");
      semester.sysPrint();
      
   
      /* Input supplied by user */
      String inputChoice = ""; // default to file
      Scanner sc = chooseInput(inputChoice); // file
   
      /* Read stuff at top of input "page" */
      testInitial(sc);
      
      Person student = new Person ("This Student");
      
      /* Just the list of courses */
      readCourses(sc, student);
      
      /* Assignment dates and Assignments */
      //readAssignments.readAssignments(sc, student, semester);
      readAssignments2.readAssignments(sc, student, semester);
      
      /* BillBoard is the grid of dates and courses with assignments */
   
      plainDate startDate = todayDate;
      if (semester.getSemesterNumber () == 1) {
         startDate = new plainDate( 9, "Dec", semester.getFirstDate().getYear() );// default
      }
      
      BillBoard bBoard = new BillBoard(student, semester, 
         startDate // today? lastassignment? any date?
         );
      bBoard.draw();
      
   }// read()

   
   
   /** chooseInput takes the assignment and course data from user or from a file */
   public static Scanner chooseInput (String choice) throws FileNotFoundException {
   
      //System.out.println();
   
      Scanner sc;
      if (choice.equalsIgnoreCase("")) {
         File f = new File("developer/student.txt");
         //System.out.println(f.exists() + " " + f);
         if(f.exists() && !f.isDirectory()) { 
            sc = new Scanner (f);
         }
         else {
            f = new File("../../../../../../../Projects/JavaCourse/JavaInGeneral/JavaWork/ThinkAway/developer/student.txt");
            //System.out.println(f.exists() + " " + f);
            if(f.exists() && !f.isDirectory()) { 
               sc = new Scanner (f);
            }
            else {
               System.out.println();
               System.out.println("Paste text (right-click on Windows). HIT RETURN. (There was no file.)");
               sc = new Scanner (System.in);
            }
         }
      }
      else {// chose paste
         System.out.println();
         System.out.println("Paste text (right-click on Windows). HIT RETURN.");
         sc = new Scanner (System.in);
      }
   
      return sc;
   
   
   }// chooseInput
   
   /** Read a file with the semester information */
   public static Semester readSemesterInfo(String semesterInfoFile) throws FileNotFoundException {  
   
      String semesterName = "Semester";
      int semesterNumber = 2;
      plainDate firstDate = new plainDate(5, "Jan", 2015);
      plainDate lastDate = new plainDate(12, "Jun", 2015);
   
         /* "SemesterInfo.txt" */
      File f = new File(semesterInfoFile);
      //System.out.print(f.exists() + " " + f);
      if(! f.exists() || f.isDirectory()) { // default
         //System.out.println(" - Use default semester dates");
      }
      else { // read existing file
      
         //System.out.println();
      
         Scanner scSem = new Scanner (f);
      
         semesterName = scSem.next();
         semesterNumber = scSem.nextInt();
      
         String firstMonth = scSem.next();
         int firstDay = scSem.nextInt();
         int firstYear = scSem.nextInt();
         scSem.next();
         String lastMonth = scSem.next();
         int lastDay = scSem.nextInt();
         int lastYear = scSem.nextInt();
      
      
         firstDate = new plainDate(firstDay, firstMonth, firstYear);
         lastDate = new plainDate(lastDay, lastMonth, lastYear);
      
      }
      
      return new Semester(semesterName, semesterNumber, firstDate, lastDate);
   
   }
   
   /** testInitial reads the input file:  Is the beginning of the imput looking good? */
   public static void testInitial (Scanner sc) {    
      boolean badInitial = false;
      boolean tooShort = false;
      
      String [] initialText = {"", "Dashboard", "Assignments", "Attendance", "Grades", "", "Assignments", "", "All Classes"};     
   
      // loop through initial text
      for (int i = 0; i<initialText.length; i++) {
      
         if ( sc.hasNextLine() ){
            String line = sc.nextLine();
            //outPrintln(line);
         
            if ( line.equals( initialText[i] ) ) {
               badInitial = false;
            }
            else {
               badInitial = true;
               break;
            }
         }
         else {
            tooShort = true;
         }
         
      
      }
         
      if (tooShort || badInitial ) {
         System.out.println();
         if (tooShort) System.out.println("File too short");
         if (badInitial) System.out.println ("Unsupported initial lines. Should begin with " + initialText[0] + " " + initialText[1] + " " + initialText[2] + "...");
         System.exit(3);
      }
   }// END testInitial
   
   /** Read courses from a list from a webpage from a person */
   public static void readCourses(Scanner sc, Person student) {
   
         
      while (true) {
         if ( sc.hasNextLine() ) {
            String line = sc.nextLine();
            if ( line.equals("Assignments") ) // end of courses
               break;
            //int i = line.indexOf(",");
            Course course = new Course(line);
            
            // Add each course to the student's list of courses
            student.addCourse(course);
         }
         else {
            System.exit(5);//no line left
         }
      }
   
      //student.sysPrintCourses();
      
   }// END readCourses   
   
}// END class