package org.dataone.annotator.matcher.esor;

import java.io.InputStream;
import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.dataone.annotator.matcher.ConceptItem;
import org.dataone.annotator.matcher.ConceptMatcher;
import org.dataone.annotator.matcher.QueryItem;
import org.json.JSONArray;
import org.json.JSONObject;

public class EsorService implements ConceptMatcher {

	private static Log log = LogFactory.getLog(EsorService.class);

	//private static final String REST_URL = "http://127.0.0.1:9100/search";

	//temporary server for testing
	private static final String REST_URL = "http://dataonetwc.tw.rpi.edu/linkipedia/search";


	@Override
	public List<ConceptItem> getConcepts(String fullText) throws Exception {
		return getConcepts(new QueryItem(fullText));
	}

	@Override
	public List<ConceptItem> getConcepts(QueryItem queryItem) throws Exception {
		return lookupEsor(queryItem);
	}

	private static List<ConceptItem> lookupEsor(QueryItem queryItem) throws Exception  {

		HttpClient client = HttpClients.createDefault();
		String uriStr = REST_URL + "?query=" + URLEncoder.encode(queryItem.toString(), "UTF-8");
		//System.out.println(uriStr);

		HttpGet method = new HttpGet(uriStr);
		method.setHeader("Accept", "application/json");
		HttpResponse response = client.execute(method);
		int code = response.getStatusLine().getStatusCode();
		if (2 != code / 100) {
				throw new Exception("response code " + code + " for resource at " + uriStr);
			}
		InputStream body = response.getEntity().getContent();
		String jsonStr = IOUtils.toString(body, "UTF-8");
		System.out.println(jsonStr);

		JSONObject json = new JSONObject(jsonStr);
		String query = json.getString("query");
		JSONArray results = json.getJSONArray("results");

		//analysis the result and return
		ArrayList<ConceptItem> concepts = new ArrayList<ConceptItem>();
		for(int i = 0; i < results.length(); i++){
			JSONObject r = results.getJSONObject(i);
			String url = r.getString("url");
			String score = r.getString("score");
			System.out.println("url=" + url + ", score=" + score);

<<<<<<< HEAD
			//returned result may be empty
			if(url.length()>0) {
				ConceptItem c = new ConceptItem(new URI(url.substring(1, url.length() - 1)), Double.parseDouble(score));
				concepts.add(c);
			}else{
				System.out.println("NA");
			}
=======
			// seems service can return blanks, guard against issues
			if (url == null || url.length() == 0) {
				continue;
			}
			
			ConceptItem c = new ConceptItem(new URI(url.substring(1,url.length()-1)), Double.parseDouble(score));
			concepts.add(c);
>>>>>>> 2d936c002aaf942e8645cc55855405c22c2313ee
		}

		return concepts;
	}
}

@@ -2427,21 +2427,8 @@
 );%0A%0A
-%3C%3C%3C%3C%3C%3C%3C HEAD%0A
 %09%09%09/
@@ -2660,315 +2660,11 @@
 %09%09%7D%0A
-=======%0A%09%09%09// seems service can return blanks, guard against issues%0A%09%09%09if (url == null %7C%7C url.length() == 0) %7B%0A%09%09%09%09continue;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09ConceptItem c = new ConceptItem(new URI(url.substring(1,url.length()-1)), Double.parseDouble(score));%0A%09%09%09concepts.add(c);%0A%3E%3E%3E%3E%3E%3E%3E 2d936c002aaf942e8645cc55855405c22c2313ee
+%09%09%09
 %0A%09%09%7D

fix a merge issue where extra text was added to the java file