package com.zj365.health.act.binddevice

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.xty.base.act.BaseVmAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.setImage
import com.zj365.health.R
import com.zj365.health.adapter.binddevice.BodyFatDetailAdapter
import com.zj365.health.databinding.ActBodyFatDetailBinding
import com.zj365.health.vm.BindDeviceVm
import com.xty.network.model.BodyFatBean
import com.xty.network.model.BodyFatHistoryBean
import com.xty.network.model.HealthDataBean


@Route(path = ARouterUrl.BODY_FAT_DETAIL)
class BodyFatDetailAct : BaseVmAct<BindDeviceVm>() {

    private val defaultIcon by lazy { arrayOf(R.mipmap.ic_default_man, R.mipmap.ic_default_women) }
    val binding by lazy { ActBodyFatDetailBinding.inflate(layoutInflater) }
    val mDetailAdapter by lazy { BodyFatDetailAdapter() }

    var bodyData: BodyFatHistoryBean? = null
    var manageInfo:HealthDataBean?=null

    var id:String?=null

    override fun initData() {
        super.initData()
        bodyData = intent.getSerializableExtra("bodyData") as BodyFatHistoryBean?
        manageInfo = intent.getSerializableExtra("manageInfo") as HealthDataBean?
        id = intent.getStringExtra("pbfId")
    }

    override fun liveObserver() {
        mViewModel.pbfDetail.observe(this){
            setData(it.data)
        }
    }

