package com.zj365.health.weight

import android.annotation.SuppressLint
import android.content.Context
import android.widget.TextView
import com.github.mikephil.charting.components.MarkerView
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.highlight.Highlight
import com.github.mikephil.charting.utils.MPPointF
import com.zj365.health.R
import java.text.DecimalFormat

/**
 * 对应第几页
 * @param page 0:心率 1：血压 2：血氧 3：体温 4：睡眠 5：呼吸率 6：血糖
 */
@SuppressLint("ViewConstructor")
class ICompareMarkLineView(
    context: Context?,
    layoutResource: Int,
    var unit: String,
    var page: MarkPage,
    var xzhiType: Int = 1//1：总胆固醇，2：甘油三酯，3：高密度蛋白，4：低密度蛋白
) : MarkerView(context, layoutResource) {

    enum class MarkPage(val title: String) {
        /**血压**/
        XYa("血压"),

        /**血氧**/
        XYang("血氧"),

        /**心率**/
        XLv("心率"),

        /**体温**/
        Temp("体温"),

        /**睡眠**/
        Sleep("睡眠"),

        /**呼吸率**/
        Breath("呼吸率"),

        /**血糖**/
        XTang("血糖"),

        UricAcid("尿酸"),

        /**睡眠----天**/
        Sleep_Day("睡眠-天"),

        WEIGHT("体重"),

        /**单独折线图**/
        TShu("特殊"),

        /**BMI折线图**/
        Bmi("Bmi"),

        /**血脂**/
        XZhi("血脂"),

        /**体脂**/
        PBF("pbf")
    }

    var tvValue: TextView
    var tvTime: TextView
    var df: DecimalFormat = DecimalFormat("0.00")

    init {
        tvTime = findViewById(R.id.mTvTime)
        tvValue = findViewById(R.id.mTvValue)
    }

    /**
     * 保留小数位
     */
    private fun changeDecimals(str: String, number: Int): String {
        return String.format("%.${number}f", str.toFloat())
    }

    @SuppressLint("SetTextI18n")
    override fun refreshContent(e: Entry?, highlight: Highlight?) {
        when (page) {
            MarkPage.XZhi -> {
                val datas = e!!.data as MutableList<String>
                val day = datas[0]
                val time = datas[1]
                tvTime.text = "$day $time"
                //1：总胆固醇，2：甘油三酯，3：高密度蛋白，4：低密度蛋白
                when (xzhiType) {
                    1 -> {
                        val tc = datas[4]
                        if (tc.toFloat().isNaN() || tc.toFloat() == 0f) {
                            tvValue.text = "--"
                        } else {
                            tvValue.text = "${changeDecimals(tc, 1)}$unit"
                        }
                    }

                    2 -> {
                        val tc = datas[5]
                        if (tc.toFloat().isNaN() || tc.toFloat() == 0f) {
                            tvValue.text = "--"
                        } else {
                            tvValue.text = "${changeDecimals(tc, 1)}$unit"
                        }
                    }

                    3 -> {
                        val tc = datas[2]
                        if (tc.toFloat().isNaN() || tc.toFloat() == 0f) {
                            tvValue.text = "--"
                        } else {
                            tvValue.text = "${changeDecimals(tc, 1)}$unit"
                        }
                    }

                    4 -> {
                        val tc = datas[3]
                        if (tc.toFloat().isNaN() || tc.toFloat() == 0f) {
                            tvValue.text = "--"
                        } else {
                            tvValue.text = "${changeDecimals(tc, 1)}$unit"
                        }
                    }
                }
            }

            MarkPage.TShu -> {
                val datas = e!!.data as MutableList<String>
                val day = datas[0]
                val time = datas[1]
                tvTime.text = "$day $time"
                if (datas[2].toFloat().isNaN() || datas[2].toFloat() == 0f) {
                    tvValue.text = "--"
                } else {
                    tvValue.text = "${changeDecimals(datas[2], 1)}$unit"
                }
            }

            MarkPage.WEIGHT ->{
                val datas = e!!.data as MutableList<String>
                val day = datas[0]
                val time = datas[1]
                tvTime.text = "$day $time"
                if (datas[2].toFloat().isNaN() || datas[2].toFloat() == 0f) {
                    tvValue.text = "--"
                } else {
                    tvValue.text = "${changeDecimals(datas[2], 2)}$unit"
                }
            }

            MarkPage.Bmi -> {
                val datas = e!!.data as MutableList<String>
                val day = datas[0]
                val time = datas[1]
                tvTime.text = "$day $time"
                if (datas[3].toFloat().isNaN() || datas[3].toFloat() == 0f) {
                    tvValue.text = "--"
                } else {
                    tvValue.text = "${changeDecimals(datas[3], 1)}$unit"
                }
            }

            MarkPage.PBF -> {
                val datas = e!!.data as MutableList<String>
                val day = datas[0]
                val time = datas[1]
                tvTime.text = "$day $time"
                if (datas[3].toFloat().isNaN() || datas[4].toFloat() == 0f) {
                    tvValue.text = "--"
                } else {
                    tvValue.text = "${changeDecimals(datas[4], 1)}$unit"
                }
            }

            else -> {
                val datas = e!!.data as MutableList<String>
                tvTime.text = "${datas[0]}"
                if (datas[2].toFloat() == 0f) {
                    tvValue.text = "--"
                } else {
                    tvValue.text = "${changeDecimals(datas[2], 0)}$unit"
                }
            }
        }
        super.refreshContent(e, highlight)
    }

    override fun getOffset(): MPPointF {
        return MPPointF((-(width / 2)).toFloat(), (-height).toFloat())
    }

    private val STOKE_WIDTH = 5f //这里对于stroke_width的宽度也要做一定偏移

    override fun getOffsetForDrawingAtPoint(posX: Float, posY: Float): MPPointF? {
        val offset = offset
        val chart = chartView
        val width = width.toFloat()
        val height = height.toFloat()
        // posY \posX 指的是markerView左上角点在图表上面的位置
        //处理Y方向
        if (posY <= height) { // 如果点y坐标小于markerView的高度，如果不处理会超出上边界，处理了之后这时候箭头是向上的，我们需要把图标下移一个箭头的大小
            offset.y = 0f
        } else { //否则属于正常情况，因为我们默认是箭头朝下，然后正常偏移就是，需要向上偏移markerView高度和arrow size，再加一个stroke的宽度，因为你需要看到对话框的上面的边框
            offset.y = -height - STOKE_WIDTH // 40 arrow height   5 stroke width
        }
        //处理X方向，分为3种情况，1、在图表左边 2、在图表中间 3、在图表右边（不考虑超出屏幕问题）
        offset.x = 0f
        if (posX > chart.width - width) {//如果超过右边界，则向左偏移markerView的宽度
            offset.x = -width;
        } else {//默认情况，不偏移（因为是点是在左上角）
            offset.x = 0f
            if (posX > width / 2) {//如果大于markerView的一半，说明箭头在中间，所以向右偏移一半宽度
                offset.x = -(width / 2);
            }
        }
        return offset
    }
}

