package com.k_int.discover.util;

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

public class ParseSpatialData {

	public HashMap<String,String[]> parseSpatialData(String[] spatialData, String type) {
		// Work out the type of data we're dealing with and then just call the relevant method
		if ( "OSGridRef".equals(type) ) {
			return this.parseOsGridRef(spatialData);
		} else if ( "LatLong".equals(type) ) {
			return this.parseLatLong(spatialData);
		} else if ( "Getty".equals(type) ) {
			return this.parseGettyData(spatialData);
		} else if ( "General".equals(type) ) {
			return this.parseGeneralPlaceData(spatialData);
		} else if ( "Postcode".equals(type) ) {
			return this.parsePostcodeData(spatialData);
		}
		
		return null;
	}
	
	protected HashMap<String,String[]> parseGettyData(String[] gettyData) {
		
		List<String> placeNames = new ArrayList<String>();

		for(String thisData: gettyData) {
			// Check to see whether the string is delimited by / or , and then split accordingly
			String[] splitString = new String[0];
		
			if ( thisData.contains(",") ) {
				splitString = thisData.split(",");
			} else if ( thisData.contains("/") ) {
				splitString = thisData.split("/");
			}
		
			for(String section: splitString) {
				if (!"world".equalsIgnoreCase(section) && !"europe".equalsIgnoreCase(section) && !"united kingdom".equalsIgnoreCase(section) ) {
					// Something we're interested in indexing
					placeNames.add(section);
				}
			}
			// TODO - do more processing for proper spatial search
		}
		
		
		
		// Convert the different information we have into the hashmap to be returned
		HashMap<String,String[]> returnValue = new HashMap<String,String[]>();
		returnValue.put("unparsed.place", placeNames.toArray(new String[placeNames.size()]));
		// TODO when we have more parsed information add it to the hashmap
		
		if ( returnValue.size() > 0 ) {
			return returnValue;
		} else {
			return null;
		}
	}
	
	
	protected HashMap<String,String[]> parseLatLong(String[] latLongData) {
		HashMap<String,String[]> returnValue = new HashMap<String,String[]>();
		
		for(String thisData: latLongData) {
			String[] gridRefs = {thisData};
		
			returnValue.put("unparsed.latlong", gridRefs);

			// TODO - actually parse and get spatial info..
		}
		
		return returnValue;
	}
	
	protected HashMap<String,String[]> parseOsGridRef(String[] osGridRefData) {
		HashMap<String,String[]> returnValue = new HashMap<String,String[]>();
		
		for(String thisData: osGridRefData) {
			String[] gridRefs = {thisData};
		
			returnValue.put("unparsed.gridref", gridRefs);

			// 	TODO - actually parse and get spatial info..
		}
		
		return returnValue;
	}
	
	protected HashMap<String,String[]> parseGeneralPlaceData(String[] generalPlaceData) {
		HashMap<String,String[]> returnValue = new HashMap<String,String[]>();
		
		for(String thisData: generalPlaceData) {
			String[] places = null;
			if ( thisData.contains(",") ) {
				places = thisData.split(",");
			} else if ( thisData.contains("/") ) {
				places = thisData.split("/");
			} else {
				places = new String[1];
				places[0] = thisData;
			}
			returnValue.put("unparsed.place", places);

			// 	TODO - do a spatial lookup on each of these places
		}
		
		return returnValue;
	}

	protected HashMap<String, String[]> parsePostcodeData(String[] postcodeData) {
		HashMap<String,String[]> returnValue = new HashMap<String,String[]>();
		
		for(String thisData: postcodeData ) {
			String[] postcode = {thisData};

			// TODO - do some spatial look up to get lat, long, etc.
			returnValue.put("unparsed.postcode", postcode);
		}
		
		return returnValue;
	}
	
	
}
