package cgeo.geocaching.address;

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

import android.location.Address;
import android.location.Geocoder;
import android.test.suitebuilder.annotation.Suppress;

import java.util.Locale;

import io.reactivex.rxjava3.core.Single;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.data.Offset;
import static org.assertj.core.api.Java6Assertions.assertThat;

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() {
        final Locale locale = Locale.getDefault();
        try {
            Locale.setDefault(Locale.US);
            // 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).firstOrError(), "Android", true);
                testGeocoder(geocoder.getFromLocation(TEST_COORDS), "Android reverse", true);
            } else {
                Log.i("not testing absent Android geocoder");
            }
        } finally {
            Locale.setDefault(locale);
        }
    }

    @Suppress // Do not run test on MapQuest, quotas are very low and exhaust easily.
    public static void testMapQuestGeocoder() {
        final Locale locale = Locale.getDefault();
        try {
            Locale.setDefault(Locale.US);
            testGeocoder(MapQuestGeocoder.getFromLocationName(TEST_ADDRESS).firstOrError(), "MapQuest", true);
            testGeocoder(MapQuestGeocoder.getFromLocation(TEST_COORDS), "MapQuest reverse", true);
        } finally {
            Locale.setDefault(locale);
        }
    }

    public static void testGeocoder(final Single<Address> addressObservable, final String geocoder, final boolean withAddress) {
        final Address address = addressObservable.blockingGet();
        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)).contains("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 field + " for " + geocoder + " .geocoder";
    }

}

@@ -2315,32 +2315,50 @@
  withAddress) %7B%0A
+        try %7B%0A    
         final Ad
@@ -2402,24 +2402,28 @@
 ckingGet();%0A
+    
         asse
@@ -2526,32 +2526,36 @@
 FFSET);%0A        
+  
+  
 assertThat(addre
@@ -2641,32 +2641,36 @@
 , TEST_OFFSET);%0A
+    
         if (with
@@ -2684,32 +2684,36 @@
 ) %7B%0A            
+    
 assertThat(Strin
@@ -2824,32 +2824,36 @@
 %22);%0A            
+  
+  
 assertThat(addre
@@ -2921,24 +2921,28 @@
 o(%22Paris%22);%0A
+    
             
@@ -3046,16 +3046,20 @@
         
+    
 // don't
@@ -3133,32 +3133,739 @@
 pquest geocoder%0A
+            %7D%0A        %7D catch (RuntimeException re) %7B%0A            //protect against known Android bug (as of July 2020). For more information see:%0A            // https://stackoverflow.com/questions/47331480/geocoder-getfromlocation-grpc-failed-on-android-real-device?rq=1%0A            // https://issuetracker.google.com/issues/64418751?pli=1%0A            // https://issuetracker.google.com/issues/64247769%0A            //Check may be removed when this bug is resolved%0A            if (re.getMessage() != null && re.getMessage().endsWith(%22grpc failed%22)) %7B%0A                Log.i(%22AndroidGeocoder test failed with known bug 'grpc failed', is ignored%22);%0A            %7D else %7B%0A                throw re;%0A            %7D%0A
         %7D%0A    %7D%0A

add handling for known-bug in Android GeoCoder whilest testing