package com.zj365.health.act.datamanage

import android.view.View
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.databinding.ActNewHealthRecordBinding
import com.zj365.health.vm.MedicalHistoryVm
import com.xty.network.model.SettingBean
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 健康档案
 */
@Route(path = ARouterUrl.HEALTH_RECORD)
class NewHealthRecordAct : BaseVmAct<MedicalHistoryVm>() {

    val binding by lazy { ActNewHealthRecordBinding.inflate(layoutInflater) }
    var userInfo: SettingBean.User? = null
    var userId:String?= null

    override fun initData() {
        super.initData()
        userId = intent.getStringExtra("id")
    }

    override fun liveObserver() {
        mViewModel.allData.observe(this) {
            userInfo = it.data.user

            if(!isCompeleteInfo(userInfo)){
                binding.tvBaseStatus.text = "未完善"
            }else{
                binding.tvBaseStatus.text = "已完善"
            }
        }
    }

    override fun setLayout(): View = binding.root

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text = "健康档案"

        //既往病史
        binding.clHistoryLayout.setOnClickListener {
            bundle.clear()
            bundle.putSerializable("userId", userId)
            RouteManager.goAct(ARouterUrl.ILL_HISTORY,bundle)
        }
        //用药记录
        binding.clMedicalLayout.setOnClickListener {
            bundle.clear()
            bundle.putSerializable("userId", userId)
            RouteManager.goAct(ARouterUrl.DATA_MANAGE_USE_MEDICAL,bundle)
        }
        //体检记录
        binding.clCheckLayout.setOnClickListener {
            bundle.clear()
            bundle.putSerializable("userId", userId)
            RouteManager.goAct(ARouterUrl.DATA_MANAGE_TIJIAN,bundle)
        }

        //基础信息
        binding.clBaseInfoLayout.setOnClickListener {
            if (userInfo != null) {
                bundle.clear()
                bundle.putSerializable("userInfo", userInfo)
            }
            RouteManager.goAct(ARouterUrl.DATA_MANAGE_BASE_INFO, bundle)
        }

        //住院记录
        binding.clZhuyuanLayout.setOnClickListener {
            bundle.clear()
            bundle.putSerializable("userId", userId)
            RouteManager.goAct(ARouterUrl.DATA_MANAGE_HOSPITAL, bundle)
        }
        mViewModel.getUserInfo(userId)
    }

    fun isCompeleteInfo(userInfo: SettingBean.User?): Boolean {
        userInfo?.apply {
            if (name.isNullOrEmpty() || birthday.isNullOrEmpty()
                || nation.isNullOrEmpty()
                || height.isNaN() || weight.isNaN()||waistline == 0.0f
                || waistline.isNaN() || (province.isNullOrEmpty() && city.isNullOrEmpty() && district.isNullOrEmpty())
                || hobby.isNullOrEmpty()
                || religion.isNullOrEmpty()
                || nation.isNullOrEmpty()) {
                return false
            }
        }
        return true

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun userInfoUpdate(info: SettingBean.User) {
        userInfo = info
        if (isCompeleteInfo(userInfo)) {
            binding.tvBaseStatus.text = "已完善"
        } else {
            binding.tvBaseStatus.text = "未完善"
        }
    }
}