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.ChineseNumberAndNumberUtils
import com.xty.common.util.CommonToastUtils
import com.xty.common.util.PointLengthFilter
import com.zj365.health.R
import com.zj365.health.databinding.ActBloodLipidAdjustBinding
import com.zj365.health.model.BloodLipidAdjustBean
import com.zj365.health.vm.BloodLipidAdjustVm
import com.zj365.health.weight.HealthDialog


/**
 * 血脂校准
 */
@Route(path = ARouterUrl.BL_ADJUST)
class BloodLipidAdjustAct : BaseVmAct<BloodLipidAdjustVm>() {

    val binding by lazy { ActBloodLipidAdjustBinding.inflate(layoutInflater) }
    override fun setLayout() = binding.root
    private val MAX_LENGTH=10
    val timSelect by lazy {
        TimeSelect(this) {
        }.apply {
            showHour = true
            showMin = true
            showSec = false
        }
    }

    private val submitDialog by lazy {
        HealthDialog(this, false, "请确保输入数据的准确性，确认是否提交？", true) {
            val bloods: MutableList<BloodLipidAdjustBean> =
                getBloodLipidAdjustData() ?: return@HealthDialog
            mViewModel.submitBloodLipidAdjustData(bloods)
        }
    }

    private val successDialog by lazy {
        HealthDialog(this, true, "尊敬的用户您好，系统将在30分钟后完成血脂校准，请您耐心等待！") {
            RouteManager.goAct(ARouterUrl.BL_ADJUST_RECORD)
            finish()
        }
    }

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

    override fun initData() {
        super.initData()
        addChildViews()
    }

    override fun initView() {
        super.initView()
        binding.title.mTvTitle.text = "血脂校准"
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }

        binding.mSubmit.isSelected = true
        binding.mSubmit.setOnClickListener {
            submitDialog.show()
        }

        binding.jiaozhijilu.setOnClickListener {
            RouteManager.goAct(ARouterUrl.BL_ADJUST_RECORD)
        }

        binding.mAdd.setOnClickListener {
            addChildViews()
        }

        //血脂校准须知
        binding.mTip.setOnClickListener {
            val webUrl = "https://mp.weixin.qq.com/s?__biz=Mzg4MDEyMDA3Mw==&mid=22474949" +
                    "13&idx=1&sn=ff21da0895b646004a7281fce0881d61&chksm=cf78b567f80f3c" +
                    "710cd94ac3f9a5606bdc13af60e4b3ee5143e59faf5f766bcc3dbebf946409#rd"
            bundle.clear()
            bundle.putString("title", "血脂校准须知")
            bundle.putString("url", webUrl)
            RouteManager.goAct(ARouterUrl.WEB_ACT, bundle)
        }
    }

    @SuppressLint("SetTextI18n")
    private fun addChildViews() {
        val viewIndex = binding.childLayout.childCount
        val viewNum = viewIndex + 1
        if (viewNum>MAX_LENGTH){
            CommonToastUtils.showToast("血脂校准最多添加"+MAX_LENGTH+"条")
            return
        }
        val childView = LayoutInflater.from(this).inflate(R.layout.item_blood_lipid_adjust, null)
        childView.findViewById<TextView>(R.id.mTitle).text =
            "第${ChineseNumberAndNumberUtils.intToChinese(viewNum)}次"
        childView.findViewById<EditText>(R.id.mEditLDL).filters = arrayOf(PointLengthFilter(2))
        childView.findViewById<EditText>(R.id.mEditCholesterol).filters =
            arrayOf(PointLengthFilter(2))
        childView.findViewById<EditText>(R.id.mEditTg).filters = arrayOf(PointLengthFilter(2))
        childView.findViewById<EditText>(R.id.mEditHDL).filters = arrayOf(PointLengthFilter(2))

        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
        )
    }

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

    private fun getBloodLipidAdjustData(): MutableList<BloodLipidAdjustBean>? {
        val list = mutableListOf<BloodLipidAdjustBean>()
        for (i in 0 until binding.childLayout.childCount) {
            val bloodLipidAdjustBean = BloodLipidAdjustBean()
            bloodLipidAdjustBean.bpTime = (getChildView(
                i,
                R.id.mVTimeView
            ) as TextView).text.toString()
            bloodLipidAdjustBean.tc = (getChildView(
                i,
                R.id.mEditCholesterol
            ) as TextView).text.toString()
            bloodLipidAdjustBean.tg = (getChildView(
                i,
                R.id.mEditTg
            ) as TextView).text.toString()
            bloodLipidAdjustBean.hdl = (getChildView(
                i,
                R.id.mEditHDL
            ) as TextView).text.toString()
            bloodLipidAdjustBean.ldl = (getChildView(
                i,
                R.id.mEditLDL
            ) as TextView).text.toString()

            if (list.size < 1) {
                if (bloodLipidAdjustBean.bpTime.isNullOrEmpty()) {
                    CommonToastUtils.showToast("请选择测量时间")
                    return null
                }
                if (bloodLipidAdjustBean.tc.isNullOrEmpty()) {
                    CommonToastUtils.showToast("请填写总胆固醇数值")
                    return null
                }
                if (bloodLipidAdjustBean.tg.isNullOrEmpty()) {
                    CommonToastUtils.showToast("请填写甘油三酯数值")
                    return null
                }
                if (bloodLipidAdjustBean.hdl.isNullOrEmpty()) {
                    CommonToastUtils.showToast("请填写高密度脂蛋白数值")
                    return null
                }
                if (bloodLipidAdjustBean.ldl.isNullOrEmpty()) {
                    CommonToastUtils.showToast("请填写低密度脂蛋白数值")
                    return null
                }
            } else {
                if (bloodLipidAdjustBean.bpTime.isNullOrEmpty()
                    && bloodLipidAdjustBean.tc.isNullOrEmpty()
                    && bloodLipidAdjustBean.tg.isNullOrEmpty()
                    && bloodLipidAdjustBean.hdl.isNullOrEmpty()
                    && bloodLipidAdjustBean.ldl.isNullOrEmpty()
                ) {
                    continue
                } else {
                    if (bloodLipidAdjustBean.bpTime.isNullOrEmpty()) {
                        CommonToastUtils.showToast("请选择测量时间")
                        return null
                    }
                    if (bloodLipidAdjustBean.tc.isNullOrEmpty()) {
                        CommonToastUtils.showToast("请填写总胆固醇数值")
                        return null
                    }
                    if (bloodLipidAdjustBean.tg.isNullOrEmpty()) {
                        CommonToastUtils.showToast("请填写甘油三酯数值")
                        return null
                    }
                    if (bloodLipidAdjustBean.hdl.isNullOrEmpty()) {
                        CommonToastUtils.showToast("请填写高密度脂蛋白数值")
                        return null
                    }
                    if (bloodLipidAdjustBean.ldl.isNullOrEmpty()) {
                        CommonToastUtils.showToast("请填写低密度脂蛋白数值")
                        return null
                    }
                }
            }
            bloodLipidAdjustBean.bpTime = "${bloodLipidAdjustBean.bpTime}:00"
            list.add(bloodLipidAdjustBean)
        }
        if (list.size < 1) {
            CommonToastUtils.showToast("数据不完整，请检查后再提交")
            return null
        }
        return list
    }
}