package com.dlc.exam.ui.exam

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.dlc.exam.ExamApp
import com.dlc.exam.common.ExamType
import com.dlc.exam.databinding.ActivitySafetyExamHomeBinding
import com.dlc.exam.ui.common.BaseCountDownActivity
import com.dlc.exam.ui.me.PersonalCenterActivity
import com.dlc.exam.ui.widget.TitleBar
import com.rc.httpcore.HttpClient
import com.rc.httpcore.client.ApiRepository

/**
 * 安全考试
 *
 * @author ReiChin_
 */
class SafetyExamHomeActivity : BaseCountDownActivity<ActivitySafetyExamHomeBinding>(),
    TitleBar.TitleListener {

    override fun createViewBinding() =
        ActivitySafetyExamHomeBinding.inflate(LayoutInflater.from(this))

    override fun initListener() {
        viewBinding.titleBar.setTitleListener(this)
        viewBinding.titleBar.setUsername(ExamApp.sLearnLoginVo?.userName)

        // 安全准入考试
        viewBinding.safetyExam.setOnClickListener {
            val intent = Intent(this, ExamActivity::class.java)
            intent.putExtra("exam_type", ExamType.SAFETY.type)
            startActivity(intent)
        }
        // 负面清单考试
        viewBinding.negativeExam.setOnClickListener {
            callQueryUserInfoApi { status ->
                if (ExamType.NEGATIVE.type == status) {
                    val intent = Intent(this, ExamActivity::class.java)
                    intent.putExtra("exam_type", ExamType.NEGATIVE.type)
                    startActivity(intent)
                } else {
                    showToast("当前登录人员不在负面清单中。")
                }
            }
        }
        // 黑名单考试
        viewBinding.blackListExam.setOnClickListener {
            callQueryUserInfoApi { status ->
                if (ExamType.BLACK_LIST.type == status) {
                    val intent = Intent(this, ExamActivity::class.java)
                    intent.putExtra("exam_type", ExamType.BLACK_LIST.type)
                    startActivity(intent)
                } else {
                    showToast("当前登录人员不在黑名单中。")
                }
            }
        }
        // 模拟考试
        viewBinding.mockTest.setOnClickListener {
            startActivity(Intent(this, MockExamActivity::class.java))
        }
    }

    private fun callQueryUserInfoApi(callback: (status: String?) -> Unit) {
        showLoading("查询中...")
        val disposable = ApiRepository.queryUserInfo()
            .subscribe({
                dismissLoading()
                callback.invoke(it.status)
            }, { throwable ->
                dismissLoading()
                throwable.printStackTrace()
                showNetError(throwable)
            })
        addDisposable(disposable)
    }

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

    override fun onLogoutViewClicked(view: View) {
        callLogoutApi {
            showToast("已退出登录")

            ExamApp.sLearnLoginVo = null
            HttpClient.token = null
            finish()
        }
    }

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

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

}