package org.greenstone.atlas.server;

import java.util.ArrayList;

public class GazetteerHelper 
{
	/**
	 * Gets the population from the line in the gazetteer file
	 * @param columns is the line in the file that has been split
	 * @return the population (if available)
	 */
	public static int getPopulation(String[] columns)
	{
		// If the row has a sixth element it is the population of the place
		if (columns.length > 5 && columns[5].length() > 0) 
		{
			return Integer.parseInt(columns[5]);
		}
		return -1;
	}
	
	/**
	 * Gets the longitude from the line in the gazetteer file
	 * @param columns is the line in the file that has been split
	 * @return the longitude (if available)
	 */
	public static Float getLongitude(String[] columns)
	{
		try
		{
			// If the row has a seventh element it is the longitude of the place
			if (columns.length > 7 && columns[7].length() > 0) 
			{
				return (Float.parseFloat(columns[7]) / 100);
			}
			return null;
		}
		catch(Exception ex)
		{
			return null;
		}
	}
	
	/**
	 * Gets the latitude from the line in the gazetteer file
	 * @param columns is the line in the file that has been split
	 * @return the latitude (if available)
	 */
	public static Float getLatitude(String[] columns)
	{
		try
		{
			// If the row has a eighth element it is the latitude of the place
			if (columns.length > 6 && columns[6].length() > 0) 
			{
				return (Float.parseFloat(columns[6]) / 100);
			}
			return null;
		}
		catch(Exception ex)
		{
			return null;
		}
	}

	/**
	 * Gets the country name from the line in the gazetteer file
	 * @param columns is the line in the file that has been split
	 * @return the country name
	 */
	public static String getCountryName(String[] columns)
	{
		// If the row has a ninth element it is the name of the country that
		// this place is in
		if (columns.length > 8 && columns[8].length() > 0) 
		{
			return columns[8];
		}
		return null;
	}
	
	/**
	 * Gets the region name from the line in the gazetteer file
	 * @param columns is the line in the file that has been split
	 * @return the region name
	 */
	public static String getRegionName(String[] columns)
	{
		// If the row has a tenth element it is the name of the region in
		// the country that this place is in
		if (columns.length > 9 && columns[9].length() > 0) 
		{
			return columns[9];
		}
		return null;
	}
	
	/**
	 * Gets the sub region name from the line in the gazetteer file
	 * @param columns is the line in the file that has been split
	 * @return the sub region name
	 */
	public static String getSubRegionName(String[] columns)
	{
		// If the row has an eleventh element is is the name of the
		// sub-region in the country that this place is in
		if (columns.length > 10 && columns[10].length() > 0) 
		{
			return columns[10];
		}
		return null;
	}
	
	/**
	 * Gets the place type (e.g. county, locality etc.) from the line in the gazetteer file
	 * @param columns is the line in the file that has been split
	 * @return the place type
	 */
	public static String getPlaceType(String[] columns)
	{
		if (columns.length > 4 && columns[4].length() > 0) 
		{
			return columns[4];
		}
		return null;
	}

	/**
	 * Gets the place name that this place is most commonly known as
	 * @param columns is a tab seperated line in the gazetteer file that has been split
	 * @return the most common place name
	 */
	public static String getMainPlaceName(String[] columns)
	{
		if(columns.length > 1 && columns[1].length() > 0)
		{
			return columns[1];
		}
		return null;
	}
	
	/**
	 * Adds the alternative names for a place to the list of alternative place names that map back to the most common place name
	 * @param columns is a tab seperated line in the gazetteer file that has been split
	 */
	protected static ArrayList<String> getAlternativePlaceNames(String[] columns)
	{
		ArrayList<String> placeNames = new ArrayList<String>();
		
		//If the row has a 3rd element it is a list of alternate names for this place
		if(columns.length > 2 && columns[2].length() > 0)
		{
			String[] names = columns[2].split(", ");
			
			for(String name : names)
			{
				if(!placeNames.contains(name))
				{
					placeNames.add(name);
				}
			}
		}
		
		//If the row has a 4th element it is the name of this place in it's home language
		if(columns.length > 3 && columns[3].length() > 0)
		{
			if(!placeNames.contains(columns[3]))
			{
				placeNames.add(columns[3]);
			}
		}
		
		return placeNames;
	}
	
	protected static Integer getID(String[] columns)
	{
		try
		{
			if(columns.length > 0 && columns[0].length() > 0)
			{
				return Integer.parseInt(columns[0]);
			}
			return null;
		}
		catch(Exception ex)
		{
			return null;
		}
	}
}
