/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hsbo.fbg.sm4c.classify.geotag;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.tasks.geocode.GeocodeParameters;
import com.esri.arcgisruntime.tasks.geocode.GeocodeResult;
import com.esri.arcgisruntime.tasks.geocode.LocatorTask;
import de.hsbo.fbg.sm4c.common.model.Location;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Seba
 */
public class ArcGISGeoTagger implements GeoTagger {
    
   private static final Logger LOGGER = LogManager.getLogger(ArcGISGeoTagger.class);

    private final String locatorURL = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer";
    private final LocatorTask locator;
    private Runnable doneListener;

    public ArcGISGeoTagger() {
        this.locator = new LocatorTask(locatorURL);

    }

    public void addDoneListener(Runnable doneListener) {
        this.doneListener = doneListener;
    }

    @Override
    public Location geocode(String address) {
        Location resultLocation = null;
        GeocodeParameters params = new GeocodeParameters();
        params.setCountryCode("de");
        params.setMaxResults(0);
        List<String> resultAttributeNames = params.getResultAttributeNames();
        resultAttributeNames.add("Place_addr");
        resultAttributeNames.add("Match_addr");
        resultAttributeNames.add("extent");
        final ListenableFuture<List<GeocodeResult>> geocodeFuture = this.locator.geocodeAsync(address, params);
        try {
            List<GeocodeResult> geocodeResults = geocodeFuture.get();
            if (geocodeResults.size() > 0) {
                GeocodeResult topResult = geocodeResults.get(0);
                double score = topResult.getScore();
                Point p = topResult.getDisplayLocation();
                resultLocation = new Location(p.getX(), p.getY());
            }
        } catch (InterruptedException ex) {
            LOGGER.error("Geocoding was interrupted", ex);
        } catch (ExecutionException ex) {
            LOGGER.error("There occured an error during geocoding", ex);
        }
        return resultLocation;
    }

    @Override
    public List<Location> geocode(List<String> addresses) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

@@ -768,36 +768,12 @@
 ger 
-implements GeoTagger %7B%0A    %0A
+%7B%0A%0A 
    p
@@ -1148,132 +1148,8 @@
 oid 
-addDoneListener(Runnable doneListener) %7B%0A        this.doneListener = doneListener;%0A    %7D%0A%0A    @Override%0A    public Location 
 geoc
@@ -1336,17 +1336,17 @@
 Results(
-0
+5
 );%0A     
@@ -1682,14 +1682,139 @@
     
-try %7B%0A
+geocodeFuture.addDoneListener(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                try %7B%0A        
     
@@ -1891,16 +1891,24 @@
         
+        
 if (geoc
@@ -1944,24 +1944,32 @@
             
+ 
+       
 GeocodeResul
@@ -2005,16 +2005,218 @@
 get(0);%0A
+                        String place_addr = (String)topResult.getAttributes().get(%22Place_addr%22);%0A                        String match_addr = (String)topResult.getAttributes().get(%22Match_addr%22);%0A        
         
@@ -2276,16 +2276,24 @@
         
+ 
+       
 Point p 
@@ -2342,22 +2342,24 @@
         
-result
+        
 Location
@@ -2359,16 +2359,20 @@
 ocation 
+loc 
 = new Lo
@@ -2411,26 +2411,191 @@
         
-%7D%0A
+            LOGGER.info(%22Found location x(%22 + loc.getLatitude() + %22)%22%0A                                + %22 (%22 + loc.getLongitude() + %22)%22);%0A                    %7D%0A       
         
 %7D catch 
@@ -2578,32 +2578,33 @@
 %0A               
+ 
 %7D catch (Interru
@@ -2615,32 +2615,40 @@
 Exception ex) %7B%0A
+        
             LOGG
@@ -2690,32 +2690,40 @@
 %22, ex);%0A        
+ 
+       
 %7D catch (Executi
@@ -2744,32 +2744,40 @@
 ) %7B%0A            
+  
+      
 LOGGER.error(%22Th
@@ -2813,32 +2813,40 @@
 eocoding%22, ex);%0A
+        
         %7D%0A      
@@ -2851,51 +2851,32 @@
     
-return resultLocation;%0A
+    %7D%0A    
     %7D
-%0A
+);
 %0A    
-@Override
+%7D%0A
 %0A   

implement asynchronous geocoding