package plugin.google.maps;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

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

import com.google.android.gms.maps.model.LatLngBounds;

import android.location.Address;
import android.location.Geocoder;
import android.util.Log;

public class PluginGeocoder extends MyPlugin implements MyPluginInterface {

  @SuppressWarnings("unused")
  private void createGeocoder(final JSONArray args,
      final CallbackContext callbackContext) throws JSONException, IOException {

    JSONObject opts = args.getJSONObject(1);
    Geocoder geocoder = new Geocoder(this.cordova.getActivity());
    List<Address> geoResults;
    JSONArray results = new JSONArray();
    Iterator<Address> iterator = null;

    // Geocoding
    if (opts.has("position") == false && opts.has("address")) {
      String address = opts.getString("address");
      if (opts.has("bounds")) {
        if (opts.has("bounds") == true) {
          JSONArray points = opts.getJSONArray("bounds");
          LatLngBounds bounds = PluginUtil.JSONArray2LatLngBounds(points);
          geoResults = geocoder.getFromLocationName(address, 20,
                bounds.southwest.latitude, bounds.southwest.longitude,
                bounds.northeast.latitude, bounds.northeast.longitude);
          iterator = geoResults.iterator();
        }
      } else {
        geoResults = geocoder.getFromLocationName(address, 20);
        iterator = geoResults.iterator();
      }
    }

    // Reverse geocoding
    if (opts.has("position") && opts.has("address") == false) {
      JSONObject position = opts.getJSONObject("position");
      geoResults = geocoder.getFromLocation(
          position.getDouble("lat"), 
          position.getDouble("lng"), 20);
      iterator = geoResults.iterator();
    }
    
    if (iterator == null) {
      callbackContext.error("Invalid request for geocoder");
      return;
    }
    
    while(iterator.hasNext()) {
      JSONObject result = new JSONObject();
      Address addr = iterator.next();

      JSONObject position = new JSONObject();
      position.put("lat", addr.getLatitude());
      position.put("lng", addr.getLongitude());
      result.put("position", position);

      result.put("locality", addr.getLocality());
      result.put("adminArea", addr.getAdminArea());
      result.put("country", addr.getCountryCode());
      result.put("featureName", addr.getFeatureName());
      result.put("locale", addr.getLocale());
      result.put("phone", addr.getPhone());
      result.put("permises", addr.getPremises());
      result.put("postalCode", addr.getPostalCode());
      result.put("subAdminArea", addr.getSubAdminArea());
      result.put("subLocality", addr.getSubLocality());
      result.put("subThoroughfare", addr.getSubThoroughfare());
      result.put("thoroughfare", addr.getThoroughfare());
      
      results.put(result);
    }
    callbackContext.success(results);
    
  }

}

@@ -99,16 +99,38 @@
 il.List;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -257,64 +257,8 @@
 t;%0A%0A
-import com.google.android.gms.maps.model.LatLngBounds;%0A%0A
 impo
@@ -339,16 +339,73 @@
 oid.
-util.Log
+os.Bundle;%0A%0Aimport com.google.android.gms.maps.model.LatLngBounds
 ;%0A%0Ap
@@ -2514,181 +2514,31 @@
 ut(%22
-featureName%22, addr.getFeatureName());%0A      result.put(%22locale%22, addr.getLocale());%0A      result.put(%22phone%22, addr.getPhone());%0A      result.put(%22permises%22, addr.getPremises
+locale%22, addr.getLocale
 ());
@@ -2828,22 +2828,630 @@
 are());%0A
-
       
+%0A%0A      JSONObject extra = new JSONObject();%0A      extra.put(%22featureName%22, addr.getFeatureName());%0A      extra.put(%22phone%22, addr.getPhone());%0A      extra.put(%22permises%22, addr.getPremises());%0A      extra.put(%22url%22, addr.getUrl());%0A%0A      %0A      Bundle extraInfo = addr.getExtras();%0A      if (extraInfo != null) %7B%0A        Set%3CString%3E keys = extraInfo.keySet();%0A        Iterator%3CString%3E keyIterator = keys.iterator();%0A        String key;%0A        while(keyIterator.hasNext()) %7B%0A          key = keyIterator.next();%0A          extra.put(key, extraInfo.get(key));%0A        %7D%0A      %7D%0A      result.put(%22extra%22, extra);
 %0A      r

Add extra property for the results of geocoder