package com.jimind.sportdemo;

import android.graphics.Color;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

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.components.AxisBase;
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.CombinedData;
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.formatter.IAxisValueFormatter;
import com.jimind.sportdemo.adapter.TeacherAdapter;
import com.jimind.sportdemo.base.BaseActivity;
import com.jimind.sportdemo.bean.Bill;
import com.jimind.sportdemo.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 数据统计
 *
 * @author Any
 */
public class StatisticsActivity extends BaseActivity {

    @BindView(R.id.tv_bar_title)
    TextView tvBarTitle;
    @BindView(R.id.cl_statistics)
    ConstraintLayout clTitleBar;
    @BindView(R.id.school_chart)
    LineChart schoolChart;
    @BindView(R.id.rlv_teacher)
    RecyclerView rlvTeacher;
    @BindView(R.id.grade_chart)
    HorizontalBarChart gradeChart;
    @BindView(R.id.class_chart)
    HorizontalBarChart classChart;
    @BindView(R.id.tv_data_date)
    TextView tvDataDate;

    private int textColor, lineColor;
    private TeacherAdapter mTeacherAdapter;
    private BarDataSet mGradeSet, mClassSet;
    private LineDataSet mCourseSet, mLoadSet, mRateSet;
    private List<BarEntry> mGradeEntry = new ArrayList<>(), mClassEntry = new ArrayList<>();
    private List<Entry> mCourseEntry = new ArrayList<>(), mLoadEntry = new ArrayList<>(), mRateEntry = new ArrayList<>();

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_statistics;
    }

    @Override
    protected void initView() {
        CommonUtils.initTitleBar(this, clTitleBar);
        tvBarTitle.setText("数据统计");
        textColor = ContextCompat.getColor(this, R.color.lengendColor);
        lineColor = ContextCompat.getColor(this, R.color.highLightColor);
        rlvTeacher.setLayoutManager(new LinearLayoutManager(this));
        mTeacherAdapter = new TeacherAdapter(this);
        rlvTeacher.setAdapter(mTeacherAdapter);

        initLineChart();
        initGradeChart();
        initClassChart();
    }


    private void initClassChart() {
        classChart.getDescription().setEnabled(false);
        classChart.setDoubleTapToZoomEnabled(false);
        classChart.getLegend().setEnabled(false);
        classChart.setTouchEnabled(false);
        classChart.setExtraOffsets(20f, 0f, 20f, 20f);

        classChart.getAxisLeft().setEnabled(false);
        XAxis xAxis = classChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextColor(textColor);
        xAxis.setTextSize(12f);
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMinimum(-0.5f);
        xAxis.setAxisMaximum(6.5f);
        xAxis.setLabelCount(7);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int xValue = (int) value;
                if (xValue >= 0 && xValue < 7) {
                    return "1年级" + String.valueOf(xValue + 1) + "班";
                }
                return null;
            }
        });

        YAxis leftAxis = classChart.getAxisRight();
        leftAxis.setTextColor(textColor);
        leftAxis.setTextSize(12f);
        leftAxis.setDrawGridLines(true);
        leftAxis.setDrawAxisLine(false);
        leftAxis.setAxisMinimum(5f);
        leftAxis.setAxisMaximum(30f);
        leftAxis.setLabelCount(6, true);
        leftAxis.enableGridDashedLine(3f, 3f, 0);
    }

    private void initGradeChart() {
        gradeChart.getDescription().setEnabled(false);
        gradeChart.setDoubleTapToZoomEnabled(false);
        gradeChart.getLegend().setEnabled(false);
        gradeChart.setTouchEnabled(false);
        gradeChart.setExtraOffsets(20f, 0f, 20f, 20f);

        gradeChart.getAxisLeft().setEnabled(false);
        XAxis xAxis = gradeChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextColor(textColor);
        xAxis.setTextSize(12f);
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMinimum(-0.5f);
        xAxis.setAxisMaximum(5.5f);
        xAxis.setLabelCount(6);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int xValue = (int) value;
                if (xValue >= 0 && xValue < 6) {
                    return String.valueOf(xValue + 1) + "年级";
                }
                return null;
            }
        });

        YAxis leftAxis = gradeChart.getAxisRight();
        leftAxis.setTextColor(textColor);
        leftAxis.setTextSize(12f);
        leftAxis.setDrawGridLines(true);
        leftAxis.setDrawAxisLine(false);
        leftAxis.setAxisMinimum(5f);
        leftAxis.setAxisMaximum(30f);
        leftAxis.setLabelCount(6, true);
        leftAxis.enableGridDashedLine(3f, 3f, 0);
    }

    private void initLineChart() {
        schoolChart.getDescription().setEnabled(false);
        schoolChart.setDoubleTapToZoomEnabled(false);
        schoolChart.getLegend().setEnabled(true);
        schoolChart.setTouchEnabled(false);
        schoolChart.setExtraOffsets(20f, 0f, 20f, 20f);

        Legend legend = schoolChart.getLegend();
        legend.setTextSize(12f);
        legend.setTextColor(textColor);
        legend.setForm(Legend.LegendForm.LINE);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);

        XAxis xAxis = schoolChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextColor(textColor);
        xAxis.setTextSize(12f);
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMinimum(0f);
        xAxis.setAxisMaximum(4f);
        xAxis.setLabelCount(5, true);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int xValue = (int) value;
                if (xValue >= 0 && xValue < 5) {
                    return "周" + String.valueOf(xValue + 1);
                }
                return null;
            }
        });

        YAxis rightAxis = schoolChart.getAxisRight();
        rightAxis.setTextColor(textColor);
        rightAxis.setTextSize(12f);
        rightAxis.setDrawGridLines(true);
        rightAxis.setDrawAxisLine(false);
        rightAxis.setAxisMinimum(0.5f);
        rightAxis.setAxisMaximum(3f);
        rightAxis.setLabelCount(5, true);
        rightAxis.enableGridDashedLine(5f, 5f, 0);

        YAxis leftAxis = schoolChart.getAxisLeft();
        leftAxis.setTextColor(textColor);
        leftAxis.setTextSize(12f);
        leftAxis.setDrawGridLines(false);
        leftAxis.setDrawAxisLine(false);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(100f);
        leftAxis.setLabelCount(5, true);
    }

    @Override
    protected void initData() {
        List<Bill> billList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Bill bill = new Bill();
            bill.setName("王明");
            bill.setLoad("1.7");
            bill.setDensity("60%");
            billList.add(bill);
        }
        mTeacherAdapter.setDataList(billList);

        for (int i = 0; i < 5; i++) {
            float val1 = (float) (Math.random() * 100);
            float val2 = (float) (Math.random() * 100);
            float val3 = (float) (Math.random() * 2);
            float val4 = (float) (Math.random() * 30);
            float val5 = (float) (Math.random() * 30);
            mCourseEntry.add(new Entry(i, val1));
            mLoadEntry.add(new Entry(i, val2));
            mRateEntry.add(new Entry(i, val3));
            mGradeEntry.add(new BarEntry(i, val4));
            mClassEntry.add(new BarEntry(i, val5));
        }

        mGradeEntry.add(new BarEntry(5, 25));
        mClassEntry.add(new BarEntry(5, 15));
        mClassEntry.add(new BarEntry(6, 25));

        mCourseSet = new LineDataSet(mCourseEntry, "课程数");
        mCourseSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        mCourseSet.setColor(lineColor);
        mCourseSet.setDrawCircles(false);
        mCourseSet.setDrawValues(false);
        mCourseSet.setLineWidth(2f);

        mLoadSet = new LineDataSet(mLoadEntry, "运动负荷");
        mLoadSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        mLoadSet.setColor(Color.parseColor("#1890FF"));
        mLoadSet.setDrawCircles(false);
        mLoadSet.setDrawValues(false);
        mLoadSet.setLineWidth(2f);

        mRateSet = new LineDataSet(mRateEntry, "完成率");
        mRateSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
        mRateSet.setColor(Color.parseColor("#9013FE"));
        mRateSet.setDrawCircles(false);
        mRateSet.setDrawValues(false);
        mRateSet.setLineWidth(2f);

        LineData lineData = new LineData(mCourseSet, mLoadSet, mRateSet);
        schoolChart.setData(lineData);
        schoolChart.invalidate();

        //柱状图
        mGradeSet = new BarDataSet(mGradeEntry, "");
        mGradeSet.setColor(lineColor);
        mGradeSet.setDrawValues(false);
        mGradeSet.setAxisDependency(YAxis.AxisDependency.LEFT);

        mClassSet = new BarDataSet(mClassEntry, "");
        mClassSet.setColor(lineColor);
        mClassSet.setDrawValues(false);
        mClassSet.setAxisDependency(YAxis.AxisDependency.LEFT);

        BarData gradeData = new BarData(mGradeSet);
        gradeData.setBarWidth(0.6f);
        gradeChart.setData(gradeData);
        gradeChart.invalidate();

        BarData classData = new BarData(mClassSet);
        classData.setBarWidth(0.6f);
        classChart.setData(classData);
        classChart.invalidate();
    }

    @OnClick({R.id.ib_bar_back, R.id.rb_last, R.id.rb_current})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_bar_back:
                finish();
                break;
            case R.id.rb_last:
                break;
            case R.id.rb_current:
                break;
            default:
        }
    }

}
