package com.hong.job_management;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
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.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

import static com.hong.job_management.MainActivity.chinese;
import static com.hong.job_management.MainActivity.chinese_finish;
import static com.hong.job_management.MainActivity.biology;
import static com.hong.job_management.MainActivity.biology_finish;
import static com.hong.job_management.MainActivity.chemistry;
import static com.hong.job_management.MainActivity.chemistry_finish;
import static com.hong.job_management.MainActivity.english;
import static com.hong.job_management.MainActivity.english_finish;
import static com.hong.job_management.MainActivity.geography;
import static com.hong.job_management.MainActivity.geography_finish;
import static com.hong.job_management.MainActivity.history;
import static com.hong.job_management.MainActivity.history_finish;
import static com.hong.job_management.MainActivity.math;
import static com.hong.job_management.MainActivity.math_finish;
import static com.hong.job_management.MainActivity.other;
import static com.hong.job_management.MainActivity.other_finish;
import static com.hong.job_management.MainActivity.physics;
import static com.hong.job_management.MainActivity.physics_finish;
import static com.hong.job_management.MainActivity.politics;
import static com.hong.job_management.MainActivity.politics_finish;
import static com.hong.job_management.MainActivity.sum;
import static com.hong.job_management.MainActivity.sum_finish;

public class StatisticsActivity extends AppCompatActivity {

    private PieChart pic_chart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistics);
        initView();
    }

    private void initView(){
        pic_chart = (PieChart) findViewById(R.id.pic_chart);
        pic_chart.setUsePercentValues(true);//设置为显示百分比
        pic_chart.getDescription().setEnabled(false);//设置pieChart图表的描述
        pic_chart.setExtraOffsets(5,0,5,5);//设置pieChart图表上下左右的偏移，类似于外边距
        pic_chart.setDragDecelerationFrictionCoef(0.95f);//设置pieChart图表转动阻力摩擦系数[0,1]
        pic_chart.animateY(1400, Easing.EasingOption.EaseInOutBack);//设置pieChart图表展示动画效果

        //设置圆环内部属性
        pic_chart.setDrawCenterText(true);//是否绘制PieChart内部中心文本（true：下面属性才有意义）
        pic_chart.setCenterText("作业统计");//设置PieChart内部圆文字的内容
        pic_chart.setCenterTextSize(27);
        //pic_chart.setCenterTextTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//设置PieChart内部圆文字的字体样式
        pic_chart.setDrawHoleEnabled(true);//是否显示PieChart内部圆环(true:下面属性才有意义)
        pic_chart.setHoleColor(Color.WHITE);//设置PieChart内部圆的颜色
        pic_chart.setTransparentCircleColor(Color.WHITE);//设置PieChart内部透明圆与内部圆间距的填充颜色
        pic_chart.setTransparentCircleAlpha(110);//设置PieChart内部透明圆与内部圆间距透明度[0~255]数值越小越透明
        pic_chart.setHoleRadius(58f);//设置PieChart内部圆的半径
        pic_chart.setTransparentCircleRadius(61f);//设置PieChart内部透明圆的半径

        pic_chart.setRotationAngle(0);//设置pieChart图表起始角度
        // 触摸旋转
        pic_chart.setRotationEnabled(true);//设置pieChart图表是否可以手动旋转
        pic_chart.setHighlightPerTapEnabled(true);//设置pieChart图表点击Item高亮是否可用

        //模拟数据
        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
        entries.clear();
        PieEntry pieEntry =new PieEntry(3,"模板");
        if(chinese!=0)
            entries.add(new PieEntry(chinese,"语文"));
        if(math!=0)
            entries.add(new PieEntry(math, "数学"));
        if(english!=0)
            entries.add(new PieEntry(english, "英语"));
        if(biology!=0)
            entries.add(new PieEntry(biology, "生物"));
        if(chemistry!=0)
            entries.add(new PieEntry(chemistry, "化学"));
        if(physics!=0)
            entries.add(new PieEntry(physics, "物理"));
        if(history!=0)
            entries.add(new PieEntry(history, "历史"));
        if(politics!=0)
            entries.add(new PieEntry(politics, "政治"));
        if(geography!=0)
            entries.add(new PieEntry(geography, "地理"));
        if(other!=0)
        entries.add(new PieEntry(other, "其他"));

        //pieEntry.setData(10); //重复传入同个科目时重新设置其值
        //pieEntry.setLabel("chensh");
        //entries.set(0,pieEntry);

        //设置数据
        setDate(entries);

        // 获取pieChart图列
        Legend l=pic_chart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);

        // 输入标签样式
        pic_chart.setEntryLabelColor(Color.WHITE);
        pic_chart.setEntryLabelTextSize(16f);
    }

    private void setDate(ArrayList<PieEntry> entries) {
        PieDataSet dataSet = new PieDataSet(entries, "科目类别");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);

        //数据和颜色
        ArrayList<Integer> colors = new ArrayList<Integer>();
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);
        colors.add(ColorTemplate.getHoloBlue());
        dataSet.setColors(colors);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(14f);
        data.setValueTextColor(Color.WHITE);
        pic_chart.setData(data);
        pic_chart.highlightValues(null);
        //刷新
        pic_chart.invalidate();
    }
}