package com.grkj.iscs.view.activity.test.face

import android.view.View
import com.grkj.iscs.databinding.ActivityFaceBinding
import com.grkj.iscs.util.Executor
import com.grkj.iscs.view.base.BaseActivity

class FaceActivity : BaseActivity<ActivityFaceBinding>() {

    private val faceDetectorHelper by lazy {
        FaceDetectorHelper()
    }

    override val viewBinding: ActivityFaceBinding
        get() = ActivityFaceBinding.inflate(layoutInflater)

    override fun initView() {
        // 启动预览
        mBinding?.preview?.visibility = View.VISIBLE
        mBinding?.preview?.bringToFront()
        faceDetectorHelper.startPreview(this, mBinding?.preview!!) {
            // 注意切换线程
            Executor.runOnMain {
                // 关闭识别
                faceDetectorHelper.stopDetector()
                mBinding?.startDetector?.text = "开始识别"
                // 设置图片
                mBinding?.image?.setImageBitmap(it)
                mBinding?.image?.bringToFront()
                // 隐藏预览页面
                mBinding?.preview?.visibility = View.INVISIBLE
            }
        }


        mBinding?.startDetector?.setOnClickListener {
            faceDetectorHelper.let {
                if (it.isOpenDetector) {
                    it.stopDetector()
                    mBinding?.startDetector?.text = "开始识别"
                } else {
                    it.startDetector()
                    mBinding?.startDetector?.text = "关闭识别"
                }
            }
        }

        mBinding?.stopDetector?.setOnClickListener {
            faceDetectorHelper.switchCamera(this)
        }

        mBinding?.continueDetector?.setOnClickListener {
            mBinding?.preview?.visibility = View.VISIBLE
            mBinding?.preview?.bringToFront()
            faceDetectorHelper.startDetector()
            mBinding?.startDetector?.text = "关闭识别"
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        faceDetectorHelper.release()
    }
}