package com.grkj.iscs.view.activity

import android.content.Intent
import android.widget.ImageView
import com.grkj.iscs.BusinessManager
import com.grkj.iscs.R
import com.grkj.iscs.databinding.ActivityLoginBinding
import com.grkj.iscs.modbus.ModBusController
import com.grkj.iscs.util.AppUtils
import com.grkj.iscs.util.log.LogUtil
import com.grkj.iscs.view.base.BaseMvpActivity
import com.grkj.iscs.view.dialog.LoginDialog
import com.grkj.iscs.view.iview.ILoginView
import com.grkj.iscs.view.presenter.LoginPresenter
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

class LoginActivity : BaseMvpActivity<ILoginView, LoginPresenter, ActivityLoginBinding>() {

    private var cardLoginDialog: LoginDialog? = null

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

    override fun initView() {
        mBinding?.tvVersion?.text = "v${AppUtils.getPkgVerName(this)}"

        mBinding?.main?.setBackgroundResource(R.mipmap.login_bg)

        val pairList = mutableListOf(
            Pair(getString(R.string.login_face), R.mipmap.login_face),
            Pair(getString(R.string.login_fingerprint), R.mipmap.login_fingerprint),
            Pair(getString(R.string.login_card), R.mipmap.login_card),
            Pair(getString(R.string.login_account), R.mipmap.login_account)
        )

        mBinding?.rvType?.adapter =
            object : CommonAdapter<Pair<String, Int>>(this, R.layout.item_rv_login, pairList) {
                override fun convert(holder: ViewHolder, pair: Pair<String, Int>, position: Int) {
                    holder.setText(R.id.tv_name, pair.first)
                    holder.getView<ImageView>(R.id.iv_icon).setImageResource(pair.second)
                    holder.setOnClickListener(R.id.root) {
                        showLoginDialog(position)
                    }
                }
            }

        // TODO 只适配armeabi-v7a
        BusinessManager.connectDock(true)
    }

    /**
     * @param loginType 0:人脸 1:工卡 2:账号 3:指纹
     */
    private fun showLoginDialog(loginType: Int) {
        cardLoginDialog ?: run {
            LogUtil.i("创建Swipe dialog : ${presenter == null}")
            cardLoginDialog = LoginDialog(presenter, this) { isSuccess, cardInfoRespVO, userInfoRespVO ->
                if (isSuccess) {
                    val intent = Intent(this, HomeActivity::class.java)
                    if (cardInfoRespVO != null) {
                        intent.putExtra("cardInfo", cardInfoRespVO)
                    }
                    if (userInfoRespVO != null) {
                        intent.putExtra("userInfo", userInfoRespVO)
                    }
                    startActivity(intent)
                }
            }
        }
        cardLoginDialog?.showByType(loginType)
    }

    override fun onResume() {
        super.onResume()
        if (ModBusController.isRunning() != true) {
            BusinessManager.connectDock(true)
        }
        presenter?.registerListener()
    }

    override fun onStop() {
        super.onStop()
        cardLoginDialog?.dismiss()
        presenter?.unregisterListener()
    }

    override fun initPresenter(): LoginPresenter {
        return LoginPresenter()
    }
}