package cordova.plugin.nativegeocoder;

import android.location.Address;
import android.location.Geocoder;
import android.text.TextUtils;

import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class NativeGeocoder extends CordovaPlugin {
    
    Geocoder geocoder;

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        System.out.print("NativeGeocoder initialize");
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        if (action.equals("reverseGeocode")) {
            double latitude = args.getDouble(0);
            double longitude = args.getDouble(1);
            this.reverseGeocode(latitude, longitude, callbackContext);
            return true;
        }

        if (action.equals("forwardGeocode")) {
            String addressString = args.getString(0);
            this.forwardGeocode(addressString, callbackContext);
            return true;
        }

        return false;
        
    }

    private void reverseGeocode(double latitude, double longitude, CallbackContext callbackContext) {

        geocoder = new Geocoder(cordova.getActivity().getApplicationContext(), Locale.getDefault());

        if (!geocoder.isPresent()) {
            PluginResult r = new PluginResult(PluginResult.Status.ERROR, "Geocoder is not present on this device/emulator.");
            callbackContext.sendPluginResult(r);
            return;
        }

        try {
            List<Address> geoResults = geocoder.getFromLocation(latitude, longitude, 1);
            if (geoResults.size() > 0) {
                Address address = geoResults.get(0);
                ArrayList<String> addressFragments = new ArrayList<String>();

                // Fetch the address lines using getAddressLine,
                // join them, and send them to the thread.
                int maxLines = address.getMaxAddressLineIndex();
                for (int i = 0; i < maxLines; i++) {
                    addressFragments.add(address.getAddressLine(i));
                }

                String addressStr = TextUtils.join(System.getProperty("line.separator"), addressFragments) + ", " + address.getCountryName();
                JSONObject resultObj = new JSONObject();
                resultObj.put("address", addressStr);
                resultObj.put("countryCode", address.getCountryCode());

                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, resultObj));
            } else {
                PluginResult r = new PluginResult(PluginResult.Status.ERROR, "Cannot get a address.");
                callbackContext.sendPluginResult(r);
            }
        } catch (Exception e) {
            PluginResult r = new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            callbackContext.sendPluginResult(r);
        }
    }

    private void forwardGeocode(String addressString, CallbackContext callbackContext) {

        geocoder = new Geocoder(cordova.getActivity().getApplicationContext(), Locale.getDefault());

        if (!geocoder.isPresent()) {
            PluginResult r = new PluginResult(PluginResult.Status.ERROR, "Geocoder is not present on this device/emulator.");
            callbackContext.sendPluginResult(r);
            return;
        }

        if (addressString != null && addressString.length() > 0) {

            try {
                List<Address> geoResults = geocoder.getFromLocationName(addressString, 1);
                if (geoResults.size()>0) {
                    Address addr = geoResults.get(0);
                    System.out.print(addr);
                    
                    JSONObject coordinates = new JSONObject();
                    coordinates.put("latitude", addr.getLatitude());
                    coordinates.put("longitude", addr.getLongitude());

                    System.out.print(coordinates);
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, coordinates));
                } else {
                    PluginResult r = new PluginResult(PluginResult.Status.ERROR, "Cannot get a location.");
                    callbackContext.sendPluginResult(r);
                }
            
            } catch (Exception e) {
                PluginResult r = new PluginResult(PluginResult.Status.ERROR, e.getMessage());
                callbackContext.sendPluginResult(r);
            }
            
        } else {
            PluginResult r = new PluginResult(PluginResult.Status.ERROR, "Expected a non-empty string argument.");
            callbackContext.sendPluginResult(r);
        }
    }
    
}
@@ -1892,378 +1892,206 @@
     
-ArrayList%3CString%3E addressFragments = new ArrayList%3CString%3E();%0A%0A                // Fetch the address lines using getAddressLine,%0A                // join them, and send them to the thread.%0A                int maxLines = address.getMaxAddressLineIndex();%0A                for (int i = 0; i %3C maxLines; i++) %7B%0A                    addressFragments.add(address.getAddressLin
+%0A                JSONObject resultObj = new JSONObject();%0A                resultObj.put(%22street%22, address.getThoroughfare());%0A                resultObj.put(%22houseNumber%22, address.getSubThoroughfar
 e(
-i
 ));%0A
@@ -2110,126 +2110,35 @@
     
-%7D%0A%0A                String addressStr = TextUtils.join(System.getProperty(%22line.separator%22), addressFragments) + %22, %22 +
+resultObj.put(%22postalCode%22,
  add
@@ -2149,21 +2149,21 @@
 .get
-CountryNam
+PostalCod
 e()
+)
 ;%0A  
@@ -2180,100 +2180,123 @@
     
-JSONObject resultObj = new JSONObject();%0A                resultObj.put(%22address%22, addressStr
+resultObj.put(%22city%22, address.getLocality());%0A                resultObj.put(%22countryName%22, address.getCountryName()
 );%0A 
@@ -2581,16 +2581,17 @@
 ot get a
+n
  address
@@ -3537,16 +3537,19 @@
 ess addr
+ess
  = geoRe
@@ -3566,72 +3566,8 @@
 0);%0A
-                    System.out.print(addr);%0A                    
 %0A   
@@ -3674,24 +3674,27 @@
 itude%22, addr
+ess
 .getLatitude
@@ -3751,16 +3751,19 @@
 e%22, addr
+ess
 .getLong
@@ -3777,59 +3777,8 @@
 );%0A%0A
-                    System.out.print(coordinates);%0A
     

update java plugin