package com.zj365.health.fragment.bloodlipid

import android.content.Context
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.github.mikephil.charting.components.IMarker
import com.github.mikephil.charting.components.MarkerView
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.highlight.Highlight
import com.tamsiree.rxkit.RxTimeTool
import com.xty.common.arouter.ARouterUrl
import com.zj365.health.R
import com.zj365.health.act.exception.BaseExceptionDataAct
import com.zj365.health.vm.ExceptionVm
import com.xty.network.model.ExceptionBean
import java.text.SimpleDateFormat

/**
 *  血脂异常数据
 */
@Route(path = ARouterUrl.BL_FAT_EXCEPTION_DATA)
class BloodFatExceptionDataAct : BaseExceptionDataAct<ExceptionVm>() {
    override fun getUnit(): String {
        return "mmol/L"
    }

    override fun getHint(): String {
        return if(binding.title.mTvTitle.text.toString() == "总胆固醇偏高"){
            "正常范围：2.83-5.20mmol/L"
        }else if(binding.title.mTvTitle.text.toString() == "甘油三酯偏高"){
            "正常范围：0.45-1.69mmol/L"
        }else if(binding.title.mTvTitle.text.toString() == "高密度脂蛋白偏低"){
            "正常范围：0.93-1.93mmol/L"
        }else if (binding.title.mTvTitle.text.toString() == "低密度脂蛋白偏高"){
            "正常范围：0-3.12mmol/L"
        }else{
            "正常范围：2.83-5.20mmol/L"
        }
    }

    override fun setLabelCount(leftAxis: YAxis) {
        list?.let {
            if (it.isNotEmpty()) {
                leftAxis.setLabelCountAndMaxMinValue(
                    6,
                    true,
                    0f,
                    it.maxBy { item -> item.toFloat() }.toFloat()
                )
            } else {
                leftAxis.setLabelCountAndMaxMinValue(6, true, 0f, 160f)
            }
        }
    }

    override fun getMarkView(): IMarker? {
        return CustomMarkerViewXBloodLipid(
            this,
            R.layout.custom_marker_view_blood_lipid, result
        )
    }

    class CustomMarkerViewXBloodLipid(
        context: Context?,
        layoutResource: Int,
        var result: MutableList<ExceptionBean>
    ) :
        MarkerView(context, layoutResource) {
        private val mData: TextView
        private val mTime: TextView
        override fun refreshContent(e: Entry, highlight: Highlight) {
            // 在 refreshContent 方法中设置需要显示的内容。
            val barEntry = e as BarEntry
            val x = barEntry.x
            val get = result.get(x.toInt())
            var data = get.data + "mmol/L"
            mData.setText(data)
            mTime.setText(
                RxTimeTool.milliseconds2String(
                    get.time.toLong() * 1000,
                    SimpleDateFormat("HH:mm")
                )
            )
            setOffset((-getWidth() / 2).toFloat(), (-getHeight()).toFloat() - 10f);
        }

        init {
            // 在构造函数中获取 MarkerView 中的 TextView。
            mData = findViewById<View>(R.id.tvDataBloodlipid) as TextView
            mTime = findViewById<View>(R.id.tvbloodFatTime) as TextView
        }
    }

    override fun getRangeMaxmum(): Float {

        return 100f
    }

    override fun getRangeMinmum(): Float {
        return 95f
    }
}