package com.ningdatech.pmapi.dashboard.helper;

import cn.hutool.core.collection.CollUtil;
import com.ningdatech.pmapi.dashboard.constant.ChartTypeEnum;
import com.ningdatech.pmapi.dashboard.model.basic.AnalysisChart;
import com.ningdatech.pmapi.dashboard.model.basic.AnalysisData;
import com.ningdatech.pmapi.meta.helper.DictionaryCache;
import com.ningdatech.pmapi.projectlib.enumeration.ProjectStatusEnum;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author liuxinxin
 * @date 2023/8/4 下午2:36
 */

@Component
@RequiredArgsConstructor
public class DashboardChartAssembler {

    private final DictionaryCache dictionaryCache;


    public static AnalysisChart buildProjectStateAnalysisChart(Map<Integer, List<Project>> projectGroupMap, ChartTypeEnum chartTypeEnum) {
        AnalysisChart analysisChart = new AnalysisChart();
        List<AnalysisData> dataList = new ArrayList<>();
        analysisChart.setChartType(chartTypeEnum);
        analysisChart.setDataList(dataList);
        for (Integer key : projectGroupMap.keySet()) {
            AnalysisData analysisData = new AnalysisData();
            String chartKey = ProjectStatusEnum.getDesc(key);
            List<Project> projectList = projectGroupMap.get(key);
            analysisData.setKey(chartKey);
            analysisData.setValue(CollUtil.isEmpty(projectList) ? 0 : projectList.size());
            dataList.add(analysisData);
        }
        return analysisChart;
    }

    public static <T> AnalysisChart buildInvestmentAnalysisChart(Map<T, String> keyValueMap
            , Map<T, List<Project>> projectGroupMap, ChartTypeEnum chartTypeEnum) {
        AnalysisChart analysisChart = new AnalysisChart();
        List<AnalysisData> dataList = new ArrayList<>();
        analysisChart.setChartType(chartTypeEnum);
        analysisChart.setDataList(dataList);
        for (T key : keyValueMap.keySet()) {
            AnalysisData analysisData = new AnalysisData();
            String chartKey = keyValueMap.get(key);
            List<Project> projectList = projectGroupMap.get(key);

            BigDecimal approvedTotalInvestmentIncreaseSum = new BigDecimal("0");
            if (CollUtil.isNotEmpty(projectList)) {
                for (Project project : projectList) {
                    BigDecimal approvedTotalInvestmentIncrease = project.getApprovedTotalInvestmentIncrease();
                    if (Objects.nonNull(approvedTotalInvestmentIncrease)) {
                        approvedTotalInvestmentIncreaseSum.add(approvedTotalInvestmentIncrease);
                    }
                }
            }
            analysisData.setKey(chartKey);
            analysisData.setValue(approvedTotalInvestmentIncreaseSum);
            dataList.add(analysisData);
        }
        return analysisChart;
    }

    public static <T> AnalysisChart buildAnalysisChart(Map<T, String> keyValueMap
            , Map<T, List<Project>> projectGroupMap, ChartTypeEnum chartTypeEnum) {
        AnalysisChart analysisChart = new AnalysisChart();
        List<AnalysisData> dataList = new ArrayList<>();
        analysisChart.setChartType(chartTypeEnum);
        analysisChart.setDataList(dataList);
        for (T key : keyValueMap.keySet()) {
            AnalysisData analysisData = new AnalysisData();
            String chartKey = keyValueMap.get(key);
            List<Project> projectList = projectGroupMap.get(key);
            analysisData.setKey(chartKey);
            analysisData.setValue(CollUtil.isEmpty(projectList) ? 0 : projectList.size());
            dataList.add(analysisData);
        }
        return analysisChart;
    }

    public  <T> AnalysisChart assemblerAnalysisChart(Map<String, List<T>> dictionaryCodeIdMap
            , ChartTypeEnum chartTypeEnum) {
        AnalysisChart analysisChart = new AnalysisChart();
        List<AnalysisData> dataList = new ArrayList<>();
        analysisChart.setChartType(chartTypeEnum);
        analysisChart.setDataList(dataList);

        for (String dictionaryCode : dictionaryCodeIdMap.keySet()) {
            AnalysisData analysisData = new AnalysisData();
            analysisData.setKey(dictionaryCache.getByCode(dictionaryCode).getName());
            analysisData.setValue(dictionaryCodeIdMap.get(dictionaryCode).size());
            dataList.add(analysisData);
        }
        return analysisChart;
    }
}
