package com.example.fingerprint_tlk1_uart_demo;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.rscja.deviceapi.FingerprintWithTLK1NC;

public class IdentificationFragment extends Fragment {

    private String TAG = "IdentificationFragment";
    private MainActivity mActivity;

    private TextView tvMsg;
    private Button btnIdentify;
    boolean isRuning=false;
    private static final int GET_IMAGE_FAIL = -1;
    private static final int STORE_BUFFER_FAIL = -2;
    private static final int IDENTIFY_FAIL = -3;
    private static final int IDENTIFY_SUCC = 1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mActivity.mProgressDialog.dismiss();
            switch (msg.what) {
                case IDENTIFY_SUCC:
                    int pageId = (int) msg.obj;
                    tvMsg.setText(String.format("Identification success\n\nPageID:%d", pageId));
                    tvMsg.setTextColor(ActivityCompat.getColor(getContext(), R.color.green_0C7910));
                    showToast("Identification success");
                    break;
                case GET_IMAGE_FAIL:
                    tvMsg.setText("Identification failure\n\nfailure to capture fingerprint image");
                    tvMsg.setTextColor(Color.RED);
                    showToast("Identification failure\n\nfailure to capture fingerprint image");
                    break;
                case STORE_BUFFER_FAIL:
                    tvMsg.setText("Identification failure\n\nfailed to store to buffer");
                    tvMsg.setTextColor(Color.RED);
                    showToast("Identification failure\n\nfailed to store to buffer");
                    break;
                case IDENTIFY_FAIL:
                    tvMsg.setText("Identification failure");
                    tvMsg.setTextColor(Color.RED);
                    showToast("Identification failure");
                    break;
            }
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View vie= inflater.inflate(R.layout.fragment_identification, container, false);
        init(vie);
        return  vie;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = (MainActivity) getActivity();

    }

    @Override
    public void onDestroyView() {
        Log.e(TAG, "onDestroyView");
        mHandler.removeCallbacksAndMessages(null);
        super.onDestroyView();
    }

    private void init(View view) {
        tvMsg = view.findViewById(R.id.tvMsg);
        btnIdentify = view.findViewById(R.id.btnIdentify);
        btnIdentify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                identify();
            }
        });
    }

    private void identify() {
        isRuning=true;
        mActivity.mProgressDialog.setMessage("Start identifying...");
        mActivity.mProgressDialog.show();
        mActivity.mProgressDialog.setCancelable(false);
        mActivity.mExecutorService.execute(new Runnable() {
            @Override
            public void run() {
                while (isRuning) {
                    if (mActivity.mFingerprintWithTLK1NC.fingerGETImage() > 0) {
                        if (mActivity.mFingerprintWithTLK1NC.fingerStoreCharToBuffer(FingerprintWithTLK1NC.BufferEnum.B0)) {
                            int pageId = mActivity.mFingerprintWithTLK1NC.fingerSearchTemplate(FingerprintWithTLK1NC.BufferEnum.B0, 0, 128);
                            if (pageId >= 0) {
                                Message msg = mHandler.obtainMessage(IDENTIFY_SUCC, pageId);
                                mHandler.sendMessage(msg);
                            } else {
                                mHandler.sendEmptyMessage(IDENTIFY_FAIL);
                            }
                        } else {
                            mHandler.sendEmptyMessage(STORE_BUFFER_FAIL);
                        }
                        isRuning=false;
                        break;
                    } else {
                       // mHandler.sendEmptyMessage(GET_IMAGE_FAIL);
                    }
                }
            }
        });
    }
    public void showToast(String text) {
        Toast mToast = Toast.makeText(getContext(), text, Toast.LENGTH_SHORT);
        mToast.show();
    }
}
