package com.eteng.geolocation.baidu;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;

import android.util.Log;

public class GeolocationPlugin extends CordovaPlugin {

  private static final String TAG = "BaiduGeoLocationPlugin";

  private static final String ACTION_GET_CURRENT_POSITION = "getCurrentPosition";
  private static final String ACTION_WATCH_POSITION = "watchPosition";
  private static final String ACTION_CLEAR_WATCH = "clearWatch";

  private Map<int, CDVLocationListener> watches = new HashMap<int, CDVLocationListener>();

  @Override
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (ACTION_GET_CURRENT_POSITION.equals(action)) {
      JSONObject options = args.getJSONObject(0);
      return getCurrentPosition(options, callbackContext);
    } else if (ACTION_WATCH_POSITION.equals(action)) {
      JSONObject options = args.getJSONObject(0);
      return watchPosition(options, callbackContext);
    } else if (ACTION_CLEAR_WATCH.equals(action)) {
      int watchId = args.getInt(0);
      return clearWatch(watchId, callbackContext);
    }
    return false;
  }

  private boolean clearWatch(int watchId, CallbackContext callback) {
    Log.i(TAG, "停止监听");
    return false;
  }

  private boolean watchPosition(JSONObject options, CallbackContext callback) {
    Log.i(TAG, "监听位置变化");
    Context ctx = cordova.getActivity().getApplicationContext();
    PositionOptions positionOpts = new PositionOptions(options);
    CDVLocationListener listener = new CDVLocationListener(ctx);
    return listener.watchPosition(positionOpts, new CDVPositionListener() {
      @Override
      public void onReceivePosition(JSONArray message) {
        callback.setNoStop(true);
        callback.send(message);
      }
    });
  }

  private boolean getCurrentPosition(JSONObject options, final CallbackContext callback) {
    Log.i(TAG, "请求当前地理位置");
    Context ctx = cordova.getActivity().getApplicationContext();
    PositionOptions positionOpts = new PositionOptions(options);
    CDVLocationListener listener = new CDVLocationListener(ctx);

    return listener.getCurrentPosition(positionOpts, new CDVPositionListener() {
      @Override
      public void onReceivePosition(JSONArray message) {
        callback.send(message);
      }
    });
  }

}

@@ -638,85 +638,31 @@
 ate 
-Map%3Cint, CDVLocationListener%3E watches = new HashMap%3Cint, CDVLocationListener%3E
+Store store = new Store
 ();%0A
@@ -1046,32 +1046,68 @@
 tJSONObject(0);%0A
+      int watchId = args.getInt(1);%0A
       return wat
@@ -1406,36 +1406,134 @@
 %E5%81%9C%E6%AD%A2%E7%9B%91%E5%90%AC%22);%0A    
-return false
+BDGeolocation geolocation = store.get(watchId);%0A    store.remove(watchId);%0A    return geolocation.clearWatch()
 ;%0A  %7D%0A%0A  pri
@@ -1578,16 +1578,29 @@
 options,
+ int watchId,
  Callbac
@@ -1784,66 +1784,94 @@
     
-CDVL
+BDGeol
 ocation
-Listener listener
+ geolocation
  = new 
-CDVL
+BDGeol
 ocation
-Listener(ctx
+(ctx);%0A    store.put(geolocation, watchId
 );%0A 
@@ -1880,24 +1880,27 @@
  return 
-listener
+geolocation
 .watchPo
@@ -2365,70 +2365,60 @@
     
-CDVL
+BDGeol
 ocation
-Listener listener
+ geolocation
  = new 
-CDVL
+BDGeol
 ocation
-Listener
 (ctx);%0A
-%0A
     
@@ -2428,16 +2428,19 @@
 urn 
-listener
+geolocation
 .get

implement watch