package com.vessel.abplugin;

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

import android.app.Activity;
import android.util.Log;
import android.content.Intent;

import com.vessel.VesselSDK;
import com.vessel.VesselAB;
import com.vessel.enums.VesselEnums.TestVariation;
import com.vessel.interfaces.ABListener;


public class VesselABPlugin extends CordovaPlugin {
    CallbackContext callbackContext;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext _callbackContext) throws JSONException {
        try {
            callbackContext = _callbackContext;
            Log.d("VesselSDK", "Action "+action);

            if ("initialize".equals(action)) {
                String SECRET_KEY = args.getString(0);

                VesselSDK.initialize(cordova.getActivity().getApplicationContext(), SECRET_KEY);
                callbackContext.success();
                return true;

            }else if ("reloadTest".equals(action)) {
                VesselAB.reloadTest();
                callbackContext.success();
                return true;
            } else if ("pause".equals(action)) {
                VesselAB.onPause();
                callbackContext.success();
                return true;
            } else if ("resume".equals(action)) {
                VesselAB.onResume();
                callbackContext.success();
                return true;
            } else if ("startSession".equals(action)) {
                String sessionName = args.getString(0);
                VesselAB.startSession(sessionName);
                callbackContext.success();
                return true;
            } else if ("endSession".equals(action)) {
                String sessionName = args.getString(0);
                VesselAB.endSession(sessionName);
                callbackContext.success();
                return true;
            }else if ("checkPointVisited".equals(action)) {
                String checkpointName = args.getString(0);
                VesselAB.checkPointVisited(checkpointName);
                callbackContext.success();
                return true;
            }else if ("setABListener".equals(action)) {
                this.cordova.setActivityResultCallback(this);
                this.cordova.getThreadPool().execute(new Runnable() {

                    @Override
                    public void run() {
                        VesselAB.setABListener(new ABListener() {

                            @Override
                            public void testNotAvailable(TestVariation arg0) {
                                callbackContext.success(arg0.toString());
                            }

                            @Override
                            public void testLoading() {
                            }

                            @Override
                            public void testLoaded(String testName, TestVariation testVariation) {
                                callbackContext.success(testVariation.toString());

                            }
                        });

                    }
                });
                return true;
            }else if("getValue".equals(action)){
                String key = args.getString(0);
                String defaultValue = args.getString(1);
                String value = VesselAB.getValue(key, defaultValue);
                callbackContext.success(value);
                return true;
            }
            return false;
        } catch(Exception e) {
            e.printStackTrace();
            callbackContext.error(e.getMessage());
            return false;
        } 
    }
}
@@ -111,36 +111,8 @@
 in;%0A
-import org.json.JSONObject;%0A
 impo
@@ -185,76 +185,16 @@
 oid.
-app.Activity;%0Aimport android.util.Log;%0Aimport android.content.Intent
+util.Log
 ;%0A%0Ai
@@ -734,24 +734,30 @@
             
+final 
 String SECRE
@@ -780,24 +780,173 @@
 String(0);%0A%0A
+                this.cordova.getThreadPool().execute(new Runnable() %7B%0A%0A                    @Override%0A                    public void run() %7B%0A        
             
@@ -1038,32 +1038,40 @@
                 
+        
 callbackContext.
@@ -1073,32 +1073,74 @@
 text.success();%0A
+                    %7D%0A                %7D);%0A
                 
@@ -1152,17 +1152,16 @@
 n true;%0A
-%0A
         
@@ -2626,32 +2626,48 @@
                 
+                
 VesselAB.setABLi
@@ -2788,16 +2788,22 @@
 ailable(
+final 
 TestVari
@@ -2808,20 +2808,29 @@
 riation 
-arg0
+testVariation
 ) %7B%0A    
@@ -2885,12 +2885,21 @@
 ess(
-arg0
+testVariation
 .toS
@@ -3169,16 +3169,22 @@
 estName,
+ final
  TestVar
@@ -3290,17 +3290,16 @@
 ing());%0A
-%0A
         

Fix for phonegap 3.0