package io.gisbi.chart.charts.impl.numeric;

import io.gisbi.chart.charts.impl.YoyChartHandler;
import io.gisbi.chart.utils.ChartDataBuild;
import io.gisbi.extensions.datasource.model.SQLMeta;
import io.gisbi.extensions.datasource.provider.Provider;
import io.gisbi.extensions.view.dto.*;
import lombok.Getter;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class IndicatorHandler extends YoyChartHandler {
    @Getter
    private String render = "custom";
    @Getter
    private String type = "indicator";

    @Override
    public Map<String, Object> buildNormalResult(ChartViewDTO view, AxisFormatResult formatResult, CustomFilterResult filterResult, List<String[]> data) {
        boolean isDrill = filterResult.getFilterList().stream().anyMatch(ele -> ele.getFilterType() == 1);
        var xAxis = formatResult.getAxisMap().get(ChartAxis.xAxis);
        var yAxis = formatResult.getAxisMap().get(ChartAxis.yAxis);
        return ChartDataBuild.transNormalChartData(xAxis, yAxis, view, data, isDrill);
    }

    @Override
    public <T extends ChartCalcDataResult> T calcChartResult(ChartViewDTO view, AxisFormatResult formatResult, CustomFilterResult filterResult, Map<String, Object> sqlMap, SQLMeta sqlMeta, Provider provider) {
        this.setIndicatorHandlerXAxis(formatResult, filterResult);
        return (T) super.calcChartResult(view, formatResult, filterResult, sqlMap, sqlMeta, provider);
    }

    @Override
    public AxisFormatResult formatAxis(ChartViewDTO view) {
        var axisMap = new HashMap<ChartAxis, List<ChartViewFieldDTO>>();
        var yAxis = new ArrayList<>(view.getYAxis());
        axisMap.put(ChartAxis.xAxis, new ArrayList<>());
        axisMap.put(ChartAxis.yAxis, yAxis);
        var context = new HashMap<String, Object>();
        return new AxisFormatResult(axisMap, context);
    }

    private void setIndicatorHandlerXAxis(AxisFormatResult formatResult, CustomFilterResult filterResult) {
        var xAxis = formatResult.getAxisMap().get(ChartAxis.xAxis);
        var yAxis = formatResult.getAxisMap().get(ChartAxis.yAxis);
        var allFields = (List<ChartViewFieldDTO>) filterResult.getContext().get("allFields");
        ChartViewFieldDTO yAxisChartViewFieldDTO = yAxis.get(0);
        ChartFieldCompareDTO compareCalc = yAxisChartViewFieldDTO.getCompareCalc();
        boolean isYoy = org.apache.commons.lang3.StringUtils.isNotEmpty(compareCalc.getType())
                && !org.apache.commons.lang3.StringUtils.equalsIgnoreCase(compareCalc.getType(), "none");
        if (isYoy) {
            xAxis.clear();
            // 设置维度字段，从同环比中获取用户选择的字段
            xAxis.addAll(allFields.stream().filter(i -> org.springframework.util.StringUtils.endsWithIgnoreCase(i.getId().toString(), compareCalc.getField().toString())).toList());
            xAxis.get(0).setSort("desc");
            if(Objects.isNull(compareCalc.getCustom())){
                xAxis.get(0).setDateStyle("y_M_d");
            }else{
                xAxis.get(0).setDateStyle(compareCalc.getCustom().getTimeType());
            }
        }
        formatResult.getAxisMap().put(ChartAxis.xAxis, xAxis);
    }
}
