/*
 * Created on Apr 19, 2005
 *
 */
package com.dragonsoft.tryapp.ejb.entity.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;

import com.dragonsoft.tryapp.common.exceptions.TryException;
import com.dragonsoft.tryapp.ejb.entity.interfaces.CourseKey;
import com.dragonsoft.tryapp.support.TryFileSystem;

/**
 * 
 * CourseUtil is a class which aims to aid in the serialization
 * of information related to Courses on the Filesystem. This is a work in
 * progress and should be updated as needed, along with appropriate documentation.
 * @author Greg
 *
 * 
 */
public final class CourseFSUtils {

	/**
	 * The Deliminator Used when serializing the CourseKey
	 * Make sure that the course key does not contain such characters.
	 */
	public static final String DELIMINATOR = ":";
	/**
	 * The Name of the folder where section specific information is 
	 * held.
	 */
	public static final String SECTIONS = "sections";
	
	/**
	 * Gets the path structure for a given course with the given attributes.
	 * @param term the term identifier for the course.
	 * @param courseNumber the courseNumber identifier for the course
	 * @param section the section number for the course in question.
	 * @return the string path where the course information should be located.
	 */
	/*public static String getPath(String term,String courseNumber,String section){
		String assignmentDir = null;
		try {
			assignmentDir = TryFileSystem.getAssignmentDirectory();
		} catch (TryException e) {
			ExceptionHandler.logException(e, CourseFSUtils.class);
		}
		StringBuffer buffer = new StringBuffer(assignmentDir);
		buffer.append(File.separatorChar).append(term).append(
			File.separatorChar).append(courseNumber).append(File.separatorChar);
		if (section != null) { // if the section is blank it is a global identifier.
			buffer.append(SECTIONS).append(File.separatorChar).append(section).append(File.separatorChar);
		}
		return buffer.toString();
	}*/
	
	/**
	 * Gets the Path that points to a Course
	 * @param obj the courseKey which contains the information needed for the path structure.
	 * @return the path which points to the course information.
	 */
	/*public static String getPath(CourseKey obj){
		return getPath(obj.getTerm(),obj.getCourseNum(),obj.getSection());
	}*/
	/**
	 * Serializes a CourseKey to a file for later use.
	 * @param key the key to serialize.
	 * @param courseFile the File that is to contain the serialized data
	 * @throws IOException if there is a problem writing to the file.
	 */
	public static void serialize(CourseKey key,Writer courseFile) throws IOException{
		PrintWriter writer = null;
		
			writer = new PrintWriter(courseFile);
			writer.print(key.getTerm());
			writer.print(DELIMINATOR);
			writer.print(key.getCourseNum());
			if(key.getSection()!=null){
				writer.print(DELIMINATOR);
				writer.print(key.getSection());
			}
			writer.println();
			writer.flush();
	}
	
	/**
	 * Reads in a CourseKey from a file. If the file is not found or there are
	 * any errors reading the stream, an error is thrown.
	 * @param courseFile the file that holds the course information
	 * @return a courseKey which represents the key to the course information.
	 * @throws FileNotFoundException if a file is not found.
	 * @throws IOException if there is a wrong format or if there is a problem with the stream.
	 */
	public static CourseKey unserialize(Reader courseFile)throws FileNotFoundException,IOException{
		CourseKey key = null;
		BufferedReader reader = null;
		int counter = 0;
		String[] course = new String[4];
		StringTokenizer tokener = null;

		reader = new BufferedReader(courseFile);
		course[counter] = reader.readLine();

		tokener = new StringTokenizer(course[counter], DELIMINATOR);
		while (tokener.hasMoreTokens() && ++counter < course.length) {
			course[counter] = tokener.nextToken();
		}
		if (course[course.length] == null) {
			key = new CourseKey(course[1], course[2]);
		} else
			key = new CourseKey(course[1], course[2], course[3]);
	
		return key;
	}
	
	/**
	 * This will find all the sections associated with a global course.
	 * Remember a section has the ability to add custom assignments to 
	 * a course. Nice way to hand out extra credit.
	 * @param globalCourse  The location of the Global Course.
	 * @return a Collection of Files that are the roots of the sections.
	 */
	public static Collection getSectionsFromGlobal(File globalCourse){
		List rtVal = new ArrayList();
		File[] sections = null;
		File swap = null;
		if(globalCourse.exists() && globalCourse.isDirectory()){
			swap = new File (globalCourse.getAbsolutePath()+CourseFSUtils.SECTIONS+File.pathSeparatorChar);
			if(swap.exists() && swap.isDirectory()){
				sections = swap.listFiles();
			}
		}
		for(int i = 0;i<sections.length;i++){
			rtVal.add(sections[i]);
		}
		return rtVal;
	}
	
	public CourseKey reverseLookUp(File courseDir){
		CourseKey key =null;
		try {
			String root = TryFileSystem.getAssignmentDirectory();
			int length = root.length();
			StringTokenizer token = new StringTokenizer(courseDir.getAbsolutePath(),File.separator);
			
		} catch (TryException e) {
			
		}
		return key;
	}
	
}
