package com.example.chemical.ui.verify;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.GsonUtils;
import com.example.chemical.databinding.ActivityDoubleVerifyBinding;
import com.example.chemical.receiver.PortScanHelper;
import com.example.chemical.ui.common.BaseCountDownActivity;
import com.example.chemical.ui.plan.PlanAddData;
import com.example.chemical.ui.verify.include.DetectType;
import com.rc.httpcore.bean.UserValidationBean;

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

import kotlin.Triple;

public class DoubleVerifyActivity extends BaseCountDownActivity<ActivityDoubleVerifyBinding> {
    protected ActivityDoubleVerifyBinding binding;
    private DoubleVerifyActivityHelp activityHelp;
    protected PortScanHelper portScanHelper;

    /**
     * 当前识别类型
     */
    protected DetectType detectType = DetectType.FACE_DETECT;

    /**
     * a:是否验证通过
     * b:是否是 全部柜门管理员或安全员或实验室负责人
     * c:通过人的ID
     */
    protected final List<Triple<Boolean, Boolean, Long>> doubleVerifyList = new ArrayList<Triple<Boolean, Boolean, Long>>() {{
        add(new Triple<>(false, false, 0L));
        add(new Triple<>(false, false, 0L));
    }};

    protected PlanAddData planAddData;

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

    @Override
    protected void initViews(@Nullable Bundle savedInstanceState) {
        super.initViews(savedInstanceState);
        activityHelp = new DoubleVerifyActivityHelp(this);
    }

    @Override
    protected void initData() {
        super.initData();

        planAddData = GsonUtils.fromJson(getIntent().getStringExtra("data"), PlanAddData.class);

        binding.card1IB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityHelp.switchShowDetect(detectType = DetectType.CARD_DETECT, true, true);
            }
        });
        binding.face1IB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityHelp.switchShowDetect(detectType = DetectType.FACE_DETECT, true, true);
            }
        });
        binding.scan1IB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityHelp.switchShowDetect(detectType = DetectType.SCAN_DETECT, true, true);
            }
        });
        binding.card2IB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityHelp.switchShowDetect(detectType = DetectType.CARD_DETECT, false, true);
            }
        });
        binding.face2IB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityHelp.switchShowDetect(detectType = DetectType.FACE_DETECT, false, true);
            }
        });

        binding.scan2IB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityHelp.switchShowDetect(detectType = DetectType.SCAN_DETECT, false, true);
            }
        });
        activityHelp.switchShowDetect(detectType = DetectType.FACE_DETECT, true, true);

        binding.tvReturn.setOnClickListener(v -> finish());
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (detectType == DetectType.CARD_DETECT) {
            portScanHelper.dispatchKeyEvent(event);
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void cdTime(int cd) {
        binding.tvReturn.setText("返回" + cd + "s");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != activityHelp) {
            activityHelp.onDestroy();
            activityHelp = null;
        }
    }

    public interface DoubleVerifyListener {
        /**
         * @param type               验证类型
         * @param faceBitmap         人脸图片
         * @param userValidationBean 验证通过的人信息
         */
        void detect(DetectType type, Bitmap faceBitmap, UserValidationBean userValidationBean);
    }
}