package com.grkj.iscs_mc.view.dialog

import android.content.Context
import android.view.InputDevice
import android.view.KeyEvent
import android.view.View
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.DialogLoginBinding
import com.grkj.iscs_mc.extentions.toByteArrays
import com.grkj.iscs_mc.extentions.toHexStrings
import com.grkj.iscs_mc.model.vo.card.CardInfoRespVO
import com.grkj.iscs_mc.model.vo.user.UserInfoRespVO
import com.grkj.iscs_mc.util.Executor
import com.grkj.iscs_mc.view.base.BaseDialog
import com.grkj.iscs_mc.view.presenter.LoginPresenter

class LoginDialog(val presenter: LoginPresenter?, val ctx: Context, private var callBack: ((Boolean, CardInfoRespVO?, UserInfoRespVO?) -> Unit)? = null) :
    BaseDialog<DialogLoginBinding>(ctx) {

    private var cardNo = ""
    private var mLoginType = 3 // 0:人脸 1:指纹 2:工卡 3:账号
    private val mPairList = mutableListOf(
        Pair(context.getString(R.string.please_scan_face), R.mipmap.login_face),
        Pair(context.getString(R.string.please_scan_fingerprint), R.mipmap.login_fingerprint),
        Pair(context.getString(R.string.please_swipe_card), R.mipmap.login_card)
    )

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

    override fun initView() {
        mBinding?.tvLogin?.setOnClickListener {
            presenter?.login(ctx, mBinding?.etAccount?.text.toString(), mBinding?.etPassword?.text.toString()) { isSuccess, cardInfoRespVO, userInfoRespVO ->
                Executor.runOnMain {
                    if (isSuccess) {
                        dismiss()
                    }
                    callBack?.invoke(isSuccess, cardInfoRespVO, userInfoRespVO)
                }
            }
        }
        mBinding?.tvCancel?.setOnClickListener { dismiss() }
    }

    /**
     * 根据类型显示弹框
     *
     * @param loginType 0:人脸 1:指纹 2:工卡 3:账号
     */
    fun showByType(loginType: Int) {
        mLoginType = loginType
        if (loginType == 3) {
            mBinding?.llAccountContainer?.visibility = View.VISIBLE
            mBinding?.llEasyContainer?.visibility = View.GONE
        } else {
            mBinding?.llAccountContainer?.visibility = View.GONE
            mBinding?.llEasyContainer?.visibility = View.VISIBLE
            mBinding?.ivIcon?.setImageResource(mPairList[loginType].second)
            mBinding?.tvTip?.text = mPairList[loginType].first
        }
        show()
    }

    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        if (mLoginType != 2) {
            return super.dispatchKeyEvent(event)
        }
        if (event.action == KeyEvent.ACTION_UP && event.source == InputDevice.SOURCE_KEYBOARD) {
//            val view = currentFocus
//            if (view is EditText) {
//                val text = view.text?.toString()
//                if (text!!.isNotEmpty()) {
//                    val rst = text.substring(0, text.length - 1)
//                    view.setText(rst)
//                }
//            }
            // 检测到回车开始处理
            if (event.keyCode == 66) {
                cardNo = cardNo.toLong().toByteArrays().toHexStrings(false)
                presenter?.cardLogin(cardNo) { isSuccess, cardInfoRespVO, userInfoRespVO ->
                    if (isSuccess) {
                        dismiss()
                    }
                    callBack?.invoke(isSuccess, cardInfoRespVO, userInfoRespVO)
                }
                // 重置cardNo
                cardNo = ""
                return super.dispatchKeyEvent(event)
            }
            cardNo += event.keyCharacterMap.getDisplayLabel(event.keyCode)
        }
        return super.dispatchKeyEvent(event)
    }

    override fun dismiss() {
        super.dismiss()
        if (mLoginType == 3) {
            mBinding?.etAccount?.text?.clear()
            mBinding?.etPassword?.text?.clear()
        }
    }
}