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.7
 *
 */
 
 /* 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).
     Fred Kral       1.6   1/20/2015   Reading from a text area.
     Fred Kral       1.7   1/21/2015   Windows 7 vs Mac OS X issues for pasting.
     
 */
 
 /* Help: how to generate javadoc
    javadoc *.java -d javadoc -author -version
 */

public class ThinkAway {

   /** Choices for input source:  see the method itself. */
   public static final String INPUTCHOICE = "";
   /** Do we default to the window pop-up dialog box (or the terminal/command prompt) 
       for pasting input? */
   public static final boolean DEFAULT_IS_POPUP = true;


   /** 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, OS, and date */
   
      System.out.println ("Hello!");
      String osName = System.getProperty("os.name");
      System.out.print(osName);
      if (osName.substring(0,7).equals("Windows")) System.out.println(" -- in Windows mode --");
      else System.out.println();
      plainDate dummyDate = new plainDate();
      plainDate todayDate = dummyDate.getToday(true); // 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, DEFAULT_IS_POPUP, osName);
   
      /* Read stuff at top of input "page" */
      testInitial(sc);
      
      //System.exit(19);
      
      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
   
   @param choice ""=file/window/terminal "popup"=dialog box "<anything>"=terminal. 
    File/popup/terminal out means looking for student.txt first here and then elsewhere,
    then using either popup (if default is popup) or terminal.
   */
   public static Scanner chooseInput (String choice, boolean defaultPopUp, String osName) throws FileNotFoundException {
         
      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  if (defaultPopUp && 
                      ! osName.substring(0,7).equals("Windows") ) {
               System.out.println();
               System.out.println("Enter text in the window.");
               ReadTextArea rta = new ReadTextArea("ThinkAway Assignment Entry");      
               rta.showTextArea("Paste all text from browser. Click Submit");
               String userInputString = "";
               while (userInputString.equals("")) {
                  userInputString = rta.getUserInput();
               }
               rta.mainFrame.dispose();
               
               /* Deal with newlines */
               String text = userInputString.replaceAll("\\r\\n|\\r|\\n", "\n");
               //String text = userInputString;
               System.out.println("Strings Submitted. Newlines equal? " + 
                              text.equals(userInputString));
               sc = new Scanner (userInputString);
               
            }
            else {
            
               System.out.println("****************************************************");
               System.out.println("* Paste text (right-click on Windows). HIT RETURN. *");
               System.out.println("****************************************************");
            
               sc = new Scanner (System.in);
            
            }
         }
      }
      else if (choice.equalsIgnoreCase("popup")) {// pop up window for pasting
         System.out.println();
         System.out.println("Enter text in the window.");
         ReadTextArea rta = new ReadTextArea("ThinkAway Assignment Entry");      
         rta.showTextArea("Paste all text from browser. Click Submit");
         String userInputString = "";
         int i = 0;
         while (userInputString.equals("")) {
            i++;
            if (i%1000000000 == 0) 
               System.out.print(i);
            userInputString = rta.getUserInput();
         }
         /* never get here after a double clickable jar on Mac */
         //frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
         rta.mainFrame.dispose();
         System.out.println("\nend main");
         //String test = userInputString.replaceAll("\\r\\n|\\r|\\n", "<new>");
         
         String test = userInputString.replaceAll("\\r", "<REPLACE>");
      
         System.out.println("Length user " + userInputString.length());
      
         int len = test.length();
         System.out.println("Length test " + len);
         len = Math.min(test.length(), 600);
         System.out.println(test.substring(0,len));
         System.out.println("Test String created, equal? " + 
                  test.equals(userInputString));
         
      
         /* Deal with newlines */
         String text = userInputString.replaceAll("\\r\\n|\\r|\\n", "\n");
         //String text = userInputString;
         System.out.println("Strings Submitted. Newlines equal? " + 
                              text.equals(userInputString));
         sc = new Scanner (userInputString);
      }
      else {// chose paste
         System.out.println("****************************************************");
         System.out.println("* Paste text (right-click on Windows). HIT RETURN. *");
         System.out.println("****************************************************");
      
         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();
         
            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