package yem.wmc.ydn.formplugin.dynamic;


import com.alibaba.druid.util.StringUtils;
import com.alibaba.nacos.shaded.com.google.common.collect.Maps;


import kd.bos.form.chart.*;
import kd.bos.form.plugin.AbstractFormPlugin;

import java.security.acl.Group;
import java.util.*;


public class test extends AbstractFormPlugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        HistogramChart customchart = this.getControl("柱状图标识");
        customchart.clearData(); // 清除数据 避免重复增加数据
        customchart.clearGraphic();
// customchart.setShowLegend(false); //图例
// customchart.setShowTitle(false); //标题
        customchart.setMerge(false);
// 设置图形的位置
// customchart.setMargin(Position.right, "80");

        customchart.setMargin(Position.left, "15px");
        customchart.setMargin(Position.right, "15px");
        customchart.setMargin(Position.top, "55px");
        customchart.setMargin(Position.bottom, "15px");
// 设置图例
        customchart.setShowLegend(true);
// true纵向显示 false不纵向显示
        customchart.setLegendVertical(false);
// 获取默认x轴数据
        List<String> materiaName = this.getMateriaName();
// 设置柱状图类别数据
        Map<Group, List<Number>> sortedData = this.getData02(materiaName);
// 利用lambda表达式遍历sortedData
        sortedData.forEach((group, data) -> {
            this.createBarSeries(group, data, customchart);
        });
// 创建X轴
        Axis xAxis = this.createCategoryAxis("", true, customchart);
// 设置X轴坐标数据
        xAxis.setCategorys(materiaName);
// X轴指示器属性设置
        Map<String, Object> xAxisPointer = Maps.newHashMap();
        xAxisPointer.put("show", true);
        xAxisPointer.put("type", "shadow");
// X轴文本标签设置
        Label xAxisLabel = new Label();
// 显示文本标签
        xAxisLabel.setShow(true);
// 背景色黑色
        xAxisLabel.setBackgroundColor("black");
// xAxisPointer.put("nameGap", "2");
        xAxisPointer.put("label", xAxisLabel);
// 坐标轴名字
// xAxis.setName("物料名称");
        xAxis.setPropValue("axisPointer", xAxisPointer);
// 创建Y轴
        Axis yAxis = this.createValueAxis("", false, customchart);
// yAxis.setMax(200);
// 设置Y轴数据显示格式
        Map<String, Object> axisLabel1 = Maps.newHashMap();
        axisLabel1.put("formatter", "{value} 元");
        yAxis.setPropValue("axisLabel", axisLabel1);
// Y轴设置最小刻度
        yAxis.setPropValue("minInterval", 1);
// Y轴指示器属性
        Map<String, Object> yAxisPointer = Maps.newHashMap();
        yAxisPointer.put("show", true);
        yAxisPointer.put("type", "line");
// Y轴文本标签设置
        Label yAxisLabel = new Label();
        yAxisLabel.setShow(true);
        yAxisLabel.setBackgroundColor("black");
        yAxisPointer.put("label", xAxisLabel);
// Y轴设置指示线类型
        Map<String, Object> lineStyle = Maps.newHashMap();
        lineStyle.put("type", "dotted");
        yAxisPointer.put("lineStyle", lineStyle);
// 设置Y轴不触发悬浮提示信息
        yAxisPointer.put("triggerTooltip", false);
        yAxis.setPropValue("axisPointer", yAxisPointer);
// X轴文字倾斜 -90 ~ 90
        Map<String, Object> axisLabelMap = new HashMap<>();
        axisLabelMap.put("interval", 0);
        axisLabelMap.put("rotate", 35);
        xAxis.setPropValue("axisLabel", axisLabelMap);
