package com.unis.sxzcl.ui.company;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.PieEntry;
import com.unis.model.DataCallback;
import com.unis.model.DepotModel;
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.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 包名: 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 Integer mDepotId;

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

    @Override
    protected void init() {
        MultiDepotBean selectDepot = DepotModel.get().getSelectDepot();
        mDepotId = selectDepot.depotCode;
        mBinding.bar.setTitle(selectDepot.depotName);
        mBinding.bar.setLeftClickListener(v -> onBack());

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

    @Override
    protected void onFirstShow() {

        if (mPieChartHouseStatus == null) {
            mPieChartHouseStatus = new MyPieChart(mBinding.pieHouseStatus);
        }

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

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

        showProgress("正在加载...", false);
        getHouseStatus();
        getFoodType();
        getStorageStatus();
    }


    /**
     * 仓房状态饼图
     */
    private void getHouseStatus() {

        // 请求后台接口
        DepotModel.get().getWarehouseStatus(1, new DataCallback<List<Map<String, Object>>>() {
            @Override
            public void onSuccess(List<Map<String, Object>> data) {
                if (!isActive()) return;
                hideProgress();

                if (data == null || data.size() == 0) return;

                // 将数据源 转为 饼图数据
                List<PieEntry> pieEntries = new ArrayList<>();
                for (Map<String, Object> item : data) {
                    String count = (String) item.get("count");
                    String status = (String) item.get("warehouseStatus");
                    if (count == null) count = "0";
                    if (status == null) status = "";
                    pieEntries.add(new PieEntry(Float.parseFloat(count), status));
                }
                List<Integer> list = Arrays.asList(
                        Color.parseColor("#FFA100"),
                        Color.parseColor("#1FA2EB"),
                        Color.parseColor("#F3DE38"),
                        Color.parseColor("#89BF6B"));
                // 显示饼图
                mPieChartHouseStatus.showChart(pieEntries, "仓房状态", list);
            }

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

    /**
     * 粮食品种柱状图
     */
    private void getFoodType() {

        DepotModel.get().getFoodTypeCount(1, new DataCallback<List<Map<String, Object>>>() {
            @Override
            public void onSuccess(List<Map<String, Object>> data) {
                if (!isActive()) return;
                hideProgress();

                if (data == null || data.size() == 0) return;

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

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

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

                }
                mBarChartFoodType.showChart(labelList, valueList);
            }

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

            }
        });
    }

    /**
     * 存储状态饼图
     */
    private void getStorageStatus() {

        // 请求后台接口
        DepotModel.get().getStorageStatus(1, new DataCallback<List<Map<String, Object>>>() {
            @Override
            public void onSuccess(List<Map<String, Object>> data) {
                if (!isActive()) return;
                hideProgress();

                if (data == null || data.size() == 0) return;

                // 将数据源 转为 饼图数据
                List<PieEntry> pieEntries = new ArrayList<>();
                for (Map<String, Object> item : data) {
                    String count = (String) item.get("count");
                    String status = (String) item.get("storageStatus");
                    if (count == null) count = "0";
                    if (status == null) status = "";
                    pieEntries.add(new PieEntry(Float.parseFloat(count), status));
                }
                List<Integer> list = Arrays.asList(
                        Color.parseColor("#057B43"),
                        Color.parseColor("#8ABF6B"),
                        Color.parseColor("#F3DE38"));
                // 显示饼图
                mPieChartStorageStatus.showChart(pieEntries, "储存状态", list);
            }

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

            }
        });
    }
}
