import java.util.*;
import java.net.*;
import java.io.*;

/** An instance contains a department name, the link to its CoS page of courses,
    and the liberal studies courses in that department. */
public class DeptLink {
    private String dept;   // department name
    private String link;   // relative link to its CoS page of courses
    
    private Vector<Course> courses= new Vector<Course>(); // Vector of all lib study courses in dept.
    
    /** Constructor: an instance for department d and link lk*/
    public DeptLink(String d, String lk) {
        dept= d;
        link= lk;
    }
    
    /** Constructor: an instance with null dept and link */
    public DeptLink() {
        
    }
    
    /** Constructor: an instance who dept name and link are contained in s.
      s has the form <a href="xxx">dept name</a>
      where xxx is a relative URL in directory LibStudies.prefix 
      Note: if s is not proper, an instance with null dept and link is created.
      */
    public DeptLink(String s) {
        // Remove ... <a href="  from s*/
        int k= s.indexOf("<a href=\"");
        if (k < 0) return;
        s= s.substring(k + "<a href=\"".length());
        
        // Set k to the index of the end "> of the a tag.
        k= s.indexOf("\">");
        if (k < 0) return;
        
        // Store the link in lk
        String lk= s.substring(0,k);
        
        s= s.substring(k+2);
        
        // s starts with the name of the department. Find the following </a>
        k= s.indexOf("</a>");
        if (k < 0) return;
        
        dept= s.substring(0,k);
        link= lk;
    }
    
    /** = the department name */
    public String getDept() {
        return dept;
    }
    
    /** = the vector of courses in the department */
    public Vector<Course> getCourses() {
        return courses;
    }
    
    /** = string representation of this DeptLink */
    public String toString() {
        return dept + ":  " + link;
    }
    
    /** field link is a relative URL of a web page in the directory given by LibStudies.prefix.
      The web page contains a list of courses in the department, some followed by a
      liberal studies designation. Read the web page and store in courses all courses
      that have a liberal studies designation --once for each designation.
      */
    public void fixDept() throws IOException {
        Webpage page= new Webpage(LibStudies.prefix + link);
        //System.out.println("starting on dept page: " + page);
        BufferedReader br= page.getReader();
        
        /** skip lines up to and including the one with <table . */
        String s= br.readLine();
        // System.out.println("First line is: " + s);
        while (s != null && !s.startsWith("<table ")) {
            // System.out.println("Skipping line " + s);
            s= br.readLine();
        }
        if (s != null) {
            //Remove the <table > commmand at the beginning
            int p= s.indexOf(">");
            s= s.substring(p+1);
        } else {
            System.out.println("File did not have <table . Quitting");
            return;
        }
        
        // s contains the first line with the course and title on it.
        // read all such lines (until </table>); for each line with a category on it.
        // store it in Vector courses */
        while (s != null && !s.startsWith("</table>")) {
            // Add this line to courses for each category on it
            for (int k= 0; k != LibStudies.categories.length; k= k+1) {
                if (s.contains(LibStudies.categories[k])) {
                    courses.add(new Course(s, LibStudies.categories[k]));
                    
                }
            }
            
            s= br.readLine();
        }
        
        /*for (int k= 0; k != courses.size(); k= k+1) {
         Course c= courses.get(k);
         System.out.println(c.getCategoryWO() + ": " + c);
         }
         
         */
        
        if (s == null) {
            System.out.println("end of file reached prematurely (while looking for </table>)");
        } else {
            // System.out.println("Courses successfully extracted for " + dept);
        }  
        
        
    }
    
    /** = courses does not contain a course of category cat, which is one of 
      CA, HA, KCM, LA, SBA. */
    public boolean noCourseIsCat(String cat) {
        for (int k= 0; k != courses.size(); k= k+1) {
            if (courses.get(k).getCategoryWO().equals(cat)) {
                return false;
            }
        }
        return true;
    }
    
    /** = entry k in the list has same course name as the previous one */
    public boolean isSameAsPrevious(int k) {
        if (k == 0) {
            return false;
        }
        String thisCourse= courses.get(k).getCourse();
        String prevCourse= courses.get(k-1).getCourse();
        return (thisCourse == null && prevCourse == null) ||
            (thisCourse != null && thisCourse.equals(prevCourse));
    }
    
    
    /** Add to out all courses in category cat (or all courses if cat is null).
      if there are courses to add, add 6 points before the first one.
      Return the number of courses added. */
    public int addToOut(PrintStream out, String cat) {
        if (courses.size() == 0) {
            return 0;
        }
        
        if (cat != null && noCourseIsCat(cat)) {
            return 0;
        }
        
        int number= 0;  // number of items added so far
        
        for (int k= 0; k != courses.size(); k= k+1) { 
            Course c= courses.get(k);
            String res= "";
            if ((cat == null  &&  !isSameAsPrevious(k)) ||  
                (cat != null && cat.equals(c.getCategoryWO()) && c.getCourse() != null)) {
                
                if (number == 0) {
                    res= "<p  style=\"margin-top:12px; margin-bottom:0px\" >";
                } else {
                    res= "<p  style=\"margin-top:4px; margin-bottom:0px\" >";
                }
                res= res + c.getCourse() + "&nbsp;" + c.getTitle() + "</p>";
                out.println(res);
                number= number + 1;
                
            }
        }
        
        return number;
    }
}
