package com.example.fingerprint_tlk1_uart_demo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;


import com.rscja.deviceapi.FingerprintWithFIPS;
import com.rscja.deviceapi.FingerprintWithTLK1NC;
import com.rscja.utility.StringUtility;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class ImageFragment extends Fragment implements View.OnClickListener{
    String TAG="ImageFragment";
    WaitDialog waitDialog=null;
    MainActivity  mContext;
    Button btnStart,Stop;
    ImageView iv;
    TextView tvInfo;
    TextView tvPro;
    ScrollView scroll;
    String oldMsg="";
    Handler handler=new Handler();
    Callback callback=new Callback();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_grab, container, false);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = (MainActivity) getActivity();
        btnStart=(Button)mContext.findViewById(R.id.btnStart);
        Stop =(Button) mContext.findViewById(R.id.Stop);
        btnStart.setOnClickListener(this);
        Stop.setVisibility(View.GONE);
        iv= (ImageView)mContext.findViewById(R.id.iv);
        tvInfo=(TextView)mContext.findViewById(R.id.tvInfo);
        tvPro=(TextView)mContext.findViewById(R.id.tvPro);
        scroll=(ScrollView)mContext.findViewById(R.id.scroll);
        waitDialog=new WaitDialog(getActivity());
    }
    @Override
    public void onPause() {
        super.onPause();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btnStart:
                waitDialog.show("...");
                mContext.mFingerprintWithTLK1NC.startUPImage(callback);
                btnStart.setEnabled(false);
                break;

        }
    }
    public void scrollToBottom() {
        handler.post(new Runnable() {
            @Override
            public void run() {
                scroll.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
    }

    class Callback implements FingerprintWithTLK1NC.IUPImageCallback{
        @Override
        public void messageInfo(String s) {
            if(!oldMsg.equals(s)) {
                String str1=tvInfo.getText().toString();
                String strMsg=str1+s+".\r\n";
                tvInfo.setText(strMsg);
                oldMsg=s;
                scrollToBottom();
                waitDialog.setMsg(s);
            }
        }

        @Override
        public void onComplete(boolean result, byte[] bytes,int failuerCode) {
            Log.i(TAG, "failuerCode=" + failuerCode + "  result=" + result);
            if (result) {
                //--------------------bmp-------------------------------------------------
                String path="sdcard/fingerprint.bmp";
                if  (mContext.mFingerprintWithTLK1NC.generateImg(bytes, path))
                {
                    Bitmap bitmap = BitmapFactory.decodeFile(path);
                    if (bitmap != null) {

                        iv.setImageBitmap(bitmap);
                    }
                }



                //--------------------wsq-------------------------------------------------
                String wsqPath="sdcard/fingerprint.wsq";
                Wsq wsq=new Wsq(mContext);
                wsq.compressWSQ(bytes,wsqPath);

                //-------------------------------------------
            } else {
               //fail
            }
            waitDialog.hide();
            btnStart.setEnabled(true);
        }

        @Override
        public void progress(int i) {
            tvPro.setText(i+"%");
        }


    }



}
