package com.zj365.health.act.exception

import android.content.Context
import android.graphics.DashPathEffect
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
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.vm.ExceptionVm
import com.xty.network.model.ExceptionBean
import java.text.SimpleDateFormat

/**
 *  体温异常数据
 */
@Route(path = ARouterUrl.TIWEN_EXCEPTION_DATA_ACT)
class TiwenExceptionDataAct : BaseExceptionDataAct<ExceptionVm>() {
    override fun getUnit(): String {
        return "℃"
    }

    override fun initYAxis(leftAxis:YAxis) {
        super.initYAxis(leftAxis)
        leftAxis.setLabelCountAndMaxMinValue(6, true, 35f, 40f)
        leftAxis.textColor = ContextCompat.getColor(this@TiwenExceptionDataAct, R.color.col_92a)
        leftAxis.setDrawAxisLine(false)
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
        leftAxis.gridColor = ContextCompat.getColor(this@TiwenExceptionDataAct, R.color.col_92a)
        leftAxis.setGridDashedLine(DashPathEffect(floatArrayOf(5f, 5f), 0f))
        leftAxis.setDrawGridLines(true)  //绘制横者的x轴
        leftAxis.setDrawZeroLine(true)
    }
    override fun getHint(): String {
        return "正常体温：35.0-37.2℃"
    }

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

    override fun getMarkView(): IMarker? {
        return CustomMarkerViewTIWEN(
            this,
            R.layout.custom_marker_view_tiwen,result)
    }
    class CustomMarkerViewTIWEN(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+"℃"
            mData.setText(data)

            mTime.setText(    RxTimeTool.milliseconds2String(
                get.time.toLong() * 1000,
                SimpleDateFormat("HH :mm")
            ))
            mData.gravity = Gravity.CENTER
            mTime.gravity = Gravity.CENTER
            setOffset((-getWidth() / 2).toFloat(), (-getHeight()).toFloat() - 10f);
        }

        init {
            // 在构造函数中获取 MarkerView 中的 TextView。
            mData = findViewById<View>(R.id.tvData) as TextView
            mTime = findViewById<View>(R.id.tvTime) as TextView
        }
    }
    override fun getRangeMaxmum(): Float {
        return 37.2f
    }

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