package eu.freme.eservices.eentity.api;

import org.springframework.http.HttpStatus;

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

import eu.freme.eservices.eentity.exceptions.BadRequestException;
import eu.freme.eservices.eentity.exceptions.ExternalServiceFailedException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;

public class EEntityService {
    
    private String dbpediaSpotlightURL = "http://spotlight.nlp2rdf.aksw.org/spotlight?f=text&t=direct&confidence=";
    private String fremeNERURL = "http://139.18.2.231:8080/api/entities?";
    
    public String callDBpediaSpotlight(String text, String confidenceParam, String languageParam, String prefix)
            throws ExternalServiceFailedException, BadRequestException {
        
        try {
            // if confidence param is not set, the default value is 0.3
            if(confidenceParam == null) {
                confidenceParam = "0.3";
            }
           
            HttpResponse<String> response = Unirest.post(dbpediaSpotlightURL+confidenceParam+"&prefix="+prefix)
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .body("i="+URLEncoder.encode(text, "UTF-8")).asString();
            
            if (response.getStatus() != HttpStatus.OK.value()) {
                if( response.getStatus() == HttpStatus.BAD_REQUEST.value() ) {
                    throw new BadRequestException(response.getBody());
                } else {
                    throw new ExternalServiceFailedException(response.getBody());
                }
            }
            String nif = response.getBody();
            return nif;
        } catch (UnirestException e) {
            throw new ExternalServiceFailedException(e.getMessage());
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(EEntityService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public String callFremeNER(String text, String languageParam, String prefix, String dataset, int numLinks)
            throws ExternalServiceFailedException, BadRequestException {
        
        try {
//            System.out.println(text);
//            System.out.println(URLDecoder.decode(text, "UTF-8"));
            System.out.println("links: "+numLinks);
            HttpResponse<String> response = Unirest.post(fremeNERURL+"language="+languageParam+"&dataset="+dataset+"&prefix="+prefix+"&numLinks="+numLinks)
                    .header("Content-Type", "text/plain; charset=UTF-8")
//                    .body(URLEncoder.encode(text, "UTF-8").replaceAll("\\+", " ")).asString();
                    .body(text).asString();
            
//            HttpResponse<String> response = Unirest.post(fremeNERURL+"language="+languageParam+"&dataset="+dataset)
//                    .header("Content-Type", "application/x-www-form-urlencoded")
//                    .body("i="+URLEncoder.encode(text, "UTF-8")).asString();
            
            if (response.getStatus() != HttpStatus.OK.value()) {
                if( response.getStatus() == HttpStatus.BAD_REQUEST.value() ) {
                    throw new BadRequestException(response.getBody());
                } else {
                    throw new ExternalServiceFailedException(response.getBody());
                }
            }
            String nif = response.getBody();
            return nif;
        } catch (UnirestException e) {
            throw new ExternalServiceFailedException(e.getMessage());
        }
//        catch (UnsupportedEncodingException ex) {
//            Logger.getLogger(EEntityService.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return null;
    }
}

@@ -978,16 +978,170 @@
   try %7B%0A
+            %0A            if(prefix.equals(%22http://freme-project.eu/%22)) %7B%0A                prefix = %22http://freme-project.eu/#%22;%0A            %7D%0A            %0A
         
@@ -1417,22 +1417,49 @@
 refix=%22+
-prefix
+URLEncoder.encode(prefix,%22UTF-8%22)
 )%0A      
@@ -2014,32 +2014,71 @@
 onse.getBody();%0A
+//            System.out.println(nif);%0A
             retu
@@ -2871,22 +2871,49 @@
 refix=%22+
-prefix
+URLEncoder.encode(prefix,%22UTF-8%22)
 +%22&numLi
@@ -3972,32 +3972,174 @@
 .getMessage());%0A
+        %7D catch (UnsupportedEncodingException ex) %7B%0A            Logger.getLogger(EEntityService.class.getName()).log(Level.SEVERE, null, ex);%0A
         %7D%0A//    
@@ -4307,20 +4307,121 @@
 eturn null;%0A
+        throw new ExternalServiceFailedException(%22External service failed to process the request.%22);%0A
     %7D%0A%7D%0A

fix for https://github.com/freme-project/e-Entity/issues/38 https://github.com/freme-project/e-Entity/issues/37 https://github.com/freme-project/e-Entity/issues/35 https://github.com/freme-project/e-Entity/issues/34 https://github.com/freme-project/e-Entity/issues/33