package com.casic.br.widgets

import android.content.Context
import android.widget.TextView
import com.casic.br.R
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 java.text.DecimalFormat
import java.util.*

class LineChartMarkerView(context: Context?) :
    MarkerView(context, R.layout.popu_line_chart_marker) {

    private val decimalFormat = DecimalFormat("##0.0")
    private val dayView: TextView = findViewById(R.id.dayView)
    private val dataView: TextView = findViewById(R.id.dataView)
    private var xAxisDate: MutableList<String> = ArrayList()

    fun setXAxisDate(date: MutableList<String>) {
        this.xAxisDate = date
    }

    //每次重绘，会调用此方法刷新数据
    override fun refreshContent(e: Entry, highlight: Highlight) {
        super.refreshContent(e, highlight)
        val data = e.data as String
        try {
            dataView.text =
                String.format(data + ":" + decimalFormat.format(e.y.toString().toDouble()))
            val date = xAxisDate[(e.x).toInt()]
            val split = date.split("/")
            dayView.text = String.format(Locale.CHINA, "${split[0]}月${split[1]}日")
        } catch (e: Exception) {
            e.printStackTrace()
        }
        super.refreshContent(e, highlight)
    }

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