package routines;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;




/*
 * user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
 * it must be before the "{talendTypes}" key.
 * 
 * 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
 * long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
 * Short
 * 
 * 3. {Category} define a category for the Function. it is required. its value is user-defined .
 * 
 * 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
 * 
 * <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
 * Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
 * added. you can have many parameters for the Function.
 * 
 * 5. {example} gives a example for the Function. it is optional.
 */
public class LcagBusinessCode {
	private static LcagLogger log = LcagLogger.getLogger(LcagDateTime.class);
	private static Map<String, Pattern>  LOCATION_PATTERN = null;

	public LcagBusinessCode() {} { }
	
	private static Map<String, Pattern> getLocationPatterns() {
		if (LOCATION_PATTERN != null) return LOCATION_PATTERN;
		
		LOCATION_PATTERN = new HashMap<String, Pattern>(); 
		List<String>locPatternList = null;
		int flags = Pattern.CASE_INSENSITIVE;
		
		LOCATION_PATTERN.put("Airport", Pattern.compile(".*airport.*|.*apt$", flags));
		LOCATION_PATTERN.put("Bus Station", Pattern.compile(".* bus .*|.*central.b*", flags));
		LOCATION_PATTERN.put("Harbour", Pattern.compile(".* harbour.*", flags));
		LOCATION_PATTERN.put("Heliport", Pattern.compile(".*heliport.*", flags));
		LOCATION_PATTERN.put("Off-Line Point", Pattern.compile(".*off.line p.*|.*off$|.*off.l.{0,2}$", flags));
		LOCATION_PATTERN.put("Rail Station", Pattern.compile(".* rail .*|.*central.r.*", flags));
		//LOCATION_PATTERN.put("Miscellaneous", null);

		return LOCATION_PATTERN;
	}
    	
    /**
     * deriveLocationType: Parse a Station Name and figure out if it is an Airport, a Bus Station, etc..
     * 
     * @param	Original Station Name
     * @return  null if no location Type can be derived, otherwise a IATA Location Type
     *  
     * {talendTypes} String
     * 
     * {Category} LCAG
     * 
     * {param} String("aName") aName: Original Station Name 
     * 
     * {examples} 
     */    
    public static String deriveLocationType(String aName) 
    {
    	// Function implemented because BE input often uses Airport but the name indicate a different location.
    	String	result = null;
    	Map<String, Pattern> locPatternList = LcagBusinessCode.getLocationPatterns();
    	
    	for (String locType : locPatternList.keySet()) {
			Pattern pattern = locPatternList.get(locType);
			
			if (pattern == null) continue;
			
			if (pattern.matcher(aName).matches()) {
				result = locType;
				break;
			}
		}
    	
    	return result;
    }
   
}
