package com.jianzhi_inc.dandelion.bdpush;

import android.util.Log;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * Baidu Push Message Cordova Plugin
 * Created by Sam on 9/19/15.
 */
public class BDPush extends CordovaPlugin {

    public final static String ACTION_BIND = "bind";
    public final static String ACTION_UNBIND = "unbind";


    //public final static String CALLBACK_BIND_SUCCESS = "bind_success";

    private static String apiKey;
    public static CallbackContext currentCallbackContext;

    /** TAG to Log */
    public static final String TAG = BDPush.class.getSimpleName();


    @Override
    protected void pluginInitialize() {
        super.pluginInitialize();
        if(null == apiKey) {
            apiKey = preferences.getString("BD_PUSH_API_KEY", "");
        }
    }

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

        Log.i(TAG, action + " is called, param [" + args.toString() + "]");

        if(ACTION_BIND.equals(action)) {
            return bindBDPush(args, callbackContext);
        }
        if(ACTION_UNBIND.equals(action)) {
            return unbindBDPush(args, callbackContext);
        }
        return super.execute(action, args, callbackContext);
    }


    private boolean bindBDPush(JSONArray args, CallbackContext callbackContext) {
        cordova.getThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                PushManager.startWork(webView.getContext(), PushConstants.LOGIN_TYPE_API_KEY, apiKey);
                currentCallbackContext = callbackContext;
            }
        });
        return true;
    }

    private boolean unbindBDPush(JSONArray args, CallbackContext callbackContext) {
        cordova.getThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                if(PushManager.isConnected(webView.getContext())) {
                    PushManager.stopWork(webView.getContext());
                    currentCallbackContext = callbackContext;
                }
            }
        });
        return true;
    }
}

@@ -668,23 +668,16 @@
 private 
-static 
 String a
@@ -683,16 +683,16 @@
 apiKey;%0A
+
     publ
@@ -940,16 +940,21 @@
 null == 
+this.
 apiKey) 
@@ -967,16 +967,21 @@
         
+this.
 apiKey =
@@ -1615,32 +1615,34 @@
 text) %7B%0A        
+//
 cordova.getThrea
@@ -1674,32 +1674,34 @@
 ble() %7B%0A        
+//
     @Override%0A  
@@ -1698,32 +1698,34 @@
 verride%0A        
+//
     public void 
@@ -1826,16 +1826,21 @@
 PI_KEY, 
+this.
 apiKey);
@@ -1898,32 +1898,34 @@
 ontext;%0A        
+//
     %7D%0A        %7D)
@@ -1914,32 +1914,34 @@
 //    %7D%0A        
+//
 %7D);%0A        retu
@@ -2048,16 +2048,18 @@
         
+//
 cordova.
@@ -2103,24 +2103,26 @@
 ) %7B%0A        
+//
     @Overrid
@@ -2127,24 +2127,26 @@
 ide%0A        
+//
     public v
@@ -2369,32 +2369,34 @@
       %7D%0A        
+//
     %7D%0A        %7D)
@@ -2393,16 +2393,18 @@
         
+//
 %7D);%0A    

fix unbind method