package com.zd.common.core.echarts;

import cn.hutool.core.collection.CollUtil;
import com.zd.common.core.echarts.enums.SerieType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: zhoupan
 * @Date: 2021/09/15/9:42
 * @Description:
 */
public class EchartsBuilder<X> implements Builder {

    private EchartConfig echartConfig = new EchartConfig();

    private List<? extends EchartsData> echartsDatas;

    private List<String> legendData = null;
    private Boolean isLegendData = Boolean.FALSE;
    private Map<String, Object> temp = null;
    private List<String> axisData = null;

    public EchartsBuilder(List<? extends EchartsData<X>> echartsDatas, EchartConfig echartConfig) {
        this.echartsDatas = echartsDatas;
        this.echartConfig = echartConfig;
        init();
    }

    public EchartsBuilder(List<? extends EchartsData<X>> echartsDatas) {
        this.echartsDatas = echartsDatas;
        init();
    }

    public EchartsBuilder() {
    }

    /**
     * 实现listmap 的数据
     *
     * @param listMap
     * @param key
     * @param v
     * @return
     */
    public EchartsBuilder setListData(List<Map<String, Object>> listMap, String key, String v) {
        this.echartsDatas = MapInList.conversion(listMap, key, v);
        init();
        return this;

    }

    /**
     * 初始化方法
     */
    void init() {
        if (echartConfig == null || echartConfig.getType() == null || echartConfig.getXyValue() == null) {
            throw new IllegalArgumentException("Echart转换配置错误!");
        }
        if (CollUtil.isEmpty(echartsDatas)) return;

        legendData = echartsDatas.stream().map(a -> a.legendData() == null ? "" : a.legendData()).distinct().collect(Collectors.toList());
        //折线图
        if (echartConfig.getType().equals(SerieType.line)) {
            if (CollUtil.isNotEmpty(legendData) && legendData.size() > 1) {
                isLegendData = Boolean.TRUE;
            }
        }
        //axisData
        axisData = echartsDatas.stream().map(a -> a.axisData() == null ? "" : a.axisData()).distinct().collect(Collectors.toList());

        if (CollUtil.isEmpty(axisData)) return;
        temp = new HashMap<>(echartsDatas.size());
        echartsDatas.forEach(a -> {
            String x = a.axisData() == null ? "" : a.axisData();
            String l = a.legendData() == null ? "" : a.legendData();
            if (temp.containsKey(l + x)) {
                throw new IllegalArgumentException("存在重复的数据！");
            }
            if (echartConfig.getType().equals(SerieType.pie)) {
                temp.put(l + x, new DataValue(a.dataValue(), a.axisData()));
            } else {
                temp.put(l + x, a.dataValue());
            }

        });

    }

    @Override
    public EchartConfig getEchartConfig() {
        return echartConfig;
    }

    @Override
    public Builder setEchartConfig(EchartConfig echartConfig) {
        this.echartConfig = echartConfig;
        return this;
    }

    @Override
    public Builder setTitle(Title title) {
        return null;
    }

    @Override
    public Option build() {
        Option option = new Option();
        if (CollUtil.isEmpty(axisData)) return option;


        List list = new ArrayList(legendData.size());
        option.setSeries(list);


        //
        if (CollUtil.isNotEmpty(legendData)) {
            Axis axis = new Axis();
            axis.setData(axisData);
            if (echartConfig.getXyValue().equals(EchartConfig.XYEnum.Y_VALUE)) {
                option.setxAxis(axis);
            } else {
                option.setyAxis(axis);
            }

            if (legendData.size() > 1) {
                if (echartConfig.getType().equals(SerieType.line)) {
                    Legend legend = new Legend();
                    legend.setData(legendData);
                    option.setLegend(legend);
                }

            }
            for (int i = 0; i < legendData.size(); i++) {
                Serie serie = new Serie();
                list.add(serie);
                serie.setType(echartConfig.getType());
                List data = new ArrayList();
                serie.setData(data);
                if (legendData.size() > 1) {
                    serie.setName(legendData.get(i));
                }
                for (int i1 = 0; i1 < axisData.size(); i1++) {
                    Object o = temp.get(legendData.get(i) + axisData.get(i1));
                    data.add(o);
                }

            }

        }

        return option;
    }
}
