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.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

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

import com.rscja.utility.StringUtility;


public class CollectionFragment extends Fragment {

    private MainActivity mActivity;

    private EditText etPageId;
    private Button btnCollect;
    private TextView tvMsg;
    private int pageID;

    private static final int GET_IMAGE_FAIL = -1;
    private static final int STORE_BUFFER_FAIL = -2;
    private static final int STORE_FLASH_FAIL = -3;
    private static final int COLLECT_SUCC = 1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mActivity.mProgressDialog.dismiss();
            switch (msg.what) {
                case COLLECT_SUCC:
                    tvMsg.setText("Collection success");
                    tvMsg.setTextColor(ActivityCompat.getColor(getContext(), R.color.green_0C7910));
                    showToast("Collection success");
                    break;
                case GET_IMAGE_FAIL:
                    tvMsg.setText("Collection failure\n\nfailure to capture fingerprint image");
                    tvMsg.setTextColor(Color.RED);
                    showToast("Collection failure\n\nfailure to capture fingerprint image");
                    break;
                case STORE_BUFFER_FAIL:
                    tvMsg.setText("Collection failure\n\nfailed to store to buffer");
                    tvMsg.setTextColor(Color.RED);
                    showToast("Collection failure\n\nfailed to store to buffer");
                    break;
                case STORE_FLASH_FAIL:
                    tvMsg.setText("Collection failure\n\nfailed to store to flash");
                    tvMsg.setTextColor(Color.RED);
                    showToast("Collection failure\n\nfailed to store to flash");
                    break;
            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_collection, container, false);
        init(view);
        return view;
    }

    private void init(View view) {
        etPageId = view.findViewById(R.id.etPageId);
        btnCollect = view.findViewById(R.id.btnCollect);
        btnCollect.setOnClickListener(collectListener);
        tvMsg = view.findViewById(R.id.tvMsg);
    }

    @Override
    public void onDestroyView() {
        mHandler.removeCallbacksAndMessages(null);
        super.onDestroyView();
    }

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

    private View.OnClickListener collectListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            collection();
        }
    };

    boolean isRuning=false;
    private void collection() {
        String pageIdStr = etPageId.getText().toString();
        if(!mActivity.checkPageID(pageIdStr)) {
            return;
        }

        pageID = StringUtility.string2Int(pageIdStr, -1);
        if (pageID < 0 || pageID > 127) {
            showToast("Page ID is error, from 0 to 127");
            return;
        }

        tvMsg.setText("Start collecting...");
        mActivity.mProgressDialog.setMessage("Start collecting...");
        mActivity.mProgressDialog.show();
        mActivity.mProgressDialog.setCancelable(false);
        isRuning=true;
        mActivity.mExecutorService.execute(new Runnable() {
            @Override
            public void run() {
                while (isRuning) {
                    if (mActivity.mFingerprintWithTLK1NC.fingerGETImage() > 0) {
                        if (mActivity.mFingerprintWithTLK1NC.fingerStoreCharToFlashPage(pageID)) {
                            mHandler.sendEmptyMessage(COLLECT_SUCC);
                        } else {
                            mHandler.sendEmptyMessage(STORE_FLASH_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();
    }
}
