package com.casic.dcms.utils;


import android.graphics.Color;

import com.casic.dcms.R;
import com.casic.dcms.base.BaseApplication;
import com.casic.dcms.widgets.DetailsMarkerView;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.pengxh.androidx.lite.kit.IntKit;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author a203
 * @description 各种图初始化配置类
 * @package com.casic.dcms.utils
 * @date 2022/2/15 16:24
 * @email 290677893@qq.com
 */
public class ChartViewHelper {

    private static final String TAG = "ChartViewHelper";
    private static final String[] labelName = {"普通", "快速", "三包"};

    private static void initPieChart(PieChart chart) {
        chart.setUsePercentValues(true); //百分比数字显示
        chart.getDescription().setEnabled(false);
        chart.setBackgroundColor(Color.WHITE);      //设置图表背景色
        chart.setRotationEnabled(true);
        chart.setHighlightPerTapEnabled(true);
        chart.animateY(1200, Easing.EaseInOutQuad);// 设置图表展示动画效果
        chart.setDrawEntryLabels(false); //不显示分类标签
        chart.setDrawCenterText(false);
        chart.setDrawHoleEnabled(true);
        chart.setEntryLabelColor(Color.BLUE);//图表文本字体颜色
        //设置图表上下左右的偏移，类似于外边距，可以控制饼图大小
        chart.setExtraOffsets(5, 5, 5, 5);
        //设置图例位置
        Legend legend = chart.getLegend();
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setWordWrapEnabled(true);
    }