// 刷新图表
        customchart.refresh();




    }
    private void createBarSeries(Group group, List<Number> data, HistogramChart customchart) {
        // TODO Auto-generated method stub
        BarSeries barSeries = customchart.createBarSeries(group.groupName);


        /**
         * 不设置柱宽度 让其随类目宽度自适应
         */
        // 设置柱宽度
        // barSeries.setBarWidth("25px");

        // 设置柱颜色
        barSeries.setColor(group.color);
        // 设置类别展示类型
        barSeries.setType(group.chartType);
        // 动画过度时间
        barSeries.setAnimationDuration(2000);
        // 设置相同的stack会堆叠显示
//       barSeries.setStack("stack");
        // 设置所属坐标轴
        barSeries.setPropValue("yAxisIndex", group.yAxisIndex);
        // 设置类别对应的数据包
        barSeries.setData((Number[]) data.toArray(new Number[0]));

    }

    private List<String> getMateriaName() {
        List<String> materiaName = new ArrayList<>();
        materiaName.add("大连市");
        materiaName.add("北京市");
        materiaName.add("昆明市");
        materiaName.add("沈阳市");
        materiaName.add("九江市");
        materiaName.add("天津市");
        materiaName.add("上海市");
        materiaName.add("无锡市");
        materiaName.add("石家庄市");
        materiaName.add("新疆维吾尔自治区");
        return materiaName;
    }

    private Map<Group, List<Number>> getData02(List<String> materiaName) {
        // TODO Auto-generated method stub
        Map<Group, List<Number>> summaryResult = new LinkedHashMap<Group, List<Number>>();
        // 得到默认groups
        List<Group> groups = this.getDefaultGroups();

        // 给每个分组添加根据日期添加默认数据值
        List<Number> list01=new ArrayList<>();
        list01.add(10);
        list01.add(100);
        list01.add(70);
        list01.add(120);
        list01.add(15);
        list01.add(80);
        list01.add(130);
        list01.add(50);
        list01.add(120);
        list01.add(15);
        list01.add(80);
        summaryResult.put(groups.get(0), list01);

        List<Number> list02=new ArrayList<>();
        list02.add(15);
        list02.add(80);
        list02.add(130);
        list02.add(50);
        list02.add(30);
        list02.add(170);
        list02.add(60);
        list02.add(70);
        list02.add(30);
        list02.add(170);
        list02.add(60);
        summaryResult.put(groups.get(1), list02);

        List<Number> list03=new ArrayList<>();
        list03.add(30);
        list03.add(170);
        list03.add(60);
        list03.add(70);
        list03.add(120);
        list03.add(140);
        list03.add(160);
        list03.add(60);
        list03.add(70);
        list03.add(120);
        list03.add(70);
        summaryResult.put(groups.get(2), list03);

        List<Number> list04=new ArrayList<>();
        list04.add(120);
        list04.add(140);
        list04.add(70);
        list04.add(60);
        list04.add(70);
        list04.add(160);
        list04.add(70);
        list04.add(60);
        list04.add(70);
        list04.add(120);
        list04.add(140);
        summaryResult.put(groups.get(3), list04);

        return summaryResult;
    }

    private Group matchGroup(List<Group> groups, String ren) {
        // TODO Auto-generated method stub
        for (Group group : groups) {
            if (StringUtils.equals(group.groupKey, ren)) {
                return group;
            }
        }
        return null;
    }

    private Axis createCategoryAxis(String name, boolean isX, HistogramChart customchart) {
        Axis axis = null;
        if (isX) {
            // X轴
            axis = customchart.createXAxis(name, AxisType.category);
        } else {
            // Y轴
            axis = customchart.createYAxis(name, AxisType.category);
        }
        // axis.setCategorys(x);
        // 坐标轴刻度相关设置
        Map<String, Object> axisTick = new HashMap<>();
        // 坐标轴刻度的显示间隔为0
        axisTick.put("interval", 0);
        // 坐标轴刻度设置为不显示
        // axisTick.put("show", true);
        // 设置坐标轴的属性-刻度
        axis.setPropValue("axisTick", axisTick);
        // 设置坐标轴轴线颜色
        this.setLineColor(axis, "#5F9EA0");
        return axis;
    }

    private Axis createValueAxis(String name, boolean isX, HistogramChart customchart) {
        Axis axis = null;
        if (isX) {
            // X轴
            axis = customchart.createXAxis(name, AxisType.value);
        } else {
            // Y轴
            axis = customchart.createYAxis(name, AxisType.value);
        }

        // 坐标轴刻度相关设置
        Map<String, Object> axisTick = new HashMap<>();
        // 坐标轴刻度设置为不显示
        // axisTick.put("show", false);
        // 设置坐标轴的属性-刻度
        axis.setPropValue("axisTick", axisTick);

        //
        Map<String, Object> splitLine = new HashMap<>();
        Map<String, Object> lineStyle = new HashMap<>();
        // 分隔线的类型
        lineStyle.put("type", "dotted");
        // 分隔线的颜色
        lineStyle.put("color", "#E2E2E2");
        splitLine.put("lineStyle", lineStyle);
        // 设置坐标轴的属性-分隔线
        axis.setPropValue("splitLine", splitLine);
        // 设置坐标轴轴线颜色
        this.setLineColor(axis, "#5F9EA0");
        // 显示悬浮提示框
        customchart.setShowTooltip(true);
        return axis;
    }

    private void setLineColor(Axis axix, String color) {
        Map<String, Object> axisLineMap = new HashMap<>();
        Map<String, Object> lineStyleMap = new HashMap<>();
        // 坐标轴轴线的颜色
        lineStyleMap.put("color", color);
        axisLineMap.put("lineStyle", lineStyleMap);
        // 设置坐标轴的属性-轴线
        axix.setPropValue("axisLine", axisLineMap);
    }

    private List<Group> getDefaultGroups() {
        List<Group> defaultGroupList = new ArrayList<>(4);
        defaultGroupList.add(new Group("2023年上半年", "length", "#F1C589", true, ChartType.bar, 0));
        defaultGroupList.add(new Group("2024年上半年", "width", "#63E354", true, ChartType.bar, 0));
        defaultGroupList.add(new Group("2023年下半年", "height", "#FFCC00", true, ChartType.bar, 0));
        defaultGroupList.add(new Group("2024年下半年", "sss", "#0AE9FD", true, ChartType.bar, 0));
        return defaultGroupList;
    }

    public class Group {
        public String groupKey;
        public Object yAxisIndex;
        public String groupName;
        private String name;            // 组名称
        private String type;            // 组类型
        private String color;           // 组颜色
        private boolean isActive;       // 是否激活
        private ChartType chartType;    // 图表类型
        private int value;              // 数值

        // 构造方法
        public Group(String name, String type, String color, boolean isActive, ChartType chartType, int value) {
            this.name = name;
            this.type = type;
            this.color = color;
            this.isActive = isActive;
            this.chartType = chartType;
            this.value = value;
        }

        // Getter 和 Setter 方法
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public boolean isActive() {
            return isActive;
        }

        public void setActive(boolean active) {
            isActive = active;
        }

        public ChartType getChartType() {
            return chartType;
        }

        public void setChartType(ChartType chartType) {
            this.chartType = chartType;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        // toString 方法，便于打印
        @Override
        public String toString() {
            return "Group{" +
                    "name='" + name + '\'' +
                    ", type='" + type + '\'' +
                    ", color='" + color + '\'' +
                    ", isActive=" + isActive +
                    ", chartType=" + chartType +
                    ", value=" + value +
                    '}';
        }
    }
}
