package cl.uach.inf.sophia.engines.input;

import java.util.ArrayList;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

public class SophiaAPIConnector {

	final String PARAM_SOPHIA_API_ARTICLES = "http://api.sophia-project.info/v2/articles/category/unclassified/";
	final String PARAM_SOPHIA_API_PUT_ARTICLES = "http://api.sophia-project.info/v2/articles/";

	final String PARAM_SOPHIA_API_UNCLASSIFIED = "http://api.sophia-project.info/v2/articles/category/unclassified/begin";

	final String PARAM_SOPHIA_API_SEARCH = "http://api.sophia-project.info/v2/search/page/1/";
	
	final String PARAM_SOPHIA_API_ARTICLES_PER_DATES = "http://api.sophia-project.info/v2/articles/"; //date1/date2/
	
	final String PARAM_SOPHIA_NEWAPI_PUT_ARTICLES = "http://146.83.216.216:3000/api/ArticleAnnotations";

	public JSONArray getArticlesBetweenDates(Map<String,Object> map) throws UnirestException{

		HttpResponse<JsonNode> jsonResponse = Unirest.post(PARAM_SOPHIA_API_SEARCH)
				.header("accept", "application/json").fields(map)
				.asJson();

		JSONObject jsonObject = jsonResponse.getBody().getObject();

		JSONObject hits = jsonObject.getJSONObject("hits");
		JSONObject hits2 = hits.getJSONObject("hits");
		JSONArray hitsArray = hits2.getJSONArray("hits");
		return hitsArray;
	}	

	public void putArticleCategory(String articleID, String category) throws UnirestException{
		System.out.println(PARAM_SOPHIA_API_PUT_ARTICLES+articleID+"/");
		HttpResponse<JsonNode> jsonResponse = Unirest.put(PARAM_SOPHIA_API_PUT_ARTICLES+articleID+"/")
				.header("accept", "application/json")
				.field("art_category", category).asJson();	
	}
	
	public void putArticleCategoryInNewAPI(String articleID, String articleDate, String category) throws UnirestException{
		System.out.println(PARAM_SOPHIA_NEWAPI_PUT_ARTICLES);
		HttpResponse<JsonNode> jsonResponse = Unirest.put(PARAM_SOPHIA_NEWAPI_PUT_ARTICLES)
				.header("accept", "application/json")
				.field("idArticle", articleID)
				.field("origin","sophiaTAL")
				.field("words", new String[]{"test"})
				.field("date", articleDate)
				.field("category", category)
				.field("id", articleID).asJson();	
		
		System.out.println(jsonResponse.getStatus()+" "+jsonResponse.getStatusText()+" "+jsonResponse.getBody());
	}

	public JSONArray getArticles() throws UnirestException{
		HttpResponse<JsonNode> response = Unirest.get(PARAM_SOPHIA_API_ARTICLES).asJson();
		// retrieve the parsed JSONObject from the response
		JSONObject jsonObject = response.getBody().getObject();

		JSONObject hits = jsonObject.getJSONObject("hits");
		JSONArray hitsArray = hits.getJSONArray("hits");
		System.out.println(hitsArray.toString());
		return hitsArray;
	}

	public int postArticles(Map<String,Object> map) throws UnirestException{

		HttpResponse<JsonNode> jsonResponse = Unirest.post(PARAM_SOPHIA_API_ARTICLES)
				.header("accept", "application/json").fields(map)
				.asJson();
		return jsonResponse.getStatus();
	}


	/********************** TEST **************************************/

	public static final void main(final String[] args) throws Exception {
		SophiaAPIConnector x = new SophiaAPIConnector();
		//x.runTest1();
		x.runTest2();
	}

	private void runTest1() throws UnirestException {
		HttpResponse<JsonNode> jsonResponse = Unirest.post(PARAM_SOPHIA_API_ARTICLES)
				.header("accept", "application/json")
				.field("parameter", "value")
				.field("foo", "bar")
				.asJson();
	}

	private void runTest2() throws UnirestException {
		HttpResponse<JsonNode> jsonResponse = Unirest.post(PARAM_SOPHIA_API_SEARCH)
				.header("accept", "application/json")
				.field("index", "articles")
				.field("fields", new ArrayList<String>().add("art_content"))
				.asJson();
		JSONObject jsonObject = jsonResponse.getBody().getObject();
		System.out.println(jsonObject);
		JSONObject hits = jsonObject.getJSONObject("hits");
		JSONObject hits2 = hits.getJSONObject("hits");
		JSONArray hitsArray = hits2.getJSONArray("hits");
		
		System.out.println(hitsArray.length());
	}

}
