package com.casic.dcms.ui.fragment;

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

import com.casic.dcms.R;
import com.casic.dcms.base.BaseFragment;
import com.casic.dcms.bean.EventSourceBean;
import com.casic.dcms.bean.HighEventBean;
import com.casic.dcms.mvp.presenter.EventSourcePresenterImpl;
import com.casic.dcms.mvp.presenter.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.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 java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

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

    @BindView(R.id.pieChartView)
    PieChart pieChartView;

    private String tag = "";
    private EventSourcePresenterImpl eventSourcePresenter;
    private HighEventPresenterImpl highEventPresenter;
    private List<PieEntry> entries;
    private String begTime;
    private String endTime;

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

    @Override
    protected int initLayoutView() {
        return R.layout.fragment_chart;
    }

    @Override
    protected void setupTopBarLayout() {

    }

    @Override
    protected void initData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            tag = arguments.getString("tag");
        }
        begTime = (String) SaveKeyValues.getValue("begTime", "");
        endTime = (String) SaveKeyValues.getValue("endTime", "");
        entries = new ArrayList<>();
        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 (tag.equals("EventSourceActivity")) {
            eventSourcePresenter.onReadyRetrofitRequest(begTime, endTime);
        } else {
            highEventPresenter.onReadyRetrofitRequest(begTime, endTime);
        }
    }

    @Override
    public void obtainEventSourceResult(EventSourceBean resultBean) {
        if (resultBean.isSuccess()) {
            List<EventSourceBean.DataBean> dataBeanList = resultBean.getData();
            for (EventSourceBean.DataBean dataBean : dataBeanList) {
                float eventNum = Float.parseFloat(dataBean.getEventNum());
                if (eventNum != 0) {
                    entries.add(new PieEntry(eventNum, dataBean.getCaseSource()));
                }
            }
            initPieChart();
        }
    }

    @Override
    public void obtainHighEventResult(HighEventBean resultBean) {
        if (resultBean.isSuccess()) {
            List<HighEventBean.DataBean> dataBeanList = resultBean.getData();
            for (HighEventBean.DataBean dataBean : dataBeanList) {
                float eventNum = Float.parseFloat(dataBean.getCaseNum());
                if (eventNum != 0) {
                    entries.add(new PieEntry(eventNum, dataBean.getCaseType()));
                }
            }
            initPieChart();
        }
    }

    private void initPieChart() {
        pieChartView.setDragDecelerationFrictionCoef(0.95f);
        pieChartView.setDrawCenterText(false);
        pieChartView.getDescription().setEnabled(false);
        pieChartView.setRotationAngle(0);
        // enable rotation of the chart by touch
        pieChartView.setRotationEnabled(true);
        pieChartView.setHighlightPerTapEnabled(true);
        pieChartView.setDrawEntryLabels(true);
        pieChartView.setDrawHoleEnabled(false);
        setChartData();
        pieChartView.animateY(1000);

        Legend l = pieChartView.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(1f);
        l.setYOffset(0f);
        // entry label styling
        pieChartView.setEntryLabelColor(Color.BLUE);
        pieChartView.setEntryLabelTextSize(12f);
        pieChartView.setExtraOffsets(30, 30, 30, 30);
    }

    private void setChartData() {
        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setSliceSpace(0f);
        dataSet.setSelectionShift(5f);
        dataSet.setColors(Constant.PIE_COLORS);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setValueLineColor(Color.GREEN);
        dataSet.setValueLinePart1Length(0.6f);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(12f);
        data.setValueTextColor(Color.BLUE);
        pieChartView.setData(data);
        pieChartView.highlightValues(null);
        pieChartView.invalidate();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (eventSourcePresenter != null) {
            eventSourcePresenter.disposeRetrofitRequest();
        }
        if (highEventPresenter != null) {
            highEventPresenter.disposeRetrofitRequest();
        }
    }
}
