package cgeo.geocaching.location;

import static org.assertj.core.api.Assertions.assertThat;

import cgeo.CGeoTestCase;
import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.utils.Log;

import org.apache.commons.lang3.StringUtils;
import org.assertj.core.data.Offset;

import rx.Observable;

import android.location.Address;
import android.location.Geocoder;

public class GeocoderTest extends CGeoTestCase {

    private static final String TEST_ADDRESS = "46 rue Barrault, Paris, France";
    private static final double TEST_LATITUDE = 48.82677;
    private static final double TEST_LONGITUDE = 2.34644;
    private static final Geopoint TEST_COORDS = new Geopoint(TEST_LATITUDE, TEST_LONGITUDE);
    private static final Offset<Double> TEST_OFFSET = Offset.offset(0.00050);

    public static void testAndroidGeocoder() {
        // Some emulators don't have access to Google Android geocoder
        if (Geocoder.isPresent()) {
            final AndroidGeocoder geocoder = new AndroidGeocoder(CgeoApplication.getInstance());
            testGeocoder(geocoder.getFromLocationName(TEST_ADDRESS), "Android", true);
            testGeocoder(geocoder.getFromLocation(TEST_COORDS), "Android reverse", true);
        } else {
            Log.i("not testing absent Android geocoder");
        }
    }

    public static void testGCGeocoder() {
        testGeocoder(GCGeocoder.getFromLocationName(TEST_ADDRESS), "GC", false);
    }

    public static void testMapQuestGeocoder() {
        testGeocoder(MapQuestGeocoder.getFromLocationName(TEST_ADDRESS), "MapQuest", true);
        testGeocoder(MapQuestGeocoder.getFromLocation(TEST_COORDS), "MapQuest reverse", true);
    }

    public static void testGeocoder(final Observable<Address> addressObservable, final String geocoder, final boolean withAddress) {
        final Address address = addressObservable.toBlocking().first();
        assertThat(address.getLatitude()).as(describe("latitude", geocoder)).isCloseTo(TEST_LATITUDE, TEST_OFFSET);
        assertThat(address.getLongitude()).as(describe("longitude", geocoder)).isCloseTo(TEST_LONGITUDE, TEST_OFFSET);
        if (withAddress) {
            assertThat(StringUtils.lowerCase(address.getAddressLine(0))).as(describe("street address", geocoder)).startsWith("46 rue barrault");
            assertThat(address.getLocality()).as(describe("locality", geocoder)).isEqualTo("Paris");
            assertThat(address.getCountryCode()).as(describe("country code", geocoder)).isEqualTo("FR");
            // don't assert on country name, as this can be localized, e.g. with the mapquest geocoder
        }
    }

    private static String describe(final String field, final String geocoder) {
        return new StringBuilder(field).append(" for ").append(geocoder).append(" .geocoder").toString();
    }

}

@@ -362,16 +362,42 @@
 coder;%0A%0A
+import java.util.Locale;%0A%0A
 public c
@@ -846,32 +846,143 @@
 oidGeocoder() %7B%0A
+        final Locale locale = Locale.getDefault();%0A        try %7B%0A            Locale.setDefault(Locale.US);%0A    
         // Some 
@@ -1040,24 +1040,28 @@
 der%0A        
+    
 if (Geocoder
@@ -1072,24 +1072,28 @@
 resent()) %7B%0A
+    
             
@@ -1177,16 +1177,20 @@
 nce());%0A
+    
         
@@ -1272,32 +1272,36 @@
 e);%0A            
+    
 testGeocoder(geo
@@ -1370,16 +1370,20 @@
         
+    
 %7D else %7B
@@ -1383,16 +1383,20 @@
  else %7B%0A
+    
         
@@ -1441,24 +1441,97 @@
 geocoder%22);%0A
+            %7D%0A        %7D finally %7B%0A            Locale.setDefault(locale);%0A
         %7D%0A  
@@ -1705,32 +1705,143 @@
 estGeocoder() %7B%0A
+        final Locale locale = Locale.getDefault();%0A        try %7B%0A            Locale.setDefault(Locale.US);%0A    
         testGeoc
@@ -1916,32 +1916,36 @@
  true);%0A        
+    
 testGeocoder(Map
@@ -2011,24 +2011,93 @@
 se%22, true);%0A
+        %7D finally %7B%0A            Locale.setDefault(locale);%0A        %7D%0A
     %7D%0A%0A    p
@@ -2676,23 +2676,18 @@
 r)).
-startsWith(%2246 
+contains(%22
 rue 

fix #4627: Android geocoder test is not robust