// ReadNotes.java  P. Conrad, for CISC370, 06/18/2007
//   based on p. 231 from Java Cookbook, 2nd Edition (O'Reilly)

import java.io.*;

import java.util.ArrayList;

public class ReadNotes {

    // the expected number of columns, and the 
    // columns where we can find the various
    // kinds of data
    
    static final int expectedNumberOfParts = 8;
    
    static final int colSafari = 0;
    static final int colPageNo = 2;
    static final int colChapter = 4;
    static final int colChapterTitle= 5;
    static final int colSection = 6;
    static final int colSectionTitle= 7;
    
    
    ArrayList<Chapter> chapters;

    private static Chapter last(ArrayList<Chapter> al) {
	if (al.size() == 0) 
	    return null;
	else 
	    return al.get(al.size() - 1);
    }
    

    public ReadNotes(String filename) {
	
	// set up empty arrays for the chapters and the chapter titles
	
	chapters = new ArrayList<Chapter>();
	
	try {
	    // from p. 266 in Java Cookbook, 2nd Edition by O'Reilly
	    
	    BufferedReader is = 
		new BufferedReader(new FileReader(filename));	    
	    
	    String inputLine = is.readLine();
	    while (inputLine != null) {
		
		processLine(inputLine);
		
		inputLine = is.readLine();		
	    }
	    
	    is.close();

	} catch (IOException e) {
	    System.out.println("Oopsy Daisy... IOException " + e);
	    System.exit(1);
	} // try/catch
    	
    } // Constructor

   
    public static void main(String args[]) {
	
	if (args.length!=1)
	    {
		System.err.println("Usage: java ReadNotes filename");
		System.exit(1);
	    }

	ReadNotes rn = new ReadNotes(args[0]);


	rn.print();

    } // main 


    void processLine(String inputLine) {
      
	String parts[] = inputLine.split("\t");
	
	
	if (parts.length != expectedNumberOfParts)
	    {
		System.err.println
		    ("Expected " + expectedNumberOfParts + 
		     " columns in input file, but found " 
		     + parts.length);
		System.err.println("Cannot continue.");
		System.exit(2);		
	    }
	
	String thisChapter = parts[colChapter];
	String thisChapterTitle = parts[colChapterTitle];

	String thisSafari = parts[colSafari];
	String thisPageNo = parts[colPageNo];
	String thisSection = parts[colSection];
	String thisSectionTitle = parts[colSectionTitle];


	Section ns = new Section(thisSection,
				 thisSectionTitle,
				 thisPageNo,
				 thisSafari);

	// if this is the very first line in the file,
	// or, if the last chapter on the list doesn't match
	// the chapter on this line of input, then
	// add a new chapter to the list

	Chapter chapter = last(chapters);

	if (chapter == null || 
	    !(chapter.getName().equals(thisChapter)) ) {
	    chapter = new Chapter(thisChapter,thisChapterTitle);
	    chapters.add(chapter);
	}

	// add this section to the chapter.

	chapter.add(ns);

    } // processLine


    public void print () {
	for (int i=0; i< chapters.size(); i++) {

	    Chapter c = chapters.get(i);
	    
	    // print value of c.getName()  in this format 
	    //  ==========
	    //  This_Title
	    //  ==========
	    
	    System.out.println(c.getName().replaceAll(".","="));  
	    System.out.println(c.getName());
	    System.out.println(c.getName().replaceAll(".","=")+"\n");
			       

	    for (int j=0; j<c.getNumSections(); j++) {
		Section s = c.get(j);
		
		System.out.println
		    (c.getName() + "|" +
		     c.getTitle() + "|" +
		     s.getName() + "|" +
		     s.getTitle() + "|" +
		     s.getPage() + "|" +
		     s.getSafari());
	    }
		
	}
    }

} // class
