package cn.linker.cordova;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

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

import cn.rongcloud.im.utils.SharedPreferencesContext;

/**
 * This class echoes a string called from JavaScript.
 */
public class CordovaLinker extends CordovaPlugin {
    public static  SharedPreferences SharedPrefs;
    private String TAG="CordovaLinker";

    @Override
    protected void pluginInitialize() {
        SharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        Log.e(TAG, "execute: "+ action);
        if (action.equals("goMain")) {
            this.goMain(args, callbackContext);
            return true;
        }else if (action.equals("exit")) {
            this.appExit(args,callbackContext);
            return true;
        }else if (action.equals("callMap")){
            this.callMap(args,callbackContext);
            return true;
        }else if (action.equals("startChat")){
            this.startChat(args,callbackContext);
            return true;
        }else if(action.equals("goBack")){
            this.goBack(args,callbackContext);
            return true;
        }else if (action.equals("deviceready")) {
            deviceReadyListener.deviceReady();
        }
        return false;
    }

    private void goBack(JSONArray message, CallbackContext callbackContext)  throws JSONException{
        if (message != null && message.length() > 0) {
            if(mLoginListener!=null){
                mLoginListener.notify("goBack",message,callbackContext);
            }
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }

    private void startChat(JSONArray message, CallbackContext callbackContext) throws JSONException{
        if (message != null && message.length() > 0) {
            if(mLoginListener!=null){
                mLoginListener.notify("startChat",message,callbackContext);
            }
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }

    private void appExit(JSONArray message, CallbackContext callbackContext)throws JSONException{
        if (message != null && message.length() > 0) {
            if(mLoginListener!=null){
                mLoginListener.notify("exit",message,callbackContext);
            }
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }

    private void goMain(JSONArray message, CallbackContext callbackContext)throws JSONException {
        if (message != null && message.length() > 0) {
            if(mLoginListener!=null){
                mLoginListener.notify("login_Success",message,callbackContext);
            }
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }

    private void callMap(JSONArray message,final CallbackContext callbackContext)throws JSONException {
        if (message != null && message.length() > 0) {
            if(mLoginListener!=null){
                mLoginListener.notify("callMap",message,callbackContext);
            }
        } else {
            callbackContext.error("Call map error.");
        }
    }

    public static LoginListener mLoginListener;
    public static DeviceReadyListener deviceReadyListener;

    public static void setLoginListener(LoginListener mListener){
        mLoginListener = mListener;
    }

    public static void setDeviceReadyListener(DeviceReadyListener mDeviceReadyListener){
        deviceReadyListener = mDeviceReadyListener;
    }

    public interface LoginListener{
        void notify(String flag,JSONArray message, CallbackContext callbackContext) throws JSONException;
    }

    public interface DeviceReadyListener{
        void deviceReady();
    }
}