    public static void setPieChartData(PieChart chart, List<PieEntry> entries) {
        initPieChart(chart);
        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setSliceSpace(0f);//设置饼状块之间的间隙
        //设置饼图块颜色
        List<Integer> colors = new ArrayList<>();
        for (int i = 0; i < entries.size(); i++) {
            colors.add(IntKit.convertColor(chart.getContext(), LocaleConstant.PIE_COLORS.get(i)));
        }
        dataSet.setColors(colors);
        //设置标签显示在饼图块外面
        dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setValueLineColor(Color.BLUE);
        PieData data = new PieData(dataSet);
        DecimalFormat decimalFormat = new DecimalFormat("##0.00");
        data.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return decimalFormat.format(value) + "%";
            }
        });
        data.setValueTextSize(12f);
        data.setValueTextColor(Color.BLUE); //数据值颜色
        chart.setData(data);
        chart.highlightValues(null);
        chart.invalidate();
    }

    private static void initBarChart(HorizontalBarChart chart) {
        chart.setDrawGridBackground(false);
        chart.setDrawBorders(false);
        chart.setScaleEnabled(false);
        //去掉描述
        chart.getDescription().setEnabled(false);
        //设置图例位置
        Legend legend = chart.getLegend();
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setWordWrapEnabled(true);

        XAxis xAxis = chart.getXAxis();
        xAxis.setTextColor(IntKit.convertColor(BaseApplication.get(), R.color.mainTextColor));
        xAxis.setTextSize(12f);
        xAxis.setDrawLabels(false);//绘制标签  指x轴上的对应数值
        xAxis.setDrawAxisLine(true);//是否绘制轴线
        xAxis.setDrawGridLines(false);//设置x轴上每个点对应的线
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //设置样式
        YAxis rightAxis = chart.getAxisRight();
        //设置图表右边的y轴禁用
        rightAxis.setEnabled(false);
        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setAxisMinimum(0f);
    }

    @SafeVarargs
    public static void setBarChartData(HorizontalBarChart chart, List<BarEntry>... barEntries) {
        initBarChart(chart);
        DecimalFormat decimalFormat = new DecimalFormat("##0");
        //绑定数据
        BarDataSet totalCheckBarDataSet = new BarDataSet(barEntries[0], "派发数");
        totalCheckBarDataSet.setColor(IntKit.convertColor(BaseApplication.get(), R.color.barChartColor));
        totalCheckBarDataSet.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return decimalFormat.format(value);
            }
        });

        BarDataSet checkedBarDataSet = new BarDataSet(barEntries[1], "处置数");
        checkedBarDataSet.setColor(IntKit.convertColor(BaseApplication.get(), R.color.onLineColor));
        checkedBarDataSet.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return decimalFormat.format(value);
            }
        });

        BarData barData = new BarData(totalCheckBarDataSet, checkedBarDataSet);
        barData.setBarWidth(0.3f);
        barData.groupBars(0f, 0.2f, 0);
        chart.setData(barData);
        chart.invalidate();
    }

    private static void initBarChart(BarChart chart) {
        chart.setDrawGridBackground(false);
        chart.setDrawBorders(false);
        chart.setScaleEnabled(false);
        //去掉描述
        chart.getDescription().setEnabled(false);
        //设置图例位置
        Legend legend = chart.getLegend();
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setWordWrapEnabled(true);

        XAxis xAxis = chart.getXAxis();
        xAxis.setTextColor(IntKit.convertColor(BaseApplication.get(), R.color.mainTextColor));
        xAxis.setTextSize(12f);
        xAxis.setDrawLabels(false);//绘制标签  指x轴上的对应数值
        xAxis.setDrawAxisLine(true);//是否绘制轴线
        xAxis.setDrawGridLines(false);//设置x轴上每个点对应的线
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //设置样式
        YAxis rightAxis = chart.getAxisRight();
        //设置图表右边的y轴禁用
        rightAxis.setEnabled(false);
        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setAxisMinimum(0f);
    }

    public static void setBarChartData(BarChart chart, List<BarEntry> barEntries) {
        initBarChart(chart);
        DecimalFormat decimalFormat = new DecimalFormat("##0");
        //绑定数据
        BarDataSet totalCheckBarDataSet = new BarDataSet(barEntries, "数量");
        totalCheckBarDataSet.setColor(IntKit.convertColor(BaseApplication.get(), R.color.barChartColor));
        totalCheckBarDataSet.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return decimalFormat.format(value);
            }
        });

        BarData barData = new BarData(totalCheckBarDataSet);
        barData.setBarWidth(0.5f);
        chart.setData(barData);
        chart.invalidate();
    }

    private static void initLineChart(LineChart chart, List<String> dates) {
        chart.setDrawGridBackground(false);
        chart.setDrawBorders(false);
        //设置样式
        YAxis rightAxis = chart.getAxisRight();
        //设置图表右边的y轴禁用
        rightAxis.setEnabled(false);
        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setAxisMinimum(0f);
        chart.setScaleYEnabled(false);//Y轴不可缩放
        //设置x轴
        XAxis xAxis = chart.getXAxis();
        xAxis.setTextColor(IntKit.convertColor(BaseApplication.get(), R.color.mainTextColor));
        xAxis.setTextSize(12f);
        xAxis.setAxisMinimum(1f);
        xAxis.setDrawLabels(true);//绘制标签  指x轴上的对应数值
        xAxis.setDrawAxisLine(true);//是否绘制轴线
        xAxis.setDrawGridLines(false);//设置x轴上每个点对应的线
        xAxis.setGranularity(1f);//禁止放大后x轴标签重绘
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return dates.get((int) value);
            }
        });
        xAxis.setLabelRotationAngle(45);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        chart.setExtraBottomOffset(5f);//解决X轴显示不完全问题
        //去掉描述
        chart.getDescription().setEnabled(false);
        //设置图例
        Legend legend = chart.getLegend();
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setWordWrapEnabled(true);
        //添加自定义Marker
        DetailsMarkerView detailsMarkerView = new DetailsMarkerView(BaseApplication.get(), dates);
        detailsMarkerView.setChartView(chart);
        chart.setMarker(detailsMarkerView);
    }

    @SafeVarargs
    public static void setLineChartData(LineChart chart, List<String> dates, List<Entry>... entries) {
        initLineChart(chart, dates);
        List<ILineDataSet> lineDataSets = new ArrayList<>();
        //设置数据
        LineDataSet registeredDataSet = new LineDataSet(entries[0], "立案数");
        registeredDataSet.setColor(IntKit.convertColor(BaseApplication.get(), R.color.punishColor));//线条颜色
        registeredDataSet.setCircleColor(IntKit.convertColor(BaseApplication.get(), R.color.punishColor));//圆点颜色
        lineDataSets.add(registeredDataSet);

        LineDataSet distributeDataSet = new LineDataSet(entries[1], "派发数");
        distributeDataSet.setColor(IntKit.convertColor(BaseApplication.get(), R.color.warnColor));//线条颜色
        distributeDataSet.setCircleColor(IntKit.convertColor(BaseApplication.get(), R.color.warnColor));//圆点颜色
        lineDataSets.add(distributeDataSet);

        LineDataSet processDataSet = new LineDataSet(entries[2], "处置数");
        processDataSet.setColor(IntKit.convertColor(BaseApplication.get(), R.color.persuadeColor));//线条颜色
        processDataSet.setCircleColor(IntKit.convertColor(BaseApplication.get(), R.color.persuadeColor));//圆点颜色
        lineDataSets.add(processDataSet);

        LineDataSet closedDataSet = new LineDataSet(entries[3], "结案数");
        closedDataSet.setColor(IntKit.convertColor(BaseApplication.get(), R.color.onLineColor));//线条颜色
        closedDataSet.setCircleColor(IntKit.convertColor(BaseApplication.get(), R.color.onLineColor));//圆点颜色
        lineDataSets.add(closedDataSet);

        LineData lineData = new LineData(lineDataSets);
        lineData.setDrawValues(false);
        chart.setData(lineData);
        chart.invalidate();
    }
}