    override fun setLayout(): View = binding.root

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)

        binding.title.mTvTitle.text = "体脂报告"
        binding.title.mIvBack.setOnClickListener { finish() }

        if (id !=null){
            mViewModel.getPbfInfo(id!!)
        }

        binding.rlcDetail.apply {
            layoutManager = GridLayoutManager(this@BodyFatDetailAct, 3)
            adapter = mDetailAdapter
        }

        mDetailAdapter.setOnItemClickListener { adapter, view, position ->

            val detail = adapter.data[position] as BodyFatBean.Label
            bundle.putSerializable("itemDetail",detail)
            RouteManager.goAct(ARouterUrl.BODY_FAT_ITEM_DETAIL,bundle)

        }

        setManageData(manageInfo)
        setData(bodyData)
    }

    private fun setManageData(manage:HealthDataBean?){
        manage?.apply {
            if (avatarUrl.isNullOrEmpty()) {
                binding.ivHeadIcon.setImageResource(defaultIcon[if (sex - 1 < 0) 0 else sex - 1])
            } else {
                binding.ivHeadIcon.apply {
                    setImage(this@BodyFatDetailAct, avatarUrl)
                }
            }

            binding.name.text = userName
            binding.tvWeight.text = "$dataValue"
            binding.tvTestTime.text = "$time 测量"

            binding.bmiLayout.seekbar.isEnabled = false

            binding.llDetailLayout.visibility = View.GONE
            binding.llNoDataLayout.visibility = View.VISIBLE

            binding.bodyFatLayout.bodyFatLayout.visibility = View.GONE
            binding.muscleLayout.muscleLayout.visibility = View.GONE
            binding.stoneLayout.stoneLayout.visibility = View.GONE

            bmiInfo.apply {
                val label = BodyFatBean.Label("BMI",null,null,levelName,null,"",bmi,levelIntervalList)
                mDetailAdapter.addData(label)

                var start: Double = 0.0
                var milldle: Double = 0.0
                var end: Double = 0.0
                if (levelIntervalList.size > 3) {
                    val first = levelIntervalList[0]
                    val second =levelIntervalList[1]
                    val three = levelIntervalList[2]
                    start = first.endValue
                    milldle = second.endValue
                    end = three.endValue
                }
                binding.bmiLayout.tvBmiValue.text = "BMI $bmi"

                binding.bmiLayout.tvStartValue.text = "$start"
                binding.bmiLayout.tvMidlleValue.text = "$milldle"
                binding.bmiLayout.tvEndValue.text = "$end"

                //0-start
                if (bmi <= start) {
                    binding.bmiLayout.seekbar.setProgress((25 * bmi / start).toFloat())
                } else if (bmi <= milldle) {  //start - millde
                    binding.bmiLayout.seekbar.setProgress(((25 * (bmi - start) / (milldle - start) + 25).toFloat()))
                } else if (bmi <= end) { //millde -end
                    binding.bmiLayout.seekbar.setProgress(((25 * (bmi - milldle) / (end - milldle) + 50).toFloat()))
                } else if (bmi > end) { //end -100
                    if (75 + (bmi - end) >= 100) {
                        binding.bmiLayout.seekbar.setProgress(100f)
                    } else {
                        binding.bmiLayout.seekbar.setProgress((75 + (bmi - end)).toFloat())
                    }
                } else {
                    binding.bmiLayout.seekbar.setProgress(100f)
                }
            }

        }
    }
    fun setData(bodyData: BodyFatHistoryBean?){

        if (bodyData == null){
            binding.llNoDataLayout.visibility = View.GONE
        }
        bodyData?.apply {
            if (avatarUrl.isNullOrEmpty()) {
                binding.ivHeadIcon.setImageResource(defaultIcon[if (sex - 1 < 0) 0 else sex - 1])
            } else {
                binding.ivHeadIcon.apply {
                    setImage(this@BodyFatDetailAct, avatarUrl)
                }
            }

            binding.name.text = userName
            binding.tvWeight.text = "$weight"
            binding.tvTestTime.text = "$createTime 测量"

            binding.bmiLayout.tvBmiValue.text = "BMI $bmi"
            binding.bodyFatLayout.tvBodyFatValue.text = "体脂率 $pbf%"
            binding.muscleLayout.tvMuscleValue.text = "肌肉量 ${muscle}kg"
            binding.stoneLayout.tvStoneValue.text = "骨量 ${bone}kg"

            binding.bmiLayout.seekbar.isEnabled = false
            binding.bodyFatLayout.seekbar.isEnabled = false
            binding.muscleLayout.seekbar.isEnabled = false
            binding.stoneLayout.seekbar.isEnabled = false

            val  detail = JSON.parseObject(interpretInfo, BodyFatBean::class.java)

            if (detail == null) {
                binding.llDetailLayout.visibility = View.GONE
                binding.llNoDataLayout.visibility = View.VISIBLE

                bmiInfo.apply {
                    val label = BodyFatBean.Label("BMI",null,null,levelName,null,"",bmi,levelIntervalList)
                    mDetailAdapter.addData(label)

                    var start = 0.0
                    var milldle = 0.0
                    var end = 0.0
                    if (levelIntervalList.size > 3) {
                        val first = levelIntervalList[0]
                        val second =levelIntervalList[1]
                        val three = levelIntervalList[2]
                        start = first.endValue
                        milldle = second.endValue
                        end = three.endValue
                    }

                    binding.bmiLayout.tvStartValue.text = "$start"
                    binding.bmiLayout.tvMidlleValue.text = "$milldle"
                    binding.bmiLayout.tvEndValue.text = "$end"

                    //0-start
                    if (bmi <= start) {
                        binding.bmiLayout.seekbar.setProgress((25 * bmi / start).toFloat())
                    } else if (bmi <= milldle) {  //start - millde
                        binding.bmiLayout.seekbar.setProgress(((25 * (bmi - start) / (milldle - start) + 25).toFloat()))
                    } else if (bmi <= end) { //millde -end
                        binding.bmiLayout.seekbar.setProgress(((25 * (bmi - milldle) / (end - milldle) + 50).toFloat()))
                    } else if (bmi > end) { //end -100
                        if (75 + (bmi - end) >= 100) {
                            binding.bmiLayout.seekbar.setProgress(100f)
                        } else {
                            binding.bmiLayout.seekbar.setProgress((75 + (bmi - end)).toFloat())
                        }
                    } else {
                        binding.bmiLayout.seekbar.setProgress(100f)
                    }
                }

                binding.bodyFatLayout.bodyFatLayout.visibility = View.GONE
                binding.muscleLayout.muscleLayout.visibility = View.GONE
                binding.stoneLayout.stoneLayout.visibility = View.GONE
            }

            detail?.apply {
                binding.tvstyle.text = bodyStyleText
                binding.tvStyleDetail.text = bodyAdviceText
                mDetailAdapter.addData(labelList)

                if (labelList.size < 2) {
                    binding.llDetailLayout.visibility = View.GONE
                    binding.llNoDataLayout.visibility = View.VISIBLE
                } else {
                    binding.llDetailLayout.visibility = View.VISIBLE
                    binding.llNoDataLayout.visibility = View.GONE
                }

                labelList.forEach {
                    when (it.label) {
                        "BMI" -> {
                            var start = 0.0
                            var milldle = 0.0
                            var end = 0.0
                            if (it.levelIntervalList.size > 3) {
                                val first = it.levelIntervalList[0]
                                val second = it.levelIntervalList[1]
                                val three = it.levelIntervalList[2]
                                start = first.endValue
                                milldle = second.endValue
                                end = three.endValue
                            }

                            binding.bmiLayout.tvStartValue.text = "$start"
                            binding.bmiLayout.tvMidlleValue.text = "$milldle"
                            binding.bmiLayout.tvEndValue.text = "$end"

                            //0-start
                            if (it.labelValue <= start) {
                                binding.bmiLayout.seekbar.setProgress((25 * it.labelValue / start).toFloat())
                            } else if (it.labelValue <= milldle) {  //start - millde
                                binding.bmiLayout.seekbar.setProgress(((25 * (it.labelValue - start) / (milldle - start) + 25).toFloat()))
                            } else if (it.labelValue <= end) { //millde -end
                                binding.bmiLayout.seekbar.setProgress(((25 * (it.labelValue - milldle) / (end - milldle) + 50).toFloat()))
                            } else if (it.labelValue > end) { //end -100
                                if (75 + (it.labelValue - end) >= 100) {
                                    binding.bmiLayout.seekbar.setProgress(100f)
                                } else {
                                    binding.bmiLayout.seekbar.setProgress((75 + (it.labelValue - end)).toFloat())
                                }
                            } else {
                                binding.bmiLayout.seekbar.setProgress(100f)
                            }
                        }

                        "体脂率" -> {
                            var start: Double = 0.0
                            var milldle: Double = 0.0
                            var end: Double = 0.0
                            if (it.levelIntervalList.size > 3) {
                                val first = it.levelIntervalList[0]
                                val second = it.levelIntervalList[1]
                                val three = it.levelIntervalList[2]
                                start = first.endValue
                                milldle = second.endValue
                                end = three.endValue
                            }
                            binding.bodyFatLayout.tvStartValue.text = "$start${it.labelUnit}"
                            binding.bodyFatLayout.tvMidlleValue.text = "$milldle${it.labelUnit}"
                            binding.bodyFatLayout.tvEndValue.text = "$end${it.labelUnit}"

                            //0-start
                            if (it.labelValue <= start) {
                                binding.bodyFatLayout.seekbar.setProgress((25 * it.labelValue / start).toFloat())
                            } else if (it.labelValue <= milldle) {  //start - millde
                                binding.bodyFatLayout.seekbar.setProgress(
                                    (25 * (it.labelValue - start)
                                            / (milldle - start) + 25).toFloat()
                                )
                            } else if (it.labelValue <= end) { //millde -end
                                binding.bodyFatLayout.seekbar.setProgress(
                                    (25 * (it.labelValue - milldle)
                                            / (end - milldle) + 50).toFloat()
                                )
                            } else if (it.labelValue > end) { //end -100
                                if (85 + (it.labelValue - end) >= 100) {
                                    binding.bodyFatLayout.seekbar.setProgress(100f)
                                } else {
                                    binding.bodyFatLayout.seekbar.setProgress((85 + (it.labelValue - end)).toFloat())
                                }
                            } else {
                                binding.bodyFatLayout.seekbar.setProgress(100f)
                            }
                        }

                        "肌肉量" -> {
                            var start: Double = 0.0
                            var end: Double = 0.0
                            if (it.levelIntervalList.size > 2) {
                                val first = it.levelIntervalList[0]
                                val second = it.levelIntervalList[1]
                                start = first.endValue
                                end = second.endValue
                            }

                            binding.muscleLayout.tvStartValue.text = "$start${it.labelUnit}"
                            binding.muscleLayout.tvEndValue.text = "$end${it.labelUnit}"


                            if (it.labelValue <= start) {
                                binding.muscleLayout.seekbar.setProgress((33 * it.labelValue / start).toFloat())
                            } else if (it.labelValue <= end) { //millde -end
                                binding.muscleLayout.seekbar.setProgress((33 * (it.labelValue - start)
                                        / (end - start) + 33).toFloat())
                            } else if (it.labelValue > end) { //end -100
                                if (66 + (it.labelValue - end) >= 100) {
                                    binding.muscleLayout.seekbar.setProgress(100f)
                                } else {
                                    binding.muscleLayout.seekbar.setProgress((66 + (it.labelValue - end)).toFloat())
                                }
                            } else {
                                binding.muscleLayout.seekbar.setProgress(100f)
                            }
                        }

                        "骨量" -> {
                            var middle: Double = 0.0
                            if (it.levelIntervalList.size > 1) {
                                val first = it.levelIntervalList[0]
                                middle = first.endValue
                            }
                            binding.stoneLayout.tvMidlleValue.text = "$middle${it.labelUnit}"

                            binding.stoneLayout.seekbar.setProgress(((it.labelValue) / (middle * 2) * 100).toFloat())
                        }
                    }
                }
            }
        }
    }
}