package com.unis.sxzcl.ui.company;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.PieEntry;
import com.unis.model.BasicModel;
import com.unis.model.DataCallback;
import com.unis.model.DepotModel;
import com.unis.model.bean.HomeStatsBean;
import com.unis.model.bean.HomeStatsItemBean;
import com.unis.model.bean.MultiDepotBean;
import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentCompanyStatsBinding;
import com.unis.sxzcl.widget.MyBarChart;
import com.unis.sxzcl.widget.MyPieChart;

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

/**
 * 包名: com.unis.sxzcl.ui.company
 * 类名: CompanyStatsFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/9 10:00
 * 更新人:
 * 更新时间: 2020/12/9 10:00
 * 更新说明:
 * 版本号: 1.0
 */
public class CompanyStatsFragment extends BaseFragment<FragmentCompanyStatsBinding> {

    private MyPieChart mPieChartHouseStatus;
    private MyBarChart mBarChartFoodType;
    private MyPieChart mPieChartStorageStatus;
    private String mDepotId;

    @Override
    protected FragmentCompanyStatsBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentCompanyStatsBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        Bundle bundle = getArguments();
        String depotJson = bundle.getString("depot", null);

        MultiDepotBean selectDepot = null;
        if (depotJson != null) {
            // 如果有参数 则从参数取
            selectDepot = getGson().fromJson(depotJson, MultiDepotBean.class);
            // 隐藏标题栏
            mBinding.bar.setVisibility(View.GONE);
            // 禁用下拉刷新
            mBinding.scrollView.setNestedScrollingEnabled(true);
        } else {
            // 没参数 取选中的库
            selectDepot = DepotModel.get().getSelectDepot();
            mBinding.bar.setTitle(selectDepot.depotName);
            mBinding.bar.setLeftClickListener(v -> onBack());
            showProgress("正在加载...", false);
        }
        mDepotId = selectDepot.depotCode;

        mBinding.tvCompany.setText(selectDepot.depotName);
    }

    @Override
    protected void onFirstShow() {
        if (mPieChartHouseStatus == null) {
            mPieChartHouseStatus = new MyPieChart(getContext(), mBinding.pieHouseStatus);
        }

        if (mBarChartFoodType == null) {
            mBarChartFoodType = new MyBarChart(getContext(), mBinding.barFoodType);
        }

        if (mPieChartStorageStatus == null) {
            mPieChartStorageStatus = new MyPieChart(getContext(), mBinding.pieStorageStatus);
        }

        getStatus();
    }

    /**
     * 获取统计信息
     */
    public void getStatus() {
        // 请求后台接口
        DepotModel.get().getStatus(mDepotId, new DataCallback<HomeStatsBean>() {
            @Override
            public void onSuccess(HomeStatsBean data) {
                if (!isActive()) return;
                hideProgress();

                if (data == null) return;

                // 仓房信息统计
                showHouseStatus(data.getWarehouseStatus());
                // 粮食品种统计
                showFoodType(data.getGrainVarieties());
                // 存储状态统计
                showStorageStatus(data.getStorageStatus());
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                if (!isActive()) return;
                hideProgress();
            }
        });
    }


    /**
     * 显示仓房状态饼图
     */
    private void showHouseStatus(List<HomeStatsItemBean> data) {
        if (data == null) return;

        // 将数据源 转为 饼图数据
        List<PieEntry> pieEntries = new ArrayList<>();
        List<Integer> colorList = new ArrayList<>();
        for (HomeStatsItemBean item : data) {

            String count = item.getCount();
            String status = item.getWarehouseStatus();
            if (count == null) count = "0";
            if (status == null) status = "";
            pieEntries.add(new PieEntry(Float.parseFloat(count), status));

            colorList.add(BasicModel.get().getColor(status));
        }

        // 显示饼图
        mPieChartHouseStatus.showChart(pieEntries, "仓房状态", colorList);
    }

    /**
     * 显示粮食品种柱状图
     */
    private void showFoodType(List<HomeStatsItemBean> data) {
        if (data == null) return;

        // 将数据源 转为 饼图数据
        List<String> labelList = new ArrayList<>();
        ArrayList<BarEntry> valueList = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            HomeStatsItemBean item = data.get(i);

            String label = item.getGrainVarieties();
            if (label == null) label = "";
            labelList.add(label);

            String count = item.getCount();
            if (count == null) count = "0";
            valueList.add(new BarEntry(i, Float.parseFloat(count)));

        }
        mBarChartFoodType.showChart(labelList, valueList);

    }

    /**
     * 显示存储状态饼图
     */
    private void showStorageStatus(List<HomeStatsItemBean> data) {
        if (data == null) return;

        // 将数据源 转为 饼图数据
        List<PieEntry> pieEntries = new ArrayList<>();
        List<Integer> colorList = new ArrayList<>();
        for (HomeStatsItemBean item : data) {

            String count = item.getCount();
            String status = item.getStorageStatus();
            if (count == null) count = "0";
            if (status == null) status = "";

            pieEntries.add(new PieEntry(Float.parseFloat(count), status));

            colorList.add(BasicModel.get().getColor(status));
        }
        // 显示饼图
        mPieChartStorageStatus.showChart(pieEntries, "储存状态", colorList);
    }


}
