package com.joangape.myfirstplugin;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * This class echoes a string called from JavaScript.
 */
public class MyFirstPlugin extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("connect")) {
            String message = args.getString(0);
            this.connect(args, callbackContext);
            return true;
        }
        return false;
    }

    private void connect(JSONArray jsonArray, CallbackContext callbackContext) {
        /*if (message != null && message.length() > 0) {
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }*/
		 if(args != null){
            try{
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                String ssid = jsonObject.getString("ssid");
                String password = jsonObject.getString("password");
                if(ssid == null || "".equals(ssid)){
                    callbackContext.error("Error: SSID empty");
                }
                if(password == null || "".equals(password)){
                    callbackContext.error("Error: Password empty");
                }

                callbackContext.success("Data Valid");
            }catch(Exception e){
                callbackContext.error("Error:"+e);
            }
        }else{
            callbackContext.error("Cannot pass null value");
        }
    }
}
