package com.mantra;

import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import com.mantra.mfs100.FingerData;
import com.mantra.mfs100.MFS100;
import com.mantra.mfs100.MFS100Event;

import java.io.File;
import java.io.FileOutputStream;

public class MantraModule extends ReactContextBaseJavaModule implements MFS100Event {

    MFS100 mfs100 = null;
    private FingerData lastCapFingerData = null;
    private boolean isCaptureRunning = false;
    int timeout = 10000;

    private enum ScannerAction {
        Capture,
        Verify
    }

    ScannerAction scannerAction = ScannerAction.Capture;
    byte[] Enroll_Template;
    byte[] Verify_Template;

    public MantraModule(ReactApplicationContext reactContext) {
        super(reactContext);
        if (mfs100 == null) {
            mfs100 = new MFS100(this);
        }
    }

    @Override
    public String getName() {
        return "MantraModule";
    }

    @ReactMethod
    private void InitScanner() {
        try {
            int ret = mfs100.Init();
            if (ret != 0) {
            SetTextOnUIThread(mfs100.GetErrorMsg(ret));
            } else {
                String info = "Serial: " + mfs100.GetDeviceInfo().SerialNo() +
                    " Make: " + mfs100.GetDeviceInfo().Make() +
                    " Model: " + mfs100.GetDeviceInfo().Model() +
                    "\nCertificate: " + mfs100.GetCertification();
            SetTextOnUIThread(info);
            }
        } catch (Exception ex) {
            SetTextOnUIThread("error");
        }
    }

    @Override
    public void OnDeviceAttached(int vid, int pid, boolean hasPermission) {
        int ret;
        if (!hasPermission) {
            return;
        }
        if (vid == 1204 || vid == 11279) {
            if (pid == 34323) {
                ret = mfs100.LoadFirmware();
                if (ret != 0) {} else {}
            } else if (pid == 4101) {
                String key = "Without Key";
                ret = mfs100.Init();
                if (ret == 0) {
                    showSuccessLog(key);
                } else {}

            }
        }
    }

    @Override
    public void OnHostCheckFailed(String err) {
        try {

        } catch (Exception ignored) {}
    }

    @Override
    public void OnDeviceDetached() {
        UnInitScanner();
    }

    private void showSuccessLog(String key) {
        String info = "\nKey: " + key + "\nSerial: " +
            mfs100.GetDeviceInfo().SerialNo() + " Make: " +
            mfs100.GetDeviceInfo().Make() + " Model: " +
            mfs100.GetDeviceInfo().Model() +
            "\nCertificate: " + mfs100.GetCertification();
    }

    private void UnInitScanner() {
        try {
            int ret = mfs100.UnInit();
            if (ret != 0) {

            } else {
                lastCapFingerData = null;
            }
        } catch (Exception e) {}
    }
    private void SetTextOnUIThread(final String str) {
                Toast.makeText(getReactApplicationContext(), str, Toast.LENGTH_LONG).show();
    }

    @ReactMethod
    private void StartSyncCapture() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                isCaptureRunning = true;
                try {
                    FingerData fingerData = new FingerData();
                    int ret = mfs100.AutoCapture(fingerData, timeout, true);
                    if (ret != 0) {
                        SetTextOnUIThread("new thread running");
                    } else {
                        lastCapFingerData = fingerData;
                        final Bitmap bitmap = BitmapFactory.decodeByteArray(fingerData.FingerImage(), 0,
                            fingerData.FingerImage().length);
                        String log = "\nQuality: " + fingerData.Quality() +
                            "\nNFIQ: " + fingerData.Nfiq() +
                            "\nWSQ Compress Ratio: " +
                            fingerData.WSQCompressRatio() +
                            "\nImage Dimensions (inch): " +
                            fingerData.InWidth() + "\" X " +
                            fingerData.InHeight() + "\"" +
                            "\nImage Area (inch): " + fingerData.InArea() +
                            "\"" + "\nResolution (dpi/ppi): " +
                            fingerData.Resolution() + "\nGray Scale: " +
                            fingerData.GrayScale() + "\nBits Per Pixal: " +
                            fingerData.Bpp() + "\nWSQ Info: " +
                            fingerData.WSQInfo();
                        SetData2(fingerData);
                    }
                } catch (Exception ex) {
                } finally {
                    isCaptureRunning = false;
                }
            }
        }).start();
    }

    public void SetData2(FingerData fingerData) {
        if (scannerAction.equals(ScannerAction.Capture)) {
            Enroll_Template = new byte[fingerData.ISOTemplate().length];
            System.arraycopy(fingerData.ISOTemplate(), 0, Enroll_Template, 0,
                fingerData.ISOTemplate().length);
        } else if (scannerAction.equals(ScannerAction.Verify)) {
            Verify_Template = new byte[fingerData.ISOTemplate().length];
            System.arraycopy(fingerData.ISOTemplate(), 0, Verify_Template, 0,
                fingerData.ISOTemplate().length);
            int ret = mfs100.MatchISO(Enroll_Template, Verify_Template);
            if (ret < 0) {

            } else {
                if (ret >= 1400) {} else {}
            }
        }


    }


}