package info.thezero.eclipse.pdt.restapi.uri;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UriNode {
	private boolean leaf;
	private String name;
	private Map<String, UriNode> children = new HashMap<String, UriNode>();
	private int skip = 0;
	private int childrenCount = 0;

	public UriNode(String name) {
		this.name = name;
	}

	public void setIsLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public boolean isLeaf() {
		return this.leaf;
	}

	public String getName() {
		return this.name;
	}

	public UriNode addChild(String name) {
		UriNode child = new UriNode(name);
		this.children.put(name, child);
		return child;
	}

	public boolean hasChild(String name) {
		return this.children.containsKey(name);
	}

	public UriNode getChild(String name) {
		return this.children.get(name);
	}

	public Collection<UriNode> getChildren() {
		return this.children.values();
	}

	public void normalize() {
		if (this.children.size() == 1 && !this.leaf) {
			UriNode child    = (UriNode) this.children.values().toArray()[0];
			this.name     = String.format("%s/%s", this.name, child.name);
			this.children = child.children;
			this.leaf     = child.leaf;
			this.skip++;
			this.normalize();
		} else {
			for (UriNode child : this.children.values()) {
				child.normalize();
				this.childrenCount += child.getChildrenCount();
			}
			
			if (this.isLeaf()) {
				this.childrenCount++;
			}
		}
	}

	public int getSkip() {
		return this.skip;
	}
	
	public int getChildrenCount() {
		return this.childrenCount;
	}

	public Collection<String> collapse() {
		return this.collapse("");
	}
	
	private Collection<String> collapse(String prefix) {
		List<String> childrenNames = new ArrayList<String>();
		
		// leaf is presented without trailing slash
		prefix = prefix.concat(this.name);
		if (this.isLeaf()) {
			childrenNames.add(prefix);
		}
		
		// all children need a slash
		prefix = prefix.concat("/");
		for (UriNode child : this.children.values()) {
			childrenNames.addAll(child.collapse(prefix));
		}
		return childrenNames;
	}
}

@@ -993,307 +993,8 @@
 ) %7B%0A
-%09%09if (this.children.size() == 1 && !this.leaf) %7B%0A%09%09%09UriNode child    = (UriNode) this.children.values().toArray()%5B0%5D;%0A%09%09%09this.name     = String.format(%22%25s/%25s%22, this.name, child.name);%0A%09%09%09this.children = child.children;%0A%09%09%09this.leaf     = child.leaf;%0A%09%09%09this.skip++;%0A%09%09%09this.normalize();%0A%09%09%7D else %7B%0A%09
 %09%09fo
@@ -1041,17 +1041,16 @@
 )) %7B%0A%09%09%09
-%09
 child.no
@@ -1060,17 +1060,16 @@
 lize();%0A
-%09
 %09%09%09this.
@@ -1117,16 +1117,11 @@
 ;%0A%09%09
-%09
 %7D%0A
-%09%09%09%0A%09
+%0A
 %09%09if
@@ -1142,17 +1142,16 @@
 )) %7B%0A%09%09%09
-%09
 this.chi
@@ -1168,13 +1168,8 @@
 ++;%0A
-%09%09%09%7D%0A
 %09%09%7D%0A
@@ -1219,18 +1219,17 @@
 kip;%0A%09%7D%0A
-%09
 %0A
+
 %09public 
@@ -1357,18 +1357,17 @@
 %22%22);%0A%09%7D%0A
-%09
 %0A
+
 %09private
@@ -1468,19 +1468,17 @@
 ing%3E();%0A
-%09%09
 %0A
+
 %09%09// lea
@@ -1609,18 +1609,16 @@
 x);%0A%09%09%7D%0A
-%09%09
 %0A%09%09// al

remove normalization to be able to suggest from the middle of normalized path