package com.zj365.health.act

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.core.view.get
import com.alibaba.android.arouter.facade.annotation.Route
import com.tamsiree.rxkit.RxKeyboardTool
import com.xty.base.act.BaseVmAct
import com.xty.common.TimeSelect
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.databinding.ActBloodCalilbrationBinding
import com.zj365.health.model.BloodBean
import com.zj365.health.vm.BloodCalibrationVm
import com.zj365.health.weight.HealthDialog

/**
 * 血压校准
 */
@Route(path = ARouterUrl.BLOOD_ALIGN)
class BloodCalibrationAct : BaseVmAct<BloodCalibrationVm>() {

    val binding by lazy { ActBloodCalilbrationBinding.inflate(layoutInflater) }
    override fun setLayout() = binding.root

    private val titleArray by lazy { resources.getStringArray(R.array.blood_array) }
    val timSelect by lazy {
        TimeSelect(this) {
        }.apply {
            showHour = true
            showMin = true
            showSec = false
        }
    }
    private val successDialog by lazy {
        HealthDialog(this, true, "尊敬的用户您好，系统将在30分钟后完成血压校准，请您耐心等待！") {
            RouteManager.goAct(ARouterUrl.BLOOD_ALIGN_HISTORY)
            finish()
        }
    }
    private val submitDialog by lazy {
        HealthDialog(this, false, "请确保输入数据的准确性，确认是否提交？", true) {
            val bloods: MutableList<BloodBean> = getBloods() ?: return@HealthDialog
            mViewModel.bloodCalibration(bloods)
        }
    }

    override fun liveObserver() {
        mViewModel.calibrationLive.observe(this) {
            successDialog.setCancelable(false)
            successDialog.setCanceledOnTouchOutside(false)
            successDialog.show()
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text = getString(R.string.blood_align)
        binding.mSubmit.isSelected = true
        binding.mSubmit.setOnClickListener {
            submitDialog.show()

        }
        binding.mTip.setOnClickListener {
            val webUrl = "https://mp.weixin.qq.com/s/gQdOW45OfSU0Y-J-zhHq1A"
            bundle.clear()
            bundle.putString("title", "血压校准须知")
            bundle.putString("url", webUrl)
            RouteManager.goAct(ARouterUrl.WEB_ACT, bundle)
        }
        binding.jiaozhijilu.text = getString(R.string.blood_align_history)
        binding.jiaozhijilu.setOnClickListener {
            RouteManager.goAct(ARouterUrl.BLOOD_ALIGN_HISTORY)
        }
        binding.mAdd.setOnClickListener {
            addChildViews()
        }

        addChildViews()
    }

    private fun getBloods(): MutableList<BloodBean>? {
        val list: MutableList<BloodBean> = mutableListOf()
        for (i in 0 until binding.childLayout.childCount) {
            val bloodBean = BloodBean()
            bloodBean.bpTime = (getChildView(i, R.id.mVTimeView) as TextView).text.toString()
            bloodBean.hdbp = (getChildView(i, R.id.mEditDdbp) as EditText).text.toString()
            bloodBean.hsbp = (getChildView(i, R.id.mEditGdbp) as EditText).text.toString()

            if (bloodBean.bpTime.isNullOrEmpty() || bloodBean.hsbp.isNullOrEmpty() || bloodBean.hdbp.isNullOrEmpty()) {
                continue
            }

            if (bloodBean.hsbp.toFloat() > 260.0 || bloodBean.hsbp.toFloat() < 70) {
                CommonToastUtils.showToast("如高压值>260则填写260，小于70则填写70")
                return null
            }

            if (bloodBean.hdbp.toFloat() > 150.0 || bloodBean.hdbp.toFloat() < 40) {
                CommonToastUtils.showToast("如低压值>150则填写150，小于40则填写40")
                return null
            }

            bloodBean.bpTime = "${bloodBean.bpTime}:00"//手动拼接秒
            list.add(bloodBean)
        }
        return list
    }

    @SuppressLint("InflateParams")
    private fun addChildViews() {
        val childView = LayoutInflater.from(this).inflate(R.layout.item_blood_calib, null)
        val viewIndex = binding.childLayout.childCount//view下标
        val viewNum = viewIndex + 1//页码 第几次
        childView.findViewById<TextView>(R.id.mTitle).text =
            titleArray[viewIndex]
        val mTvTimeView = childView.findViewById<TextView>(R.id.mVTimeView)
        mTvTimeView.setOnClickListener {
            RxKeyboardTool.hideSoftInput(this)
            timSelect.selectTime(it, "yyyy-MM-dd HH:mm")
        }
        childView.setPadding(0, 0, 0, dp2px(0))

        binding.childLayout.addView(
            childView
        )

        if (viewNum > 3) {//最多上传3组数据,不能大于区间数量
            binding.mAdd.visibility = View.GONE
        }
    }

    private fun getChildView(index: Int, childId: Int = 0): View {
        return if (childId == 0) binding.childLayout[index]
        else binding.childLayout[index].findViewById(childId)
    }
}