package com.dlc.chemical.ui.unlock

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.dlc.chemical.R
import com.dlc.chemical.databinding.ActivitySelectAuthMethodBinding
import com.dlc.chemical.ui.MainActivity
import com.dlc.chemical.ui.common.BaseCountDownActivity
import com.dlc.chemical.ui.common.LockType
import com.dlc.chemical.ui.dialog.SuccessDialog
import com.dlc.chemical.ui.widget.TitleBar
import com.rc.httpcore.client.ApiRepository

/**
 * info
 *
 * @author ReiChin_
 */
class SelectAuthMethodActivity : BaseCountDownActivity<ActivitySelectAuthMethodBinding>(),
    TitleBar.TitleListener {

    companion object {
        private const val REQ_PERSON_AUTH = 10
    }

    private var mLockType: String? = null

    override fun beforeSetContentView() {
        mLockType = intent.getStringExtra("lockType")
    }

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

    override fun initViews(savedInstanceState: Bundle?) {
        viewBinding.titleBar.setTitleListener(this)
        viewBinding.titleBar.setTitleInfoFromSp()

        viewBinding.faceDetect.setOnClickListener {
            // 人脸识别
            querySubInfo()
        }

        viewBinding.schoolCard.setOnClickListener {
            // 校园卡
            val intent = Intent(this, SchoolCardAuthActivity::class.java)
            startActivityForResult(intent, REQ_PERSON_AUTH)
        }

        SuccessDialog(
            context = this,
            message = "请选择身份验证方式",
            messageTextColor = "#FF333333",
            imageRes = R.mipmap.icon_srsfyz_ts
        ).apply {
            setCancelable(false)
            setCanceledOnTouchOutside(false)
        }.show()
    }

    /**
     * 查询实验室信息
     */
    private fun querySubInfo() {
        showLoading("查询中...")
        val disposable = ApiRepository.querySubInfo()
            .subscribe({ data ->
                dismissLoading()
                val intent = Intent(this, FaceDetectAuthActivity::class.java)
                intent.putExtra("subInfo", data)
                startActivityForResult(intent, REQ_PERSON_AUTH)
            }, { throwable ->
                dismissLoading()
                showNetError(throwable)
                throwable.printStackTrace()
            })
        addDisposable(disposable)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            REQ_PERSON_AUTH -> {
                if (RESULT_OK == resultCode) {
                    if (LockType.SYSTEM.code == mLockType) {
                        val intent = Intent(this, ChemicalCabinetActivity::class.java)
                        startActivity(intent)
                    } else {
                        val intent = Intent()
                        intent.putExtra("auth_result", true)
                        intent.putExtra("joinOneUser", data?.getStringExtra("joinOneUser"))
                        intent.putExtra("joinTwoUser", data?.getStringExtra("joinTwoUser"))
                        setResult(RESULT_OK, intent)
                    }
                    finish()
                }
            }
        }
    }

    override fun onHomeViewClicked(view: View) {
        startActivity(Intent(this, MainActivity::class.java))
    }

    override fun onBackViewClicked(view: View) {
        finish()
    }

}