package com.casic.dcms.ui.fragment;

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

import com.casic.dcms.base.AndroidxBaseFragment;
import com.casic.dcms.bean.EventSourceBean;
import com.casic.dcms.bean.HighEventBean;
import com.casic.dcms.databinding.FragmentChartBinding;
import com.casic.dcms.mvp.presenter.impl.EventSourcePresenterImpl;
import com.casic.dcms.mvp.presenter.impl.HighEventPresenterImpl;
import com.casic.dcms.mvp.view.IEventSourceView;
import com.casic.dcms.mvp.view.IHighEventView;
import com.casic.dcms.utils.Constant;
import com.casic.dcms.utils.SaveKeyValues;
import com.casic.dcms.utils.ToastHelper;
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 java.util.ArrayList;
import java.util.List;

/**
 * @author : Pengxh
 * @time : 2021/4/16 9:43
 * @email : 290677893@qq.com
 */
public class PieChartFragment extends AndroidxBaseFragment<FragmentChartBinding> implements IEventSourceView, IHighEventView {

    private final List<PieEntry> entries = new ArrayList<>();
    private EventSourcePresenterImpl eventSourcePresenter;
    private HighEventPresenterImpl highEventPresenter;
    private String begTime;
    private String endTime;

    @Override
    protected void setupTopBarLayout() {

    }

    @Override
    protected void initData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            String pageTitle = arguments.getString("tag");
        }
        begTime = (String) SaveKeyValues.getValue("begTime", "");
        endTime = (String) SaveKeyValues.getValue("endTime", "");
        eventSourcePresenter = new EventSourcePresenterImpl(this);
        highEventPresenter = new HighEventPresenterImpl(this);
    }


    @Override
    protected void initEvent() {
        if (TextUtils.isEmpty(begTime) || TextUtils.isEmpty(endTime)) {
            ToastHelper.showToast("起始时间或者结束时间不对", ToastHelper.ERROR);
            return;
        }
        if (getTag().equals("EventSourceActivity")) {
            eventSourcePresenter.onReadyRetrofitRequest(begTime, endTime);
        } else {
            highEventPresenter.onReadyRetrofitRequest(begTime, endTime);
        }
    }

    @Override
    public void obtainEventSourceResult(EventSourceBean resultBean) {
        if (resultBean.getCode() == 200) {
            List<EventSourceBean.DataBean> dataBeanList = resultBean.getData();
            for (int i = 0; i < dataBeanList.size() - 1; i++) {
                EventSourceBean.DataBean dataBean = dataBeanList.get(i);
                String caseNum = dataBean.getCaseNum();
                if (!caseNum.equals("0")) {
                    entries.add(new PieEntry(Float.parseFloat(caseNum), dataBean.getCaseSource()));
                }
            }
            initPieChart();
        }
    }

    @Override
    public void obtainHighEventResult(HighEventBean resultBean) {
        if (resultBean.getCode() == 200) {
            List<HighEventBean.DataBean> dataBeanList = resultBean.getData();
            for (int i = 0; i < dataBeanList.size() - 1; i++) {
                HighEventBean.DataBean dataBean = dataBeanList.get(i);
                String caseNum = dataBean.getCaseNum();
                if (!caseNum.equals("0")) {
                    entries.add(new PieEntry(Float.parseFloat(caseNum), dataBean.getCaseType()));
                }
            }
            initPieChart();
        }
    }

    private void initPieChart() {
        viewBinding.pieChartView.setDragDecelerationFrictionCoef(0.95f);
        viewBinding.pieChartView.setUsePercentValues(true); //百分比数字显示
        viewBinding.pieChartView.setDrawEntryLabels(false); //不显示分类标签
        viewBinding.pieChartView.setDrawCenterText(false);
        viewBinding.pieChartView.getDescription().setEnabled(false);
        viewBinding.pieChartView.setRotationAngle(0f);
        // enable rotation of the chart by touch
        viewBinding.pieChartView.setRotationEnabled(true);
        viewBinding.pieChartView.setHighlightPerTapEnabled(true);
        viewBinding.pieChartView.setDrawHoleEnabled(false);
        setChartData();
        viewBinding.pieChartView.animateY(1000);
        Legend legend = viewBinding.pieChartView.getLegend();
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);
        legend.setDrawInside(false);
        legend.setXEntrySpace(7f);
        legend.setYEntrySpace(1f);
        legend.setYOffset(0f);
        // entry label styling
        viewBinding.pieChartView.setEntryLabelColor(Color.BLUE);
        viewBinding.pieChartView.setEntryLabelTextSize(12f);
        viewBinding.pieChartView.setExtraOffsets(30f, 30f, 30f, 30f);
    }

    private void setChartData() {
        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setSliceSpace(0f);
        dataSet.setSelectionShift(5f);
        dataSet.setColors(Constant.PIE_COLORS);
        //当数据太多时，将标签显示在饼图外
        if (entries.size() > 8) {
            dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
            dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        }
        dataSet.setValueLineColor(Color.GREEN);
        dataSet.setValueLinePart1Length(0.6f);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter(viewBinding.pieChartView)); //坑：在构造PercentFormatter的时候，需要传pieChart 对象进来才能正常显示出百分号。
        data.setValueTextSize(12f);
        data.setValueTextColor(Color.BLUE); //数据值颜色
        viewBinding.pieChartView.setData(data);
        viewBinding.pieChartView.highlightValues(null);
        viewBinding.pieChartView.invalidate();
    }

    @Override
    public void onDestroyView() {
        eventSourcePresenter.disposeRetrofitRequest();
        highEventPresenter.disposeRetrofitRequest();
        super.onDestroyView();
    }

    public static PieChartFragment newInstance(String tag) {
        PieChartFragment chartFragment = new PieChartFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tag", tag);
        chartFragment.setArguments(bundle);
        return chartFragment;
    }
}