package com.zj365.health.act

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseVmAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.recycleUtils.RecycleItem
import com.zj365.health.R
import com.zj365.health.adapter.RiskAssessAdapter
import com.zj365.health.databinding.ActRiskAssessBinding
import com.zj365.health.vm.RiskAssessVm
import com.xty.network.model.AnswerBean


/**
 * 风险评估
 */
@Route(path = ARouterUrl.RISK_ASSESS)
class RiskAssessAct: BaseVmAct<RiskAssessVm>() {
    private lateinit var bean: AnswerBean
    val title by lazy{ arrayOf("高血压风险评估报告","糖尿病风险评估报告","冠心病风险评估报告","脑卒中风险评估报告")}
    var userId=""
    val mAdapter by lazy{ RiskAssessAdapter() }
    val binding by lazy{ActRiskAssessBinding.inflate(layoutInflater)}
    override fun setLayout()=binding.root
    override fun initData() {
        super.initData()
        bean=intent.extras!!.getSerializable("bean") as AnswerBean
        userId=intent.extras!!.getString("id").toString()
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }

        binding.title.mTvTitle.text=getString(R.string.early_2)
        binding.tvCreateTime.text = "评估记录：${bean.createTime}"
        initRecycle()
        mViewModel.getAnswerList(userId)

        binding.tvReport.setOnClickListener {
            mViewModel.getQuestTime()
        }
    }
    private fun initRecycle() {
        var list= mutableListOf<String>()
        bean.let {
            list.add(it.hrisk)
            list.add(it.drisk)
            list.add(it.crisk)
            list.add(it.arisk)
        }
        binding.mRecycle.adapter=mAdapter
        binding.mRecycle.addItemDecoration(RecycleItem(this,20))
        binding.mRecycle.layoutManager=LinearLayoutManager(this)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            var lev =  bean.hrisk
            var score= when (position) {
                0 -> {
                    lev = bean.hrisk
                    bean.hscore
                }
                1 -> {

                    lev = bean.drisk
                    bean.dscore

                }
                2 -> {

                    lev = bean.crisk
                    bean.cscore

                }
                3 -> {
                    lev = bean.arisk
                    bean.ascore
                }
                else -> 0
            }

            bundle.clear()
            bundle.putInt("position",position)
            bundle.putString("data",bean.createTime)
            bundle.putString("title",title[position])
            bundle.putString("score",score.toString())
            bundle.putString("level",lev)

            RouteManager.goAct(ARouterUrl.QUEST_DETAIL,bundle)
        }
        mAdapter.setNewInstance(list)
    }

    val QUEST_REQUEST=10000

    override fun liveObserver() {
        mViewModel.timeOut.observe(this){
            RouteManager.goActRequest(ARouterUrl.QUEST_LIST,this,QUEST_REQUEST)
        }
    }
}