package com.dlc.eboard.ui.auth

import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import com.dlc.eboard.LabApp
import com.dlc.eboard.databinding.ActivityAuthFingerBinding
import com.dlc.eboard.finger.FingerHelper
import com.dlc.eboard.finger.OnFingerListener
import com.dlc.eboard.ui.MainActivity
import com.dlc.eboard.ui.common.BaseCountDownActivity
import com.dlc.eboard.ui.widget.NavViewCompat
import com.dlc.eboard.ui.widget.TitleBar
import com.rc.core.log.RcLog
import com.rc.httpcore.vo.response.UserFingerVo

/**
 * 身份验证：指纹
 *
 * @author ReiChin_
 */
class FingerAuthActivity :
    BaseCountDownActivity<ActivityAuthFingerBinding>() {

    override fun createViewBinding() = ActivityAuthFingerBinding.inflate(layoutInflater)

    override val mTitleBar: TitleBar by lazy { viewBinding.titleBar }
    override val mNavView: NavViewCompat by lazy { viewBinding.navView }

    private lateinit var mFingerHelper: FingerHelper

    private var mFingerEnabled = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mFingerHelper = generateFingerHelper()
        lifecycle.addObserver(mFingerHelper)
    }

    override fun onDestroy() {
        super.onDestroy()
        lifecycle.removeObserver(mFingerHelper)
    }

    private fun generateFingerHelper(): FingerHelper {
        return FingerHelper(object : OnFingerListener {
            override fun showToast(message: String) {
                if (isFinishing) return
                this@FingerAuthActivity.showToast(message)
                if ("设备忙" == message) {
                    viewBinding.titleBar.postDelayed({
                        mFingerHelper.verify()
                    }, 1000)
                }
            }

            override fun onMatchSuccess(userVo: UserFingerVo): Boolean {
                if (isFinishing) return false
                authSuccess(userVo)
                return false
            }

            override fun onMatchFailure(message: String) {
                if (isFinishing) return
                showMatchErrDialog(message)
            }

            override fun loopMatch() {
                if (isFinishing || !mFingerEnabled) return
                mFingerHelper.verify()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        // 5s后开始验证指纹
        viewBinding.titleBar.postDelayed({
            mFingerHelper.lazyVerify()
        }, 1000)
        mFingerEnabled = true
    }

    override fun onPause() {
        mFingerEnabled = false
        super.onPause()
    }

    private fun authSuccess(userVo: UserFingerVo) {
        RcLog.info("FingerAuthActivity#onAuthSuccess() authType=FINGER, configAuthType=${LabApp.sLabConfig?.authType}, userVo=${userVo}")
        LabApp.sUserVo = userVo
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

    private fun showMatchErrDialog(message: String) {
        AlertDialog.Builder(this)
            .setTitle("提示")
            .setMessage(message)
            .setCancelable(false)
            .setPositiveButton("重新验证") { _, _ ->
                mFingerHelper.verify()
            }.show()
    }

    override fun enabledBackCountDown() = true

}