package com.vntel.cccd;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.htc.sdk.model.MRZInfo;
import com.htc.sdk.model.MRZResult;
import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraOptions;
import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.frame.FrameProcessor;
import com.otaliastudios.cameraview.markers.DefaultAutoFocusMarker;
import com.otaliastudios.cameraview.size.Size;
import com.vntel.cccd.processor.MRZFrameProcessor;
import com.vntel.cccd.processor.MRZFrameProcessorDelegate;

public class RNVntelCCCDMRZView extends FrameLayout implements MRZFrameProcessorDelegate {
    private final static String TAG = "RNVntelCCCDMRZView";
    public RNVntelCCCDMRZView(Context context) {
        super(context);
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        mCameraFrameLayout = (FrameLayout) layoutInflater.inflate(R.layout.layout_mrz_view, null, false);
        addView(mCameraFrameLayout, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        setupCamera();
    }

    private CameraView mCameraView;
    private View mFocusView;
    private FrameLayout mCameraFrameLayout;
    private MRZFrameProcessor mFrameProcessor;
    private RNVntelCCCDMRZViewDelegate mDelegate;
    private void setupCamera() {
        if (mCameraView == null) {
            mCameraView = (CameraView) mCameraFrameLayout.findViewById(R.id.cameraView);
            mFocusView = (View)mCameraFrameLayout.findViewById(R.id.focusView);

            mCameraView.setAutoFocusMarker(new DefaultAutoFocusMarker());
            mCameraView.addCameraListener(new CameraListener() {
                @Override
                public void onCameraOpened(@NonNull CameraOptions options) {

                    int[] cameraOrigin = new int[2];
                    mCameraView.getLocationOnScreen(cameraOrigin);
                    Rect cameraFrame = new Rect(cameraOrigin[0], cameraOrigin[1], cameraOrigin[0] + mCameraView.getMeasuredWidth(), cameraOrigin[1] + mCameraView.getMeasuredHeight());

                    int[] focusOrigin = new int[2];
                    mFocusView.getLocationOnScreen(focusOrigin);
                    Rect focusFrame = new Rect(focusOrigin[0], focusOrigin[1], focusOrigin[0] + mFocusView.getMeasuredWidth(), focusOrigin[1] + mFocusView.getMeasuredHeight());
                    mFrameProcessor = new MRZFrameProcessor(getContext(),
                            cameraFrame,
                            focusFrame, RNVntelCCCDMRZView.this);
                    mCameraView.addFrameProcessor(mFrameProcessor);
                    mCameraView.startAutoFocus(mCameraView.getWidth() * 0.5f , mCameraView.getHeight() * 0.5f);
                }
            });


        }
    }

    private ReactApplicationContext mReactContext;
    public void setLayout(ReadableArray layout, ReactApplicationContext context) {
        this.mReactContext = context;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (getContext().checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                mCameraView.open();
            }
        }
    }

    @Override
    public void onRecognizedMRZ(MRZResult result, MRZInfo info) {
        Log.d(TAG, "MRZResult " + result.getCccdId());
        if (this.mDelegate != null) {
            this.mDelegate.onRecognizedMRZ(result, info);
        }
        mFrameProcessor.cancel();
        mCameraView.removeFrameProcessor(mFrameProcessor);
        mCameraView.close();
    }

    public void update(RNVntelCCCDMRZViewDelegate delegate) {
        this.mDelegate = delegate;
    }
}
