package org.greenstone.atlas.server;

import java.io.Serializable;

public class GazetteerTrieTopLevelNode implements Serializable
{
	private static final long serialVersionUID = 1L;
	protected boolean _nameEnd = false;
	protected GazetteerTrieNode[] _children = new GazetteerTrieNode[65000];
	
	/**
	 * Basic constructor for a non-unicode trie node
	 * @param nameEnd defines whether the new node represents the end of a place name
	 */
	protected GazetteerTrieTopLevelNode(boolean nameEnd)
	{
		for(int i = 0; i < 65000; i++)
		{
			_children[i] = null;
		}
		_nameEnd = nameEnd;
	}
	
	/**
	 * Add a child to the current node
	 * @param c is the character used to decide where to add the child node
	 * @param nameEnd is whether or not this character represents the end of a place name
	 * @return true if a child was added and false if one already existed
	 */
	protected boolean addChild(char c, boolean nameEnd)
	{
		if(c >= 65 && c <= 90)
		{
			c += 32;
		}
		//Add the ascii character if it either does not already exist or it signifies the end of a place name
		if(_children[(int)c] != null)
		{
			if(_children[(int)c].isNameEnd() || nameEnd == false)
			{
				return false;
			}
			else
			{
				_children[(int)c].setNameEnd(true);
				return true;
			}
		}
		else
		{
			_children[(int)c] = new GazetteerTrieNode(nameEnd);
			return true;
		}
	}
	
	/**
	 * @return whether or not this node represents the end of a place name
	 */
	protected boolean isNameEnd()
	{
		return _nameEnd;
	}
	
	/**
	 * Set whether or not this node represents the end of a place name
	 * @param nameEnd is the value to set it to
	 */
	protected void setNameEnd(boolean nameEnd)
	{
		_nameEnd = nameEnd;
	}
	
	/**
	 * Returns the child at the given index
	 * @param index is the index of the child to return
	 * @return the child at the given index
	 */
	protected GazetteerTrieNode getChild(char c)
	{
		if(c >= 65 && c <= 90)
		{
			c += 32;
		}
		return _children[(int)c];
	}
	
	public void output()
	{
		/*
		if(_unicodeCharacters != null && _unicodeCharacters.size() >= 10)
		{
			System.out.println(_unicodeCharacters.size());
		}
		
		for(GazetteerTrieNode g : _children)
		{
			if(g != null)
			{
				g.output();
			}
		}
		*/
	}
}