package com.appstrax.cordova.plugin;

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

import android.content.Context;
import java.util.concurrent.ExecutorService;

import org.json.JSONArray;

import com.appstrax.cordova.plugin.UsbService;

public class RfidScannerPlugin extends CordovaPlugin {
    UsbService mUsbService;
    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) {

        if (action.equals("scan")) {
            startScanning(callbackContext);
            return true;
        } else if(action.equals("stop")) {
            stopScanning();
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
            callbackContext.sendPluginResult(pluginResult);
            return true;
        } else {
            callbackContext.error("\"" + action + "\" is not a recognized action.");
            return false;
        }
    }

    public void startScanning(CallbackContext callbackContext) {
        if (mUsbService == null) {
            mUsbService = new UsbService(cordova.getContext(), cordova.getThreadPool());
        }
        mUsbService.connectAndScan(callbackContext);
    }

    public void stopScanning() {
        if (mUsbService != null) {
            mUsbService.stopScanning();
        }
    }
}