package com.zj365.health.act

import android.view.View
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.zj365.health.R
import com.zj365.health.adapter.WarringAdapter
import com.zj365.health.databinding.ActHealthWarringBinding
import com.zj365.health.model.WarnBean
import com.zj365.health.vm.EarlyAWarringVm
import com.zj365.health.weight.HealthDialog
import com.xty.network.model.WarringBean

/**
 * 健康 预警
 */
@Route(path = ARouterUrl.WARRING)
class EarlyWarringAct : BaseVmAct<EarlyAWarringVm>() {
    private var isDemonstrate: Boolean = false
    val binding by lazy {ActHealthWarringBinding.inflate(layoutInflater)}

    val dialogMSg="30天疾病预警是根据过往30天采集的相关体征数据，通过数据智能分析相关疾病未来30天的风险概率,有助于了解发病风险趋势，如您发现疾病风险较高，请结合自身身体状况进行进一步检查和相关治疗，30天疾病预警，仅做健康提示，不作为临床诊断和疾病发生判断标准。"
    val tipDialog by lazy{ HealthDialog(this,true,dialogMSg){
    } }

    var userId=""
    override fun setLayout(): View = binding.root

    val adapter by lazy { WarringAdapter() }


    override fun initData() {
        super.initData()
        intent.extras?.let {
            userId=intent.extras!!.getString("id").toString()
            isDemonstrate = intent.extras!!.getBoolean("isDemonstrate",false)
        }

    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text=resources.getString(R.string.early_1)
        binding.title.mIvBack .setOnClickListener { finish() }
        binding.llTip.setOnClickListener {
            tipDialog.show()
        }

        initRecycle()
    //    mViewModel.getEarlyWarring(userId)
        mViewModel.earlyDesc(userId)
    }
    fun initRecycle(){
        binding.mRecycle.layoutManager= LinearLayoutManager(this)
        binding.mRecycle.adapter=adapter
        adapter.setOnItemClickListener { adapter, view, position ->
            isDemonstrate
          var bean=  adapter.getItem(position) as WarringBean.Disease
            bundle.clear()
            bundle.putSerializable("bean",bean)
            RouteManager.goAct( ARouterUrl.WARRING_CONTENT,bundle)
        }

    }

    override fun liveObserver() {
        mViewModel.warringLive.observe(this){
            var list= mutableListOf<WarnBean>()
       /*     it.data?.let {bean->
                list.add(WarnBean("睡眠呼吸暂停",(abs(bean.sleep.toDouble()) *100).toInt(),bean.sleepContent,))
                list.add(WarnBean("冠心病",(abs(bean.coronary.toDouble()) *100).toInt(),bean.coronaryContent,))
                list.add(WarnBean("心力衰竭",(abs(bean.cardiac.toDouble()) *100).toInt(),bean.cardiacContent,))
                list.add(WarnBean("心动过速",(abs(bean.tachycardia.toDouble()) *100).toInt(),bean.tachycardiaContent,))
                list.add(WarnBean("心动过缓",(abs(bean.bradycardia.toDouble()) *100).toInt(),bean.bradycardiaContent,))
                list.add(WarnBean("房颤",(abs(bean.atrial.toDouble()) *100).toInt(),bean.atrialContent,))
            }*/
            adapter.setNewInstance(it.data.disease)
            binding.mTime.text="评估时间：${it.data.disease[0].createTime}"
            binding.mDesc.text="评估依据：30天${it.data.hr}次心率，${it.data.dbp}次血压，${it.data.so}次血氧，${it.data.temp}次体温，${it.data.respiratory}次呼吸率。"
        }

    }
    /*override fun initData() {
        super.initData()
        page=intent.extras!!.getInt("page")-1

    }
    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        initTitle()
        initVp2()
        initTab()
    }


    private fun initTitle() {
        binding.title.mTvTitle.text=resources.getString(R.string.health_early)
        var parentView = binding.title.mTvTitle.parent as View
        var bg = ContextCompat.getDrawable(this, R.drawable.shape_health_bg)
        parentView.background = bg
        binding.title.mIvBack.apply {
            this.setImageResource(R.mipmap.icon_back_white)
            this.setOnClickListener {
                finish()
            }
        }

    }
    //设置tab
    private fun  initTab(){
        tabName.forEach {
            var tab=binding.xTablayout.newTab()
            tab.text = resources.getString(it)
            binding.xTablayout.addTab( tab)
        }
        binding.xTablayout.setOnTabSelectedListener(object : XTabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: XTabLayout.Tab?) {
                binding.mVp2.currentItem = tab!!.position
                Log.e("TAG", "tab: "+tab!!.position )
            }

            override fun onTabUnselected(tab: XTabLayout.Tab?) {
            }

            override fun onTabReselected(tab: XTabLayout.Tab?) {
            }

        })
        binding.xTablayout.getTabAt(page)!!.select()
    }
    //设置vp2
    private fun initVp2() {
        binding.mVp2.adapter = VpAdapter(listFragment, this)
        binding.mVp2.offscreenPageLimit = 2
        binding.mVp2.currentItem = page
        binding.mVp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.xTablayout.getTabAt(position)!!.select()
            }
        })
    }*/
}