package com.zj365.health.act

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseVmAct
import com.xty.common.TimeSelect
import com.xty.common.arouter.ARouterUrl
import com.xty.common.doubleClick
import com.xty.common.event.BodyEvent
import com.zj365.health.databinding.ActBodyInfoShowOrAddBinding
import com.zj365.health.vm.BodyManagerVm
import com.xty.network.model.BodyInfo
import org.greenrobot.eventbus.EventBus

/**
 *  身体数据添加或展示
 */
@Route(path = ARouterUrl.BODY_INFO_SHOW_OR_ADD)
class BodyInfoShowOrAddAct : BaseVmAct<BodyManagerVm>() {
    val binding by lazy { ActBodyInfoShowOrAddBinding.inflate(layoutInflater) }
    private val timeSelect by lazy {
        TimeSelect(this) {

        }
    }
    var bean: BodyInfo? = null
    override fun initView() {
        super.initView()
        val beanTemp = intent?.extras?.getSerializable("bean")
        val userId = intent.getStringExtra("userId")
        if (beanTemp != null) {
            bean = beanTemp as BodyInfo
        }
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text = "添加运动数据"
        binding.mV1.setOnClickListener {
            timeSelect.selectTimeNoneView(it)
        }
        binding.mAddData.doubleClick {
            val v1 = binding.mV1.getValues()
            val v2 = binding.mV2.getValues()
            val v3 = binding.mV3.getValues()
            val v4 = binding.mV4.getValues()
            val v5 = binding.mV5.getValues()
            val v6 = binding.mV6.getValues()
            val v7 = binding.mV7.getValues()
            val v8 = binding.mV8.getValues()
            val v9 = binding.mV9.getValues()
            val v10 = binding.mV10.getValues()
            val v11 = binding.mV11.getValues()
            val v12 = binding.mV12.getValues()
            mViewModel.addBodyInfo(
                bean?.id,
                v1,
                v2,
                v3,
                v4,
                v5,
                v6,
                v7,
                v8,
                v9,
                v10,
                v11,
                v12,
                this,
                userId
            )
        }

        if (bean != null) {
            setViewData()
            binding.title.mTvTitle.text = "运动数据详情"
        }
    }

    private fun setViewData() {
        bean?.let {
            binding.mV1.setValues(it.time.replace("00:00:00", "") ?: "")
            binding.mV2.setValues(it.height ?: "", it.isChange)
            binding.mV3.setValues(it.weight ?: "", it.isChange)
            binding.mV4.setValues(it.bm ?: "", it.isChange)
            binding.mV5.setValues(it.bf ?: "", it.isChange)
            binding.mV6.setValues(it.bmi ?: "", it.isChange)
            binding.mV7.setValues(it.whf ?: "", it.isChange)
            binding.mV8.setValues(it.basicM ?: "", it.isChange)
            binding.mV9.setValues(it.fatC ?: "", it.isChange)
            binding.mV10.setValues(it.mc ?: "", it.isChange)
            binding.mV11.setValues(it.tweight ?: "", it.isChange)
            binding.mV12.setValues(it.scoreS ?: "", it.isChange)
            binding.mV1.apply {
                isEnabled = it.isChange
                if (!it.isChange) {
                    gonNextIcon()
                }
            }
            if (!it.isChange) {
                binding.mAddData.visibility = View.GONE
            }
        }
    }

    override fun liveObserver() {
        mViewModel.addLive.observe(this) {
            EventBus.getDefault().post(BodyEvent(BodyEvent.BODY_ADD))
            finish()
        }
    }

    override fun setLayout() = binding.root
}