package fi.nls.oskari.integration.sotka;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import fi.nls.oskari.log.LogFactory;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import com.github.kevinsawicki.http.HttpRequest;

import fi.nls.oskari.log.Logger;

/**
 * Parser (JSON) for getting Sotka region ids and codes
 */
public class SotkaRegionParser {
	private static final String URL = "http://www.sotkanet.fi/rest/1.1/regions";
	private static final String ID_FIELD = "id";
	private static final String CODE_FIELD = "code";
	private static final String CATEGORY_FIELD = "category";
	private static final String REGION_CATEGORY = "KUNTA";
	private ObjectMapper mapper;
	
    private final static Logger log = LogFactory.getLogger(SotkaRegionParser.class);

	private Map<String, Integer> regionsByCode;
	private Map<Integer, String> regionsById;

	/**
	 * Inits parser and maps and triggers parsing
	 */
	public SotkaRegionParser() {
		mapper = new ObjectMapper();
		regionsByCode = new HashMap<String, Integer>();
		regionsById = new HashMap<Integer, String>();
		
		try {
			getData();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
     * Gets code from the HashMap
	 * @param id
     * @return if in map returns the code, otherwise null
	 */
    public String getCode(int id) {
    	if(regionsById.containsKey(id))
    		return regionsById.get(id);
    	return null;
    }

    /**
     * Gets id from the HashMap
     * @param code
     * @return if in map returns the id, otherwise -1
     */
    public int getId(String code) {
    	if(regionsByCode.containsKey(code))
    		return regionsByCode.get(code);
    	return -1;
    }
    
    /**
     * Makes HTTP get request and parses the responses JSON into HashMaps.
     * @throws IOException
     */
    private void getData() throws IOException{
    	String json = HttpRequest.get(URL).body();
		JsonFactory factory = new JsonFactory();
		JsonParser parser = factory.createJsonParser(json);
		
		Map<String,Object> region = null;
		
		parser.nextToken();
		while(parser.nextToken() == JsonToken.START_OBJECT) {
			region = mapper.readValue(parser, new TypeReference<Map<String,Object>>() { });
	        if(region.containsKey(CATEGORY_FIELD)) {
	        	if(REGION_CATEGORY.equals(region.get(CATEGORY_FIELD))) {
					regionsByCode.put((String) region.get(CODE_FIELD), (Integer) region.get(ID_FIELD));
					regionsById.put((Integer) region.get(ID_FIELD), (String) region.get(CODE_FIELD));
	        	}
	        }
		}
    }
}

@@ -718,30 +718,29 @@
 = %22code%22;%0A%09p
-rivate
+ublic
  static fina
@@ -1038,16 +1038,81 @@
 onsById;
+%0A    private Map%3CInteger, Map%3CString,Object%3E%3E regionsObjectsById;
 %0A%0A%09/**%0A%09
@@ -1321,16 +1321,89 @@
 ing%3E();%0A
+        regionsObjectsById = new HashMap%3CInteger, Map%3CString,Object%3E%3E();%0A
 %09%09%0A%09%09try
@@ -1985,16 +1985,244 @@
 1;%0A    %7D
+%0A%0A    /**%0A     * Gets id from the HashMap%0A     * @param id%0A     * @return if in map returns the region data else null%0A     */%0A    public Map%3CString,Object%3E getRegionById(int id) %7B%0A        return regionsObjectsById.get(id);%0A    %7D
 %0A    %0A  
@@ -2649,16 +2649,16 @@
 JECT) %7B%0A
-
 %09%09%09regio
@@ -2732,16 +2732,92 @@
 ) %7B %7D);%0A
+            regionsObjectsById.put((Integer) region.get(ID_FIELD), region);%0A
 %09       

Save link to whole region data to get category-mappings working correctly