package com.cocodin.barcodescan.plugin.devices;

import android.util.Log;
import android.widget.Toast;

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

import com.cocodin.barcodescan.plugin.BaseScan;
import com.honeywell.aidc.AidcManager;
import com.honeywell.aidc.AidcManager.CreatedCallback;
import com.honeywell.aidc.BarcodeFailureEvent;
import com.honeywell.aidc.BarcodeReadEvent;
import com.honeywell.aidc.BarcodeReader;
import com.honeywell.aidc.BarcodeReader.BarcodeListener;
import com.honeywell.aidc.ScannerUnavailableException;
import com.honeywell.aidc.UnsupportedPropertyException;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by alberto.doval on 22/10/17.
 */

public class EDA50K extends BaseScan implements BarcodeListener {

    private static String TAG = EDA50K.class.getName();

    private BarcodeReader barcodeReader;
    private AidcManager manager;

    private CallbackContext currentCallbackContext = null;

    @Override
    public String getDeviceName() {
        return TAG;
    }

    @Override
    public void scan(final CordovaInterface cordova, CordovaWebView webView, JSONArray args, final CallbackContext callbackContext) {
        if (manager == null) {
            AidcManager.create(cordova.getActivity(), new CreatedCallback() {

                @Override
                public void onCreated(AidcManager aidcManager) {
                    manager = aidcManager;
                    barcodeReader = manager.createBarcodeReader("dcs.scanner.ring");

                    if (barcodeReader != null) {

                        barcodeReader.addBarcodeListener(EDA50K.this);

                        // Map<String, Object> asdfsadf =
                        // barcodeReader.getAllDefaultProperties();
                        // for (String key : asdfsadf.keySet()) {
                        // Log.d("chromium", key + "=" + asdfsadf.get(key));
                        // }

                        // set the trigger mode to auto control
                        try {
                            barcodeReader.setProperty(BarcodeReader.PROPERTY_TRIGGER_CONTROL_MODE, BarcodeReader.TRIGGER_CONTROL_MODE_AUTO_CONTROL);
                        } catch (UnsupportedPropertyException e) {
                            Toast.makeText(cordova.getActivity(), "Failed to apply properties", Toast.LENGTH_SHORT).show();
                        }

                        Map<String, Object> properties = new HashMap<String, Object>();
                        // Set Symbologies On/Off
                        properties.put(BarcodeReader.PROPERTY_CODE_128_ENABLED, true);
                        properties.put(BarcodeReader.PROPERTY_GS1_128_ENABLED, true);
                        properties.put(BarcodeReader.PROPERTY_QR_CODE_ENABLED, true);
                        properties.put(BarcodeReader.PROPERTY_CODE_39_ENABLED, true);
                        properties.put(BarcodeReader.PROPERTY_DATAMATRIX_ENABLED, true);
                        properties.put(BarcodeReader.PROPERTY_UPC_A_ENABLE, true);
                        properties.put(BarcodeReader.PROPERTY_EAN_13_ENABLED, true);
                        properties.put(BarcodeReader.PROPERTY_AZTEC_ENABLED, false);
                        properties.put(BarcodeReader.PROPERTY_CODABAR_ENABLED, false);
                        properties.put(BarcodeReader.PROPERTY_INTERLEAVED_25_ENABLED, false);
                        properties.put(BarcodeReader.PROPERTY_PDF_417_ENABLED, false);
                        // Set Max Code 39 barcode length
                        properties.put(BarcodeReader.PROPERTY_CODE_39_MAXIMUM_LENGTH, 48);
                        // Turn on center decoding
                        properties.put(BarcodeReader.PROPERTY_CENTER_DECODE, true);
                        // Disable bad read response, handle in onFailureEvent
                        properties.put(BarcodeReader.PROPERTY_NOTIFICATION_BAD_READ_ENABLED, false);

                        properties.put(BarcodeReader.PROPERTY_DATA_PROCESSOR_LAUNCH_BROWSER, false);

                        // Apply the settings
                        barcodeReader.setProperties(properties);

                        //Perform first read after initialization
                        readBarcode(callbackContext);

                    } else {
                        callbackContext.error("Failed to open barcode reader");
                    }
                }
            });
        }
        else {
            readBarcode(callbackContext);
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {

    }

    private void readBarcode(CallbackContext callbackContext) {
        if (barcodeReader != null) {
            try {
                barcodeReader.claim();

                callbackContext.success();
            } catch (ScannerUnavailableException e) {
                e.printStackTrace();
                callbackContext.error("Unable to claim reader");
            }
        } else {
            callbackContext.error("Reader not open");
        }
    }

    @Override
    public void onDestroy() {
        if (barcodeReader != null) {
            // close BarcodeReader to clean up resources.
            barcodeReader.close();
            barcodeReader = null;
        }

        if (manager != null) {
            // close AidcManager to disconnect from the scanner service.
            // once closed, the object can no longer be used.
            manager.close();
        }
    }

    @Override
    public void onBarcodeEvent(BarcodeReadEvent barcodeReadEvent) {
        Log.d(TAG, barcodeReadEvent.getBarcodeData());

        if (currentCallbackContext != null) {
            try {
                JSONObject obj = new JSONObject();
                obj.put("success", true);
                obj.put("data", barcodeReadEvent.getBarcodeData());

                PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
                result.setKeepCallback(true);
                currentCallbackContext.sendPluginResult(result);
            } catch (Exception x) {
                Log.e(TAG, x.getMessage());
            }
        }
    }

    @Override
    public void onFailureEvent(BarcodeFailureEvent barcodeFailureEvent) {
        if (currentCallbackContext != null) {
            try {
                JSONObject obj = new JSONObject();
                obj.put("success", false);

                PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
                result.setKeepCallback(true);
                currentCallbackContext.sendPluginResult(result);
            } catch (Exception x) {
                Log.e(TAG, x.getMessage());
            }
        }
    }
}
