/*Copyright (c) 2019 "hbz"

This file is part of etikett.

etikett is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package helper;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;

import models.Etikett;

/**
 * @author Jan Schnasse
 *
 */
public class CrossrefLabelResolver implements LabelResolver {

    final public static String id = "http://dx.doi.org/10.13039";
    final public static String id2 = "https://dx.doi.org/10.13039";
    public final static String DOMAIN = "dx.doi.org";
    public String urlString = null;
    public String label = null;
    public String language = null;
    public Etikett etikett = null;

    public String lookup(String uri, String language) {
        this.urlString = uri;
        this.language = language;
        String etikettLabel = null;
        this.etikett = getEtikett(uri);
        if (etikett != null) {
            etikettLabel = etikett.getLabel();
            runLookupThread();
        } else {
            etikett = new Etikett(urlString);
            lookupAsync(urlString, language);
            etikettLabel = label;
        }
        return etikettLabel;
    }

    private void lookupAsync(String uri, String language) {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Accept", "text/html");

        try (InputStream in = urlToInputStream(new URL(uri), headers)) {
            String str = CharStreams.toString(new InputStreamReader(in, Charsets.UTF_8));
            JsonNode hit = new ObjectMapper().readValue(str, JsonNode.class);
            label = hit.at("/person/name/family-name/value").asText() + ", "
                    + hit.at("/person/name/given-names/value").asText();
            if (label != null) {
                etikett.setLabel(label);
                cacheEtikett(etikett);
            }
        } catch (Exception e) {
            play.Logger.info("Failed to find label for " + uri);
        }
    }

    private InputStream urlToInputStream(URL url, Map<String, String> args) {

        Connector hConn = Connector.Factory.getInstance(url);
        if (args != null) {
            for (Entry<String, String> e : args.entrySet()) {
                hConn.setConnectorProperty(e.getKey(), e.getValue());
            }
        }
        hConn.connect();
        return hConn.getInputStream();
    }

    @Override
    public void run() {

        lookupAsync(urlString, language);

    }

    private void runLookupThread() {

        Thread thread = new Thread(this);
        thread.start();
    }

    private Etikett getEtikett(String urlString) {
        EtikettMaker eMaker = new EtikettMaker();
        return eMaker.getValue(urlString);
    }

    private void cacheEtikett(Etikett etikett) {
        EtikettMaker eMaker = new EtikettMaker();
        eMaker.addJsonDataIntoDBCache(etikett);
    }

}

@@ -2060,32 +2060,76 @@
 ing language) %7B%0A
+        if (isCrossrefFunderUrl(uri)) %7B%0A    
         HashMap%3C
@@ -2193,16 +2193,20 @@
         
+    
 headers.
@@ -2233,17 +2233,20 @@
 html%22);%0A
-%0A
+    
         
@@ -2306,24 +2306,91 @@
 headers)) %7B%0A
+                play.Logger.debug(%22Stream: %22 + in.toString());%0A    
             
@@ -2479,16 +2479,20 @@
         
+  
+  
 JsonNode
@@ -2561,16 +2561,27 @@
         
+    String 
 label = 
@@ -2594,115 +2594,42 @@
 (%22/p
-erson/name/family-name/value%22).asText() + %22, %22%0A                    + hit.at(%22/person/name/given-names/value
+refLabel/Label/literalForm/content
 %22).a
@@ -2645,24 +2645,28 @@
             
+   
+ 
 if (label !=
@@ -2666,32 +2666,36 @@
 abel != null) %7B%0A
+    
                 
@@ -2735,16 +2735,20 @@
         
+    
 cacheEti
@@ -2766,34 +2766,42 @@
 t);%0A            
+   
+ 
 %7D%0A
+    
         %7D catch 
@@ -2820,32 +2820,36 @@
 ) %7B%0A            
+    
 play.Logger.info
@@ -2848,12 +2848,12 @@
 ger.
-info
+warn
 (%22Fa
@@ -2881,24 +2881,141 @@
 r %22 + uri);%0A
+            %7D%0A        %7D else %7B%0A            play.Logger.debug(%22Nothing to do here: DOI is not a CrossrefFunder DOI%22);%0A
         %7D%0A  
@@ -3008,24 +3008,25 @@
 ;%0A        %7D%0A
+%0A
     %7D%0A%0A    p
@@ -3412,24 +3412,244 @@
 m();%0A    %7D%0A%0A
+    private boolean isCrossrefFunderUrl(String urlString) %7B%0A        boolean isFunderUrl = false;%0A        if (urlString.contains(%2210.13039%22)) %7B%0A            isFunderUrl = true;%0A        %7D%0A        return isFunderUrl;%0A    %7D%0A%0A
     @Overrid

fix GND https issue