package com.zj365.health.act

import android.content.Intent
import android.view.View
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseListAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.adapter.AnswerAdapter
import com.zj365.health.databinding.ActSubmitReportMainBinding
import com.zj365.health.vm.RiskAssessVm
import com.xty.network.model.AnswerBean
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 已填问卷首页
 */
@Route(path = ARouterUrl.SUBMIT_REPORTMain)
class SubmitReportMainAct : BaseListAct<RiskAssessVm>() {

    private val col by lazy {
        mapOf(
            "零风险" to R.drawable.shape_low_round,
            "低风险" to R.drawable.shape_low_round,
            "中风险" to R.drawable.shape_middle_round,
            "高风险" to R.drawable.shape_high_round,
            "很高风险" to R.drawable.shape_super_high_round,
        )
    }
    private val textCol by lazy {
        mapOf(
            "零风险" to R.color.col_9Bd,
            "低风险" to R.color.col_9Bd,
            "中风险" to R.color.col_96D,
            "高风险" to R.color.col_481,
            "很高风险" to R.color.col_2621,
        )
    }

    val mAdapter by lazy { AnswerAdapter() }
    val binding by lazy { ActSubmitReportMainBinding.inflate(layoutInflater) }
    val QUEST_REQUEST = 10000
    var isEdit = false
    var bean: AnswerBean? = null
    var userId = ""
    override fun setLayout() = binding.root
    override fun initData() {
        super.initData()
        userId = intent.extras!!.getString("id").toString()
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "健康风险评估"
        binding.title.mIvBack.setOnClickListener { finish() }

        binding.title.mTvRight.apply {
            visibility =  View.VISIBLE
            text = "开始评估"
            setTextColor(resources.getColorStateList(R.color.col_02c))
            setOnClickListener {
                mViewModel.getQuestTime()
            }
        }
        binding.ll1.setOnClickListener(onClickListener)
        binding.ll2.setOnClickListener(onClickListener)
        binding.ll3.setOnClickListener(onClickListener)
        binding.ll4.setOnClickListener(onClickListener)
        binding.tvReport.setOnClickListener(onClickListener)
    }

    private var onClickListener = View.OnClickListener {
        when (it.id) {
            R.id.ll1 -> {
                bundle.clear()
                bundle.putInt("position", 0)
                bundle.putString("data", bean!!.createTime)
                bundle.putString("title", "高血压风险评估报告")
                bundle.putString("score", bean!!.hscore.toString())
                bundle.putString("level", bean!!.hrisk)
                RouteManager.goAct(ARouterUrl.QUEST_DETAIL, bundle)
            }

            R.id.ll2 -> {
                bundle.clear()
                bundle.putInt("position", 3)
                bundle.putString("data", bean!!.createTime)
                bundle.putString("title", "糖尿病风险评估报告")
                bundle.putString("score", bean!!.dscore.toString())
                bundle.putString("level", bean!!.drisk)
                RouteManager.goAct(ARouterUrl.QUEST_DETAIL, bundle)
            }

            R.id.ll3 -> {
                bundle.clear()
                bundle.putInt("position", 1)
                bundle.putString("data", bean!!.createTime)
                bundle.putString("title", "冠心病风险评估报告")
                bundle.putString("score", bean!!.cscore.toString())
                bundle.putString("level", bean!!.crisk)
                RouteManager.goAct(ARouterUrl.QUEST_DETAIL, bundle)
            }

            R.id.ll4 -> {
                bundle.clear()
                bundle.putInt("position", 2)
                bundle.putString("data", bean!!.createTime)
                bundle.putString("title", "脑卒中风险评估报告")
                bundle.putString("score", bean!!.ascore.toString())
                bundle.putString("level", bean!!.arisk)
                RouteManager.goAct(ARouterUrl.QUEST_DETAIL, bundle)
            }

            R.id.tvReport -> {
                mViewModel.getQuestTime()
            }
        }
    }

    override fun liveObserver() {
        mViewModel.answerLive.observe(this) {
            if (isEdit) {
                if (mAdapter.data.size == it.data.size) {
                    CommonToastUtils.showToast("报告正在生成中，请耐心等待1-3分钟")
                }
            }
            if (it.data.size == 0) {
                binding.title.mTvRight.text = "开始评估"
                binding.title.mTvRight.setOnClickListener {
                    mViewModel.getQuestTime()
                }
                binding.scroll.visibility = View.VISIBLE
                binding.topll.visibility = View.GONE
                return@observe
            } else {
                binding.title.mTvRight.text = "历史评估"
                binding.title.mTvRight.setOnClickListener {
                    bundle.clear()
                    bundle.putString("id", userId)
                    RouteManager.goAct(ARouterUrl.SUBMIT_REPORT, bundle)
                }
                binding.topll.visibility = View.VISIBLE
                binding.scroll.visibility = View.GONE
            }

            setDataTop(it.data)
            setDate(mAdapter, it.data)
            isEdit = false
        }
        mViewModel.timeOut.observe(this) {
            RouteManager.goActRequest(ARouterUrl.QUEST_LIST, this, QUEST_REQUEST)
//            if(it.data){
//                RouteManager.goActRequest(ARouterUrl.QUEST_LIST,this,QUEST_REQUEST)
//            }else{
//                ToastUtils.show(it.msg)
//                //RouteManager.goAct(ARouterUrl.RISK_ASSESS)
//            }
        }
    }

    private fun setDataTop(data: MutableList<AnswerBean>) {
        bean = data[0]
        binding.mStatus1.text = data[0].hrisk
        binding.mStatus1.setTextColor(ContextCompat.getColor(this, textCol[data[0].hrisk]!!))
        binding.mStatus1.setBackgroundResource(col[data[0].hrisk]!!)

        binding.mStatus2.text = data[0].drisk
        binding.mStatus2.setTextColor(ContextCompat.getColor(this, textCol[data[0].drisk]!!))
        binding.mStatus2.setBackgroundResource(col[data[0].drisk]!!)

        binding.mStatus3.text = data[0].crisk
        binding.mStatus3.setTextColor(ContextCompat.getColor(this, textCol[data[0].crisk]!!))
        binding.mStatus3.setBackgroundResource(col[data[0].crisk]!!)

        binding.mStatus4.text = data[0].arisk
        binding.mStatus4.setTextColor(ContextCompat.getColor(this, textCol[data[0].arisk]!!))
        binding.mStatus4.setBackgroundResource(col[data[0].arisk]!!)
        binding.tvCreateTime.text = "评估记录：${bean!!.createTime}"
    }

    override fun initAdapter() {
    }

    override fun loadData() {
        mViewModel.getAnswerList(userId)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == QUEST_REQUEST) {
            mainScope.launch {
                loadingView.show()
                delay(3000)
                loadingView.dismiss()
                isEdit = true
                mViewModel.getAnswerList(userId)
            }

        }

    }
}