package com.dlc.eboard.ui.finger

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.dlc.eboard.LabApp
import com.dlc.eboard.R
import com.dlc.eboard.common.CommonUtils
import com.dlc.eboard.databinding.ActivityFingerSelectBinding
import com.dlc.eboard.ui.common.BaseCountDownActivity
import com.dlc.eboard.ui.widget.NavViewCompat
import com.dlc.eboard.ui.widget.TitleBar
import com.rc.httpcore.client.ApiRepository
import com.rc.httpcore.vo.response.UserFingerVo

/**
 * info
 *
 * @author ReiChin_
 */
class SelectFingerActivity :
    BaseCountDownActivity<ActivityFingerSelectBinding>() {

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

    companion object {
        private const val REQ_ENROLL = 100
    }

    private lateinit var mUserId: String
    private lateinit var mUserName: String
    private lateinit var mUserType: String
    private var mCurrentKey: String? = null

    // key: left1 map: UserFingerVo
    private val mFingerMap = mutableMapOf<String, UserFingerVo>()

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

    override fun initViews(savedInstanceState: Bundle?) {
        mUserId = intent.getStringExtra("userId")!!
        mUserName = intent.getStringExtra("userName")!!
        mUserType = intent.getStringExtra("userType")!!

        super.initViews(savedInstanceState)
        viewBinding.fingerLeft1.setOnClickListener { fingerViewClicked("left1", it as ImageView) }
        viewBinding.fingerLeft2.setOnClickListener { fingerViewClicked("left2", it as ImageView) }
        viewBinding.fingerLeft3.setOnClickListener { fingerViewClicked("left3", it as ImageView) }
        viewBinding.fingerLeft4.setOnClickListener { fingerViewClicked("left4", it as ImageView) }
        viewBinding.fingerLeft5.setOnClickListener { fingerViewClicked("left5", it as ImageView) }
        viewBinding.fingerRight1.setOnClickListener { fingerViewClicked("right1", it as ImageView) }
        viewBinding.fingerRight2.setOnClickListener { fingerViewClicked("right2", it as ImageView) }
        viewBinding.fingerRight3.setOnClickListener { fingerViewClicked("right3", it as ImageView) }
        viewBinding.fingerRight4.setOnClickListener { fingerViewClicked("right4", it as ImageView) }
        viewBinding.fingerRight5.setOnClickListener { fingerViewClicked("right5", it as ImageView) }

        viewBinding.enroll.setOnClickListener {
            if ("录入指纹" == viewBinding.enroll.text) {
                val intent = Intent(this, FingerEnrollActivity::class.java)
                intent.putExtra("userId", mUserId)
                intent.putExtra("userName", mUserName)
                intent.putExtra("userType", mUserType)
                intent.putExtra("fingerKey", mCurrentKey)
                startActivityForResult(intent, REQ_ENROLL)
            } else {
                // 删除指纹
                mFingerMap[mCurrentKey]?.let {
                    callDeleteFingerApi(it.id)
                }
            }
        }
        queryUserFingers()
    }

    private fun callDeleteFingerApi(id: String) {
        showLoading("查询中...")
        val disposable = ApiRepository.deleteFingerById(id)
            .subscribe({
                dismissLoading()
                mFingerMap.remove(mCurrentKey)
                refreshFingerViews()
                mCurrentKey = null
                viewBinding.enroll.visibility = View.GONE
            }, { throwable ->
                dismissLoading()
                showNetError(throwable)
                throwable.printStackTrace()
            })
        addDisposable(disposable)
    }

    private fun queryUserFingers() {
        if (null == LabApp.sLabConfig) {
            showToast("暂无实验室信息，即将重新获取")
            CommonUtils.restartApp(this)
            return
        }

        showLoading("查询中...")
        val disposable = ApiRepository.getFingerByUserId(LabApp.sLabConfig!!.labId, mUserId)
            .subscribe({ data ->
                dismissLoading()
                data.forEach { mFingerMap[it.fingerKey] = it }
                refreshFingerViews()
            }, { throwable ->
                dismissLoading()
                showNetError(throwable)
                throwable.printStackTrace()
            })
        addDisposable(disposable)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (REQ_ENROLL == requestCode) {
            if (RESULT_OK == resultCode) {
                mCurrentKey = null
                viewBinding.enroll.visibility = View.GONE
                // 重新查询录入的指纹
                queryUserFingers()
            }
        }
    }

    private fun fingerViewClicked(key: String, imageView: ImageView) {
        mCurrentKey = key
        refreshFingerViews()
        viewBinding.enroll.visibility = View.VISIBLE
        val userFinger = mFingerMap[mCurrentKey]
        if (null == userFinger) {
            viewBinding.enroll.text = "录入指纹"
            imageView.setImageResource(R.mipmap.icon_xz)
        } else {
            viewBinding.enroll.text = "删除指纹"
            imageView.setImageResource(R.mipmap.icon_xz_ylr)
        }
    }

    private fun refreshFingerViews() {
        refreshFingerViews("left1", viewBinding.fingerLeft1, viewBinding.fingerLeft1Text)
        refreshFingerViews("left2", viewBinding.fingerLeft2, viewBinding.fingerLeft2Text)
        refreshFingerViews("left3", viewBinding.fingerLeft3, viewBinding.fingerLeft3Text)
        refreshFingerViews("left4", viewBinding.fingerLeft4, viewBinding.fingerLeft4Text)
        refreshFingerViews("left5", viewBinding.fingerLeft5, viewBinding.fingerLeft5Text)
        refreshFingerViews("right1", viewBinding.fingerRight1, viewBinding.fingerRight1Text)
        refreshFingerViews("right2", viewBinding.fingerRight2, viewBinding.fingerRight2Text)
        refreshFingerViews("right3", viewBinding.fingerRight3, viewBinding.fingerRight3Text)
        refreshFingerViews("right4", viewBinding.fingerRight4, viewBinding.fingerRight4Text)
        refreshFingerViews("right5", viewBinding.fingerRight5, viewBinding.fingerRight5Text)
    }

    private fun refreshFingerViews(key: String, imageView: ImageView, textView: TextView) {
        val userFinger = mFingerMap[key]
        if (null == userFinger) {
            imageView.setImageResource(R.mipmap.icon_wlr)
            textView.visibility = View.GONE
        } else {
            imageView.setImageResource(R.mipmap.icon_zwylr)
            textView.visibility = View.VISIBLE
        }
    }

    override fun enabledBackCountDown() = true

}