package com.example.chemical.ui.verify;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.chemical.databinding.ActivityDoubleVerifyBinding;
import com.example.chemical.ui.common.BaseCountDownActivity;
import com.example.chemical.ui.verify.fragment.DetectType;
import com.example.chemical.ui.verify.fragment.FaceDetectFragment;

import java.util.ArrayList;
import java.util.List;

public class DoubleVerifyActivity extends BaseCountDownActivity<ActivityDoubleVerifyBinding> {
    private ActivityDoubleVerifyBinding binding;
    private FragmentManager fragmentManager;

    private final List<Boolean> doubleVerifyList = new ArrayList<Boolean>() {{
        add(false);
        add(false);
    }};

    @NonNull
    @Override
    protected ActivityDoubleVerifyBinding createViewBinding() {
        return binding = ActivityDoubleVerifyBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initViews(@Nullable Bundle savedInstanceState) {
        super.initViews(savedInstanceState);
    }

    @Override
    protected void initData() {
        super.initData();
        fragmentManager = getSupportFragmentManager();
        FragmentUtils.add(fragmentManager, FaceDetectFragment.getInstance(), binding.fragment1FL.getId());
        binding.card1TV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        binding.face1TV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verify1(DetectType.FACE_DETECT);
            }
        });
        binding.scan1TV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        binding.card2TV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        binding.face2TV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verify2(DetectType.FACE_DETECT);
            }
        });

        binding.scan2TV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FaceDetectFragment.getInstance().startFaceDetect();
            }
        });

//        FaceDetectFragment.getInstance().setActivityListener(new DoubleVerifyListener() {
//            @Override
//            public void detect(DetectType type, Bitmap faceBitmap, boolean isVerified) {
//                LogUtils.d(type, isVerified);
//                switch (type) {
//                    case FACE_DETECT:
//                        if (isVerified) {
//                            // 是否第一个已经验证通过
//                            if (doubleVerifyList.get(0)) {
//                                ToastUtils.showLong("双人认证成功");
//                                FaceDetectFragment.getInstance().stopFaceDetect();
//                                FragmentUtils.removeAll(fragmentManager);
//                                binding.fragment2FL.setBackground(ImageUtils.bitmap2Drawable(faceBitmap));
//                            } else {
//                                ToastUtils.showLong("第一个人认证成功");
//                                doubleVerifyList.set(0, true);
//                                verify2(type);
//                                binding.fragment1FL.setBackground(ImageUtils.bitmap2Drawable(faceBitmap));
//                            }
//                        }
//                        break;
//                    case SCAN_DETECT:
//                        break;
//                    case CARD_DETECT:
//                        break;
//                }
//            }
//        });
    }

    private void verify1(DetectType faceDetect) {
        switch (faceDetect) {
            case FACE_DETECT:
                FaceDetectFragment.getInstance().stopFaceDetect();
                FragmentUtils.removeAll(fragmentManager);
                FragmentUtils.add(fragmentManager, FaceDetectFragment.getInstance(), binding.fragment1FL.getId());
                FaceDetectFragment.getInstance().startFaceDetect();
                break;
        }
    }

    private void verify2(DetectType faceDetect) {
        switch (faceDetect) {
            case FACE_DETECT:
                FaceDetectFragment.getInstance().stopFaceDetect();
                FragmentUtils.removeAll(fragmentManager);
                FragmentUtils.add(fragmentManager, FaceDetectFragment.getInstance(), binding.fragment2FL.getId());
                FaceDetectFragment.getInstance().startFaceDetect();
                break;
        }
    }

    public interface DoubleVerifyListener {
        void detect(DetectType type, Bitmap faceBitmap, boolean isVerified);
    }
}