package com.casic.br.utils

import com.casic.br.R
import com.casic.br.extensions.init
import com.casic.br.model.LineChartEntryModel
import com.casic.br.widgets.LineChartMarkerView
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.data.*
import com.github.mikephil.charting.formatter.ValueFormatter
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
import com.pengxh.kt.lite.extensions.convertColor
import java.text.DecimalFormat


/**
 * @author a203
 * @description 各种图初始化配置类
 * @date 2022/2/15 16:24
 * @email 290677893@qq.com
 */
object ChartViewHelper {
    /**
     * 单条折线图
     * */
    fun setSingleLineChartData(
        chart: LineChart, xAxisDate: MutableList<String>, entryModel: LineChartEntryModel
    ) {
        //每次加载数据都初始化折线图
        chart.init(chart.context)
        if (entryModel.entryList.isEmpty()) {
            return
        }
        //绑定数据
        val lineDataSets: MutableList<ILineDataSet> = ArrayList()
        //设置数据
        val dataSet = LineDataSet(entryModel.entryList, entryModel.entryList[0].data.toString())
        dataSet.setDrawCircles(true)
        //线条颜色
        dataSet.color = entryModel.lineColor
        //圆点颜色
        dataSet.setCircleColor(entryModel.lineColor)
        dataSet.setDrawFilled(true)
        dataSet.fillColor = entryModel.lineColor
        dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER
        lineDataSets.add(dataSet)

        val lineData = LineData(lineDataSets)
        lineData.setDrawValues(false)
        //添加自定义Marker
        val markerView = LineChartMarkerView(chart.context)
        markerView.chartView = chart
        markerView.setXAxisDate(xAxisDate)
        chart.marker = markerView
        //设置X轴坐标
        val xAxis = chart.xAxis
        if (xAxisDate.size == 1) {
            xAxis.valueFormatter = object : ValueFormatter() {
                override fun getFormattedValue(value: Float): String {
                    return xAxisDate[0]
                }
            }
        } else {
            xAxis.valueFormatter = object : ValueFormatter() {
                override fun getFormattedValue(value: Float): String {
                    return xAxisDate[value.toInt()]
                }
            }
        }
        chart.data = lineData
        chart.invalidate()
    }

    /**
     * 多条折线图
     * */
    fun setLineChartData(
        chart: LineChart, xAxisDate: MutableList<String>, entryModels: List<LineChartEntryModel>
    ) {
        //每次加载数据都初始化折线图
        chart.init(chart.context)
        if (entryModels[0].entryList.isEmpty()) {
            return
        }
        //绑定数据
        val lineDataSets: MutableList<ILineDataSet> = ArrayList()
        entryModels.forEachIndexed { index, it ->
            //设置数据
            val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString())
            dataSet.setDrawCircles(true)
            //线条颜色
            dataSet.color = it.lineColor
            //圆点颜色
            dataSet.setCircleColor(it.lineColor)
            dataSet.setDrawFilled(true)
            dataSet.fillColor = it.lineColor
            dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER
            lineDataSets.add(dataSet)
        }
        val lineData = LineData(lineDataSets)
        lineData.setDrawValues(false)
        //添加自定义Marker
        val markerView = LineChartMarkerView(chart.context)
        markerView.chartView = chart
        markerView.setXAxisDate(xAxisDate)
        chart.marker = markerView
        //设置X轴坐标
        val xAxis = chart.xAxis
        xAxis.valueFormatter = object : ValueFormatter() {
            override fun getFormattedValue(value: Float): String {
                return xAxisDate[value.toInt()]
            }
        }
        chart.data = lineData
        chart.invalidate()
    }

//    /**
//     * 竖直柱状图
//     * */
//    fun setBarChartData(
//        chart: BarChart,
//        xAxisLabel: MutableList<String>,
//        entries: List<BarEntry>,
//        dateType: String
//    ) {
//        //每次加载数据都初始化饼图
//        chart.init(chart.context, xAxisLabel)
//        val barDataSets: MutableList<IBarDataSet> = ArrayList()
//        //绑定数据
//        val dataSet = BarDataSet(entries, "")
//        dataSet.color = R.color.color_1.convertColor(chart.context)
//        dataSet.valueFormatter = object : ValueFormatter() {
//            override fun getFormattedValue(value: Float): String {
//                return DecimalFormat("##0").format(value.toDouble())
//            }
//        }
//        barDataSets.add(dataSet)
//        val barData = BarData(barDataSets)
//        barData.barWidth = 0.5f
//        //添加自定义Marker
//        val markerView = BarChartMarkerView(chart.context)
//        markerView.chartView = chart
//        markerView.setXAxisLabel(xAxisLabel)
//        markerView.setBarDataType(dateType)
//        chart.marker = markerView
//        chart.data = barData
//        chart.invalidate()
//    }
//
//    /**
//     * 横向柱状图
//     * */
//    fun setBarChartData(
//        chart: HorizontalBarChart,
//        xAxisLabel: MutableList<String>,
//        entries: List<BarEntry>
//    ) {
//        //每次加载数据都初始化饼图
//        chart.init(chart.context, xAxisLabel)
//        val barDataSets: MutableList<IBarDataSet> = ArrayList()
//        //绑定数据
//        val dataSet = BarDataSet(entries, "")
//        dataSet.color = R.color.color_6.convertColor(chart.context)
//        dataSet.valueFormatter = object : ValueFormatter() {
//            override fun getFormattedValue(value: Float): String {
//                return DecimalFormat("##0").format(value.toDouble())
//            }
//        }
//        barDataSets.add(dataSet)
//        val barData = BarData(barDataSets)
//        barData.barWidth = 0.5f
//        //添加自定义Marker
//        val markerView = BarChartMarkerView(chart.context)
//        markerView.chartView = chart
//        markerView.setXAxisLabel(xAxisLabel)
//        markerView.setBarDataType("")
//        chart.marker = markerView
//        chart.data = barData
//        chart.invalidate()
//    }

    /**
     * 饼图
     * */
    fun setPieChartData(chart: PieChart, entries: List<PieEntry?>) {
        //每次加载数据都初始化饼图
        chart.init(chart.context)
        if (entries.isEmpty()) {
            return
        }
        //绑定数据
        val dataSet = PieDataSet(entries, "")
        dataSet.sliceSpace = 1.5f //设置饼状块之间的间隙
        dataSet.selectionShift = 5f //设置饼状块被选中时突出的距离
        //设置饼图块颜色
        val colors: MutableList<Int> = ArrayList()
        for (i in entries.indices) {
            colors.add(LocaleConstant.COLORS[i].convertColor(chart.context))
        }
        dataSet.colors = colors
        //设置标签显示在饼图块外面
        dataSet.xValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
        dataSet.yValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
        dataSet.valueLineColor =
            R.color.mainThemeColor.convertColor(chart.context)
        dataSet.valueLinePart1Length = 0.55f //标签指示线前半截长度
        dataSet.valueLinePart2Length = 0.65f //标签指示线后半截长度
        val data = PieData(dataSet)
        data.setValueFormatter(object : ValueFormatter() {
            override fun getFormattedValue(value: Float): String {
                return DecimalFormat("##0.00").format(value.toDouble()) + chart.context.resources.getString(
                    R.string.volume_unit
                )
            }
        })
        data.setValueTextSize(12f)
        data.setValueTextColor(R.color.mainThemeColor.convertColor(chart.context)) //数据值颜色
        chart.data = data
        chart.highlightValues(null)
        chart.invalidate()
    }
}