package com.zj365.health.fragment.bloodlipid

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.TextView
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.zj365.health.R
import com.zj365.health.act.exception.BaseExceptionDataFragment
import com.zj365.health.vm.ExceptionVm
import com.xty.network.model.ExceptionBean
import java.text.SimpleDateFormat

class BloodFatExceptionFrag: BaseExceptionDataFragment<ExceptionVm>() {
    override fun getUnit(): String {
        return "mmol/L"
    }

    override fun getHint(): String {
        return when (binding.tvListTitle.text) {
            "总胆固醇" -> {
                "正常范围：2.83-5.20mmol/L"
            }

            "甘油三酯" -> {
                "正常范围：0.45-1.69mmol/L"
            }

            "高密度脂蛋白" -> {
                "正常范围：0.93-1.93mmol/L"
            }

            else -> {
                "正常范围：0-3.12mmol/L"
            }
        }
    }

    override fun setLabelCount(leftAxis: YAxis) {
        leftAxis.setLabelCountAndMaxMinValue(6, false, 0f, 8f)
    }

    override fun getMarkView(): IMarker? {
        return CustomMarkerViewXuezhi(
            this.requireContext(),
            R.layout.custom_marker_view_xuezhi, result
        )
    }

    @SuppressLint("ViewConstructor")
    class CustomMarkerViewXuezhi(
        context: Context?,
        layoutResource: Int,
        var result: MutableList<ExceptionBean>
    ) : MarkerView(context, layoutResource) {

        private val mXueZhiData = findViewById<View>(R.id.tvXueZhiData) as TextView
        private val mXueZhiTime = findViewById<View>(R.id.tvXueZhiTime) as TextView

        @SuppressLint("SimpleDateFormat", "SetTextI18n")
        override fun refreshContent(e: Entry, highlight: Highlight) {
            // 在 refreshContent 方法中设置需要显示的内容。
            val barEntry = e as BarEntry
            val x = barEntry.x
            val get = result[x.toInt()]
            val data = get.data
            mXueZhiData.text = data
            mXueZhiTime.text = RxTimeTool.milliseconds2String(
                get.time.toLong() * 1000,
                SimpleDateFormat("HH :mm")
            )
            setOffset((-width / 2).toFloat(), (-height).toFloat() - 10f)
        }

        init {
            // 在构造函数中获取 MarkerView 中的 TextView。
        }
    }

    override fun getRangeMaxmum(): Float {
        return 100f
    }

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