package com.jimind.sportdemo;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

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.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.BillAdapter;
import com.jimind.sportdemo.api.ApiManager;
import com.jimind.sportdemo.base.BaseActivity;
import com.jimind.sportdemo.base.BaseObserver;
import com.jimind.sportdemo.base.ResponseResult;
import com.jimind.sportdemo.bean.Bill;
import com.jimind.sportdemo.bean.Course;
import com.jimind.sportdemo.utils.AppManager;
import com.jimind.sportdemo.utils.CommonUtils;
import com.jimind.sportdemo.utils.Constant;

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

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.schedulers.Schedulers;

/**
 * 课程总结
 *
 * @author Any
 */
public class SummaryActivity extends BaseActivity {

    @BindView(R.id.tv_title_label)
    TextView tvTitleLabel;
    @BindView(R.id.tv_class_course)
    TextView tvClassCourse;
    @BindView(R.id.tv_summary_density)
    TextView tvSummaryDensity;
    @BindView(R.id.tv_summary_strength)
    TextView tvSummaryStrength;
    @BindView(R.id.tv_summary_rate)
    TextView tvSummaryRate;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.chart_hear_beat)
    LineChart heartBeatChart;
    @BindView(R.id.rlv_bill)
    RecyclerView rlvBill;

    private BillAdapter mBillAdapter;
    private LineDataSet mStepSet, mHeartSet;
    private List<Entry> mStepEntry = new ArrayList<>(), mHeartEntry = new ArrayList<>();

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

    @Override
    protected void initView() {
        CommonUtils.initTitle(this, tvTitleLabel);
        int textColor = ContextCompat.getColor(this, R.color.lengendColor);
        rlvBill.setLayoutManager(new LinearLayoutManager(this));
        mBillAdapter = new BillAdapter(this);
        rlvBill.setAdapter(mBillAdapter);

        //设置图表参数
        heartBeatChart.getDescription().setEnabled(false);
        heartBeatChart.setDoubleTapToZoomEnabled(false);
        heartBeatChart.getLegend().setEnabled(true);
        heartBeatChart.setTouchEnabled(false);
        heartBeatChart.setExtraOffsets(30f, 0f, 30f, 20f);

        Legend legend = heartBeatChart.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);

        final XAxis xAxis = heartBeatChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextColor(textColor);
        xAxis.setTextSize(12f);
        xAxis.setDrawGridLines(false);
        xAxis.setLabelCount(6, true);
        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 * 10 + 10) + "分钟";
                }
                return null;
            }
        });

        YAxis rightAxis = heartBeatChart.getAxisRight();
        rightAxis.setTextColor(textColor);
        rightAxis.setTextSize(12f);
        rightAxis.setDrawGridLines(true);
        rightAxis.setDrawAxisLine(false);
        rightAxis.setAxisMinimum(40f);
        rightAxis.setAxisMaximum(180f);
        rightAxis.setLabelCount(8, true);
        rightAxis.enableGridDashedLine(5f, 5f, 0);

        YAxis leftAxis = heartBeatChart.getAxisLeft();
        leftAxis.setTextColor(textColor);
        leftAxis.setTextSize(12f);
        leftAxis.setDrawGridLines(false);
        leftAxis.setDrawAxisLine(false);
        leftAxis.setAxisMinimum(200f);
        leftAxis.setAxisMaximum(1600f);
        leftAxis.setLabelCount(8, true);
    }

    @Override
    protected void initData() {
        Intent intent = this.getIntent();
        String className = intent.getStringExtra(Constant.CLASS_NAME);
        int totalNumber = intent.getIntExtra(Constant.TOTAL_NUMBER, 0);
        Course course = (Course) intent.getSerializableExtra(Constant.COURSE);

        String gradeClass = course.getGradeName() + className;
        String teacherName = MyApplication.getInstance().getTeacherName();
        tvClassCourse.setText("年级班级：" + gradeClass + "\t课程：" + course.getCourseName());
        tvRemark.setText("老师：" + teacherName + "\t人数：" + totalNumber + "人\t时间： 40分钟");

        int lineColor = ContextCompat.getColor(this, R.color.highLightColor);
        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);
        }
        mBillAdapter.setDataList(billList);

        for (int i = 0; i < 6; i++) {
            float val1 = (float) (Math.random() * 100 + 40);
            float val2 = (float) (Math.random() * 1000);
            mHeartEntry.add(new Entry(i, val1));
            mStepEntry.add(new Entry(i, val2));
        }

        mStepSet = new LineDataSet(mStepEntry, "步数");
        mStepSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        mStepSet.setColor(lineColor);
        mStepSet.setDrawCircles(false);
        mStepSet.setDrawValues(false);
        mStepSet.setLineWidth(2f);

        mHeartSet = new LineDataSet(mHeartEntry, "心跳");
        mHeartSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
        mHeartSet.setColor(Color.parseColor("#9013FE"));
        mHeartSet.setDrawCircles(false);
        mHeartSet.setDrawValues(false);
        mHeartSet.setLineWidth(2f);

        LineData lineData = new LineData(mStepSet, mHeartSet);
        heartBeatChart.setData(lineData);
        heartBeatChart.invalidate();
    }

    @OnClick(R.id.bt_over)
    public void onClick() {
        try {
            CourseDescActivity.mActivity.finish();
        } catch (ConcurrentModificationException e) {
            e.printStackTrace();
        }
        finish();
        sendToService();
    }

    /**
     * 传给服务器
     */
    private void sendToService() {
        ApiManager.getInstance().requestService
                .sendCommand(Constant.RETURN, "", "")
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(new BaseObserver<ResponseResult>() {
                    @Override
                    protected void onSuccess(ResponseResult responseResult) {
                    }

                    @Override
                    protected void onFailure(int code, String message) {

                    }
                });
    }
}
