package com.grkj.iscs_mc.view.activity

import android.content.Intent
import android.widget.ImageView
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.ActivityLoginBinding
import com.grkj.iscs_mc.util.AppUtils
import com.grkj.iscs_mc.view.base.BaseMvpActivity
import com.grkj.iscs_mc.view.dialog.LoginDialog
import com.grkj.iscs_mc.view.iview.ILoginView
import com.grkj.iscs_mc.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)
                    }
                }
            }
    }

    /**
     * @param loginType 0:人脸 1:工卡 2:账号 3:指纹
     */
    private fun showLoginDialog(loginType: Int) {
        cardLoginDialog ?: run {
            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 onStop() {
        super.onStop()
        cardLoginDialog?.dismiss()
    }

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