/**
 * WebTree.java
 *
 * @author      Michael Privat
 * @version 1.0 - March 1998
 */

public class WebTree {
	final int MAX_CHILDREN	= 300;

	Info inf;
	WebTree[] child;

	public WebTree(Info info) {
		child = new WebTree[MAX_CHILDREN];
		for(int i=0; i<MAX_CHILDREN; i++)
			child[i] = null;
		inf = info;
	}

	/**
	 * Adds a new element in the tree
	 * the addition is recursive, you can add in once rep1/rep2/file
	 * even if rep1 and/or rep2 has never been created
	 * @return	false if object already present in the tree, true else
	 */
	public boolean add(Info info) throws RuntimeException {
		String val = info.getName();
		int type = info.getType();
		String title = info.getTitle();
		String date = info.getDate();
		int ct_len = info.getLen();
		int idx=0;

		// NAVIGATES FIRST
		int t = val.indexOf("/", 1);	// 1 to avoid first /
		String one=new String(val);
		String rest="";
		if(val.equals("")) return true;

		if(t!=-1) {
			one = val.substring(0, t);
			rest = val.substring(t+1);

			for(int i=0; i<MAX_CHILDREN; i++) {
				if(child[i]!=null)
					if(child[i].inf.getName().equals(one)) {
						if(t==-1) return false;	// Already added
						else {
							return child[i].add(new Info(rest, type, title, date, ct_len));
						}
					}
			}
			// Means the dir was not found, add it
			add(new Info(one, Info.DIR, "Directory", "", 0));
			add(new Info(val, type, title, date, ct_len));

		}
		else {
			idx = 0;
			// CHECK IF EXISTS
			while(idx<MAX_CHILDREN) {
				if(child[idx]!=null)
					// Already exist ?
					if(one.equals(child[idx].inf.getName())) return false;
				idx++;
			}

			idx = 0;
			// ADD
			while(idx<MAX_CHILDREN) {
				if(child[idx]==null) break;
				idx++;
			}

			if(idx==MAX_CHILDREN) throw new RuntimeException();
			else child[idx] = new WebTree(new Info(one, type, title, date, ct_len));
		}
		return true;
	}

	/**
	 * @returns the n th child in exploration of path WebTree
	 * If this is the first call, it returns the first child
	 * null if there is no n th child or if path doesn't exist
	 */
	public Info getChild(String path, int n) {
		String tmp = path;
		while(tmp.startsWith("/")) tmp = tmp.substring(1);

		if(path.equals("")) {
			if(child[n]!=null) return child[n].inf;
			return null;
		}

		String dir = "";
		String file = "";

		int t = tmp.indexOf('/');
		if(t!=-1) {
			dir = tmp.substring(0, t);
			file = tmp.substring(t+1);
		}
		else {
			dir = path;
			file = "";
		}

		int i=0;
		while(child[i]!=null) {
			String cur = child[i].inf.getName();
			while(cur.startsWith("/")) cur = cur.substring(1);
			if(cur.equals(dir))
				return child[i].getChild(file, n);
			i++;
		}
		return null;
	}

	// WARNING : This method has never been used, neither tested
	public boolean exists(String val) {
		if(inf.getName().equals(val)) return true;

		int t = val.indexOf("/");
		String one;

		String rest="";
		if(t!=-1) {
			one = val.substring(0, t);
			rest = val.substring(t+1);
		}
		else one = val;

		for(int i=0; i<MAX_CHILDREN; i++)
			if(child[i].inf.getName().equals(one))
				if(t==-1) return true;
				else return child[i].exists(rest);
		return false;
	}

	public void display() {
		System.out.println("------- WebTree display -------");
		print(0);
		System.out.println("-------------------------------");
	}

	private void print(int id) {
		for(int i=0; i<id-1; i++)
			System.out.print(" | ");

		System.out.println(" + " + inf.getName() + "\t\ttype : " + inf.getType() + "\t" + inf.getTitle());

		for(int i=0; i<MAX_CHILDREN; i++) {
			if(child[i]!=null) child[i].print(id+1);
		}
	}
}

