package org.gcube.nlphub.mapper;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Set;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.apache.log4j.Logger;
import org.gcube.nlphub.legacy.NerAlgorithm;
import org.gcube.nlphub.legacy.NerAnnotationData;
import org.gcube.nlphub.legacy.NerEntity;

public class DefaultMapper implements JsonMapper {
	private Logger logger = Logger.getLogger(DefaultMapper.class.getSimpleName());

	public String getJson(String alg, String link) {
		NerAlgorithm algInfo = new NerAlgorithm(alg);
		try {
			HttpURLConnection connection = (HttpURLConnection) new URL(link).openConnection();
			connection.setRequestMethod("GET");
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			JsonObject jsonRoot = (JsonObject) new JsonParser().parse(reader);
			JsonElement entities = jsonRoot.get("entities");
			if ((entities != null) && (entities.isJsonObject())) {
				JsonObject e = (JsonObject) entities;
				Set<String> properties = e.keySet();
				for (String p : properties) {
					JsonElement property = e.get(p);
					if (!property.isJsonArray())
						continue;
					NerAnnotationData data = new NerAnnotationData(p);
					JsonArray jsonAnnotation = (JsonArray) property;
					int size = jsonAnnotation.size();
					for (int i = 0; i < size; i++) {
						JsonElement ann = jsonAnnotation.get(i);
						if (ann.isJsonObject()) {
							JsonObject annotation = (JsonObject) ann;
							Set<String> fields = annotation.keySet();
							JsonArray indices = (JsonArray) annotation.get("indices");
							NerEntity ne = new NerEntity(indices.get(0).getAsInt(), indices.get(1).getAsInt());

							for (String f : fields) {
								if (f.equals("indices"))
									continue;
								JsonElement je = annotation.get(f);
								if (!je.isJsonArray() && !je.isJsonObject() && !je.isJsonNull()) {
									ne.addProperty(f, je.getAsString());
								}
							}
							data.addNerEntity(ne);
						}
					}
					algInfo.addAnnotationData(data);
				}
			}
			return algInfo.toJson().toString();
		} catch (Exception x) {
			logger.error(x.getLocalizedMessage());
			return null;
		}
	}
	
	/*
	public static void main(String[] args) {
		String remoteJson = "http://data.d4science.org/a2JtV0lOUUxsenZCM2RmcFRqVGtWWm42NU9LQnFGTk9HbWJQNStIS0N6Yz0-VLT";
		String alg = "ENGLISH_NAMED_ENTITY_RECOGNIZER";
		//remoteJson = "http://data.d4science.org/T21IcHlMSzFJRCttZ1lHVzMxd2dyWjVOUEpyY2dwUVNHbWJQNStIS0N6Yz0-VLT";
		//remoteJson = "http://data.d4science.org/bDZLVkdlaVBjZCtLSWsrWUNQdHk3MUs4cEtDUGF5NktHbWJQNStIS0N6Yz0";
		DefaultMapper dm = new DefaultMapper();
		try {
			String s = dm.getJson(alg, remoteJson);
			System.out.println("" + s);
			String file = "/home/erico/debug.json";
			java.io.File f = new java.io.File(file);
			f.createNewFile();
			java.io.FileWriter w = new java.io.FileWriter(f);
			w.write(s);
			w.flush();
			w.close();
		} catch (Exception x) {
			System.out.println(x.getLocalizedMessage());
			x.printStackTrace();
		}
		
	}*/
}
