package com.cocodin.barcodescan.plugin;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.rscja.deviceapi.Barcode2D;
import com.rscja.deviceapi.exception.ConfigurationException;
import com.rscja.utility.StringUtility;
import com.zebra.adc.decoder.Barcode2DWithSoft;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class PluginHelper {

    private static final String TAG = "PluginHelper";
    private static boolean threadStop = true;
    private static String init_barcode;
    private static ExecutorService executor;
    private static Barcode2DWithSoft mInstance;

    public static void scan(CordovaInterface cordova, CordovaWebView webView, JSONArray args,
            final CallbackContext callbackContext) {

        Context context = null;

        try {
            context = cordova.getActivity();
            mInstance = Barcode2DWithSoft.getInstance();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            return;
        }

        mInstance.setScanCallback(new Barcode2DWithSoft.ScanCallback() {
            @Override
            public void onScanComplete(int i, int length, byte[] data) {
                Log.i(TAG, "onScanComplete() i=" + i);

                if (length < 1) {

                    return;
                }

                String barCode = new String(data);
                Log.i(TAG, barCode);

                mInstance.stopScan();
                mInstance.close();

                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, barCode));
            }
        });

        mInstance.open(context);

        boolean bContinuous = false;
        Log.i("ErDActivity", "scan() ");

        executor = cordova.getThreadPool();

        if (threadStop) {

            int iBetween = 0;

            // thread = new Thread(new GetBarcode(bContinuous, iBetween));
            // thread.start();

            executor.execute(new GetBarcode(bContinuous, iBetween));

        } else {
            threadStop = true;
        }
    }

    private static class GetBarcode implements Runnable {

        private boolean isContinuous = false;
        String barCode = "";
        private long sleepTime = 1000;
        Message msg = null;

        public GetBarcode(boolean isContinuous) {
            this.isContinuous = isContinuous;
        }

        public GetBarcode(boolean isContinuous, int sleep) {
            this.isContinuous = isContinuous;
            this.sleepTime = sleep;
        }

        @Override
        public void run() {

            do {
                mInstance.scan();

                Log.i("MY", "barCode " + barCode.trim());

                msg = new Message();

                if (StringUtility.isEmpty(barCode)) {
                    msg.arg1 = 0;
                    msg.obj = "";

                } else {

                    msg.arg1 = 1;

                    msg.obj = barCode;
                }

                if (isContinuous) {
                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            } while (isContinuous && !threadStop);

        }

    }
}
