package com.njcn.echarts.json;

import cn.hutool.core.lang.Console;
import com.njcn.echarts.pojo.bo.TolerateData;
import com.njcn.echarts.pojo.constant.PicCommonData;
import org.icepear.echarts.Option;
import org.icepear.echarts.charts.bar.BarItemStyle;
import org.icepear.echarts.charts.bar.BarLabel;
import org.icepear.echarts.charts.bar.BarSeries;
import org.icepear.echarts.charts.line.LineSeries;
import org.icepear.echarts.charts.pie.PieLabel;
import org.icepear.echarts.charts.pie.PieSeries;
import org.icepear.echarts.charts.scatter.ScatterSeries;
import org.icepear.echarts.components.coord.*;
import org.icepear.echarts.components.coord.cartesian.CategoryAxis;
import org.icepear.echarts.components.coord.cartesian.LogAxis;
import org.icepear.echarts.components.coord.cartesian.ValueAxis;
import org.icepear.echarts.components.grid.Grid;
import org.icepear.echarts.components.legend.Legend;
import org.icepear.echarts.components.title.Title;
import org.icepear.echarts.components.tooltip.Tooltip;
import org.icepear.echarts.components.visualMap.ContinousVisualMap;
import org.icepear.echarts.origin.chart.bar.BarItemStyleOption;
import org.icepear.echarts.origin.coord.AxisLabelBaseOption;
import org.icepear.echarts.origin.util.SeriesOption;
import org.icepear.echarts.render.Engine;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author hongawen
 * @version 1.0.0
 * @date 2023年06月21日 10:05
 */
public class LineGenerator {

    private final static Engine ENGINE = new Engine();


    /***
     * 生成ITIC曲线
     * @author hongawen
     * @date 2023/6/21 10:06
     */
    public static String generateIticOption(TolerateData tolerateData) {
        Option iticOption = new Option();
        //取消渲染动画
        iticOption.setAnimation(false);
        //背景色
        iticOption.setBackgroundColor(PicCommonData.PIC_BACK_COLOR);
        //标题
        iticOption.setTitle(new Title().setLeft(PicCommonData.CENTER).setText("ITIC曲线"));
        //上下左右的图内间距
        iticOption.setGrid(new Grid().setTop("80px").setLeft("40px").setRight("40px").setBottom("10%"));
        //设置图例
        iticOption.setLegend(new Legend().setData(new String[]{"上限", "下限", "可容忍事件", "不可容忍事件"}).setTop("26px").setLeft(0).setBottom("94%"));
        //设置图例对应的颜色
        iticOption.setColor(new String[]{"#FF8C00", "#00BFFF", "green", "red"});
        //横坐标
        iticOption.setXAxis(new LogAxis().setMin("0.001").setMax("1000").setSplitLine(new SplitLine().setShow(false)).setName("s"));
        //纵坐标
        iticOption.setYAxis(new ValueAxis().setName("%").setMinInterval(3).setSplitNumber(10));
        //处理上限配置
        LineSeries upperLimit = new LineSeries()
                .setName("上限")
                .setData(new float[][]{{0.001f, 200}, {0.003f, 140}, {0.003f, 120}, {0.5f, 120}, {0.5f, 110}, {10, 110}, {1000, 110}})
                .setShowSymbol(false)
                .setTooltip(new Tooltip().setShow(false));

        //处理下限配置
        LineSeries lowerLimit = new LineSeries()
                .setName("下限")
                .setData(new float[][]{{0.02f, 0}, {0.02f, 70}, {0.5f, 70}, {0.5f, 80}, {10, 80}, {10, 90}, {1000, 90}})
                .setShowSymbol(false)
                .setTooltip(new Tooltip().setShow(false));
        //配置可容忍点数据
        ScatterSeries tolerate = new ScatterSeries()
                .setName("可容忍事件")
                .setSymbol("circle")
                .setData(tolerateData.getTolerateData());
        //配置不可容忍点数据
        ScatterSeries unTolerate = new ScatterSeries()
                .setName("不可容忍事件")
                .setSymbol("circle")
                .setData(tolerateData.getUnTolerateData());
        iticOption.setSeries(new SeriesOption[]{upperLimit, lowerLimit, tolerate, unTolerate});
        return ENGINE.renderJsonOption(iticOption);
    }


    /***
     * 生成F47曲线
     * @author hongawen
     * @date 2023/6/21 10:06
     */
    public static String generateF47Option(TolerateData tolerateData) {
        Option f47Option = new Option();
        //取消渲染动画
        f47Option.setAnimation(false);
        //背景色
        f47Option.setBackgroundColor(PicCommonData.PIC_BACK_COLOR);
        //标题
        f47Option.setTitle(new Title().setLeft(PicCommonData.CENTER).setText("SEMI F47曲线"));
        //上下左右的图内间距
        f47Option.setGrid(new Grid().setTop("80px").setLeft("40px").setRight("40px").setBottom("10%"));
        //设置图例
        f47Option.setLegend(new Legend().setData(new String[]{"边界线", "可容忍事件", "不可容忍事件"}).setTop("26px").setLeft(0).setBottom("94%"));
        //设置图例对应的颜色
        f47Option.setColor(new String[]{"yellow", "green", "red"});
        //横坐标
        f47Option.setXAxis(new LogAxis().setMin("0.001").setMax("1000").setSplitLine(new SplitLine().setShow(false)).setName("s"));
        //纵坐标
        f47Option.setYAxis(new ValueAxis().setName("%").setMinInterval(0.1).setSplitNumber(10).setMax(100));
        //处理边界线
        LineSeries borderLimit = new LineSeries()
                .setName("边界线")
                .setData(new float[][]{{0.05f, 0}, {0.05f, 50}, {0.2f, 50}, {0.2f, 70}, {0.5f, 70}, {0.5f, 80}, {10, 80}, {10, 90}, {1000, 90}})
                .setShowSymbol(false)
                .setTooltip(new Tooltip().setShow(false));
        //配置可容忍点数据
        ScatterSeries tolerate = new ScatterSeries()
                .setName("可容忍事件")
                .setSymbol("circle")
                .setData(tolerateData.getTolerateData());
        //配置不可容忍点数据
        ScatterSeries unTolerate = new ScatterSeries()
                .setName("不可容忍事件")
                .setSymbol("circle")
                .setData(tolerateData.getUnTolerateData());
        f47Option.setSeries(new SeriesOption[]{borderLimit, tolerate, unTolerate});
        return ENGINE.renderJsonOption(f47Option);
    }


    /***
     * 生成暂降幅值
     * @author hongawen
     * @date 2023/6/21 10:06
     */
    public static String generateEventAmplitudeOption(List<String> ylinedata, List<String> ybardata) {
        Option eventAmplitudeOption = new Option();
        //取消渲染动画
        eventAmplitudeOption.setAnimation(false);
        //背景色
        eventAmplitudeOption.setBackgroundColor(PicCommonData.PIC_BACK_COLOR);
        //标题
        eventAmplitudeOption.setTitle(new Title().setLeft(PicCommonData.CENTER).setText("暂降幅值的概率分布"));
        //设置图例
        eventAmplitudeOption.setLegend(new Legend().setData(new String[]{"概率分布", "占比"}).setLeft(10).setShow(true));
        //横坐标
        eventAmplitudeOption.setXAxis(new CategoryAxis()
                .setBoundaryGap(true)
                .setName("暂降幅值")
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
                .setData(new String[]{"0", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%"})
        );
        //纵坐标
        eventAmplitudeOption.setYAxis(new ValueAxis[]{
                new ValueAxis()
                        .setName("%")
                        .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
                        .setAxisLabel(new ValueAxisLabel().setFormatter("{value}%"))
        });

        //配置概率分布
        LineSeries probability = new LineSeries()
                .setName("概率分布")
                .setData(ylinedata);
        //配置占比
        BarSeries proportion = new BarSeries()
                .setName("占比")
                .setBarWidth(30)
                .setData(ybardata);

        eventAmplitudeOption.setSeries(new SeriesOption[]{probability, proportion});
        return ENGINE.renderJsonOption(eventAmplitudeOption);
    }

    /***
     * 生成持续时间
     * @author hongawen
     * @date 2023/6/21 10:06
     */
    public static String generatePersistentTimeOption(List<String> ylinedata, List<String> ybardata) {
        Option persistentTimeOption = new Option();
        //取消渲染动画
        persistentTimeOption.setAnimation(false);
        //背景色
        persistentTimeOption.setBackgroundColor(PicCommonData.PIC_BACK_COLOR);
        //标题
        persistentTimeOption.setTitle(new Title().setLeft(PicCommonData.CENTER).setText("持续时间的概率分布"));
        //设置图例
        persistentTimeOption.setLegend(new Legend().setData(new String[]{"概率分布", "占比"}).setLeft(10).setShow(true));
        //横坐标
        persistentTimeOption.setXAxis(new CategoryAxis()
                .setBoundaryGap(true)
                .setName("暂态持续时间(s)")
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
                .setData(new String[]{"0.01", "0.1", "0.25", "0.5", "1", "3", "20", "60", "180"})
        );
        //纵坐标
        persistentTimeOption.setYAxis(new ValueAxis[]{
                new ValueAxis()
                        .setName("%")
                        .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
                        .setAxisLabel(new ValueAxisLabel().setFormatter("{value}%"))
        });

        //配置概率分布
        LineSeries probability = new LineSeries()
                .setName("概率分布")
                .setData(ylinedata);
        //配置占比
        BarSeries proportion = new BarSeries()
                .setName("占比")
                .setBarWidth(30)
                .setData(ybardata);

        persistentTimeOption.setSeries(new SeriesOption[]{probability, proportion});
        return ENGINE.renderJsonOption(persistentTimeOption);
    }

    /***
     * 生成区域统计
     * @author hongawen
     * @date 2023/6/21 10:06
     */
    public static String generateRegionOption(List<String> xData, List<Integer> yData) {
        Option persistentTimeOption = new Option();
        //取消渲染动画
        persistentTimeOption.setAnimation(false);
        //背景色
        persistentTimeOption.setBackgroundColor(PicCommonData.PIC_BACK_COLOR);
        //标题
        persistentTimeOption.setTitle(new Title().setLeft(PicCommonData.CENTER).setText("区域统计"));
        //设置图例
        persistentTimeOption.setLegend(new Legend().setData(new String[]{"暂降次数"}).setLeft(10).setShow(true));
        //颜色
        persistentTimeOption.setColor("orange");
        //横坐标
        persistentTimeOption.setXAxis(new CategoryAxis()
                .setBoundaryGap(true)
                .setName("地区\n(监测点数)")
                .setAxisLabel(new LogAxisLabel().setShow(true).setFontSize(10).setRotate(0))
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
                .setData(xData.toArray())
        );
        //纵坐标
        persistentTimeOption.setYAxis(new ValueAxis()
                .setName("(次)")
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
                );


        //配置占比
        BarSeries proportion = new BarSeries()
                .setName("暂降次数")
                .setBarWidth(30)
                .setLabel(new BarLabel().setShow(true).setColor("#8B008B"))
                .setData(yData);
        persistentTimeOption.setSeries(new SeriesOption[]{proportion});
        return ENGINE.renderJsonOption(persistentTimeOption);
    }
    /***
     * 生成电压暂降，已关联未关联得次数
     * @author hongawen
     * @date 2023/6/21 10:06
     */
    public static String generateAssociatedUnassociatedOption(List<String> eventValue, List<Integer> pereventValue, List<Integer> pereventValue1, String xName) {
        Option persistentTimeOption = new Option();
        //取消渲染动画
        persistentTimeOption.setAnimation(false);
        //背景色
        persistentTimeOption.setBackgroundColor(PicCommonData.PIC_BACK_COLOR);
        //标题
        persistentTimeOption.setTitle(new Title().setLeft(PicCommonData.CENTER).setText(xName));
        //设置图例
        persistentTimeOption.setLegend(new Legend().setData(new String[]{"未关联暂降次数", "已关联处理事件"}).setLeft(10).setShow(true));
        //横坐标
        persistentTimeOption.setXAxis(new CategoryAxis()
                .setBoundaryGap(true)
                .setName(xName)
                .setAxisLabel(new LogAxisLabel().setShow(true).setFontSize(7).setRotate(0))
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
                .setData(eventValue.toArray())
        );
        //纵坐标
        persistentTimeOption.setYAxis(new ValueAxis()
                .setName("(次)")
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
        );


        //配置占比
        BarSeries proportion = new BarSeries()
                .setName("未关联暂降次数")
                .setStack("事件总数")
                .setItemStyle(new BarItemStyle().setColor("orange").setBorderColor("orange").setBorderWidth(2).setBorderRadius(0))
                .setLabel(new BarLabel().setShow(true).setColor("#8B008B"))
                .setData(pereventValue);
        BarSeries proportion2 = new BarSeries()
                .setName("已关联处理事件")
                .setStack("事件总数")
                .setItemStyle(new BarItemStyle().setColor("#ccc").setBorderColor("#ccc").setBorderWidth(2).setBorderRadius(0))
                .setLabel(new BarLabel().setShow(true).setColor("#8B008B"))
                .setData(pereventValue1);
        persistentTimeOption.setSeries(new SeriesOption[]{proportion,proportion2});
        return ENGINE.renderJsonOption(persistentTimeOption);
    }

    /***
     * 生成电压等级统计
     * @author hongawen
     * @date 2023/6/21 10:06
     */
    public static String generateVoltageLevelOption(List<String> xData, List<Integer> yData) {
        Option persistentTimeOption = new Option();
        //取消渲染动画
        persistentTimeOption.setAnimation(false);
        //背景色
        persistentTimeOption.setBackgroundColor(PicCommonData.PIC_BACK_COLOR);
        //标题
        persistentTimeOption.setTitle(new Title().setLeft(PicCommonData.CENTER).setText("电压等级"));
        //设置图例
        persistentTimeOption.setLegend(new Legend().setData(new String[]{"暂降次数"}).setLeft(10).setShow(true));
        //颜色
        persistentTimeOption.setColor("orange");
        //横坐标
        persistentTimeOption.setXAxis(new CategoryAxis()
                .setBoundaryGap(true)
                .setName("电压等级\n(监测点数)")
                .setAxisLabel(new LogAxisLabel().setShow(true).setFontSize(8).setRotate(0).setFontWeight("bold"))
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
                .setData(xData.toArray())
        );
        //纵坐标
        persistentTimeOption.setYAxis(new ValueAxis()
                .setName("(次)")
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
        );

        //配置占比
        BarSeries proportion = new BarSeries()
                .setName("暂降次数")
                .setBarWidth(30)
                .setLabel(new BarLabel().setShow(true).setColor("#8B008B"))
                .setData(yData);
        persistentTimeOption.setSeries(new SeriesOption[]{proportion});
        return ENGINE.renderJsonOption(persistentTimeOption);
    }


    /***
     * 生成设备运行情况
     * @author hongawen
     * @date 2023/6/21 10:06
     */
    public static String generateRegionDeviceOption(List<String> xData, List<Integer> onlineData,List<Integer> offlineData,String subtext,String xName) {
        Option persistentTimeOption = new Option();
        //取消渲染动画
        persistentTimeOption.setAnimation(false);
        //背景色
        persistentTimeOption.setBackgroundColor(PicCommonData.PIC_BACK_COLOR);
        //标题
        persistentTimeOption.setTitle(new Title().setLeft(PicCommonData.CENTER).setText("设备运行情况")
                .setSubtext(subtext)
        );
        //设置图例
        persistentTimeOption.setLegend(new Legend().setData(new String[]{"在线", "离线"}).setLeft(10).setShow(true));
        //横坐标
        persistentTimeOption.setXAxis(new CategoryAxis()
                .setBoundaryGap(true)
                .setName(xName)
                .setAxisLabel(new LogAxisLabel().setShow(true).setFontSize(7).setRotate(0).setFontWeight("bold"))
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
                .setData(xData.toArray())
        );
        //纵坐标
        persistentTimeOption.setYAxis(new ValueAxis()
                .setName("(次)")
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
        );


        //配置占比
        BarSeries proportion = new BarSeries()
                .setName("在线")
                .setStack("account")
                .setItemStyle(new BarItemStyle().setColor("green").setBorderColor("green").setBorderWidth(2).setBorderRadius(0))
                .setLabel(new BarLabel().setShow(true).setColor("#8B008B"))
                .setData(onlineData);
        BarSeries proportion2 = new BarSeries()
                .setName("离线")
                .setStack("account")
                .setItemStyle(new BarItemStyle().setColor("orange").setBorderColor("orange").setBorderWidth(2).setBorderRadius(0))
                .setLabel(new BarLabel().setShow(true).setColor("#8B008B"))
                .setData(offlineData);
        persistentTimeOption.setSeries(new SeriesOption[]{proportion,proportion2});
        return ENGINE.renderJsonOption(persistentTimeOption);
    }

    /***
     * 生成暂态事件次数
     * @author hongawen
     * @date 2023/6/21 10:06
     */
    public static String generateFrequencyOption(List<String> xData, List<Integer> dip,List<Integer> interruptions,List<Integer> rise,String subtext,String xName) {
        Option persistentTimeOption = new Option();
        //取消渲染动画
        persistentTimeOption.setAnimation(false);
        //背景色
        persistentTimeOption.setBackgroundColor(PicCommonData.PIC_BACK_COLOR);
        //标题
        persistentTimeOption.setTitle(new Title().setLeft(PicCommonData.CENTER).setText("暂态事件次数")
                .setSubtext(subtext)
        );
        //设置图例
        persistentTimeOption.setLegend(new Legend().setData(new String[]{"暂降次数", "中断次数","暂升次数"}).setLeft(10).setShow(true));
        //横坐标
        persistentTimeOption.setXAxis(new CategoryAxis()
                .setBoundaryGap(true)
                .setName(xName)
                .setAxisLabel(new LogAxisLabel().setShow(true).setFontSize(7).setRotate(0).setFontWeight("bold"))
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
                .setData(xData.toArray())
        );
        //纵坐标
        persistentTimeOption.setYAxis(new ValueAxis()
                .setName("(次)")
                .setNameTextStyle(new AxisNameTextStyle().setFontStyle("15px"))
        );


        //配置占比
        BarSeries proportion = new BarSeries()
                .setName("暂降次数")
                .setStack("account")
                .setItemStyle(new BarItemStyle().setColor("#ff9966").setBorderColor("#ff9966").setBorderWidth(2).setBorderRadius(0))
                .setLabel(new BarLabel().setShow(true).setColor("#8B008B"))
                .setData(dip);
        BarSeries proportion2 = new BarSeries()
                .setName("中断次数")
                .setStack("account")
                .setItemStyle(new BarItemStyle().setColor("#00dd00").setBorderColor("#00dd00").setBorderWidth(2).setBorderRadius(0))
                .setLabel(new BarLabel().setShow(true).setColor("#8B008B"))
                .setData(interruptions);
        BarSeries proportion3 = new BarSeries()
                .setName("暂升次数")
                .setStack("account")
                .setItemStyle(new BarItemStyle().setColor("#0066ff").setBorderColor("#0066ff").setBorderWidth(2).setBorderRadius(0))
                .setLabel(new BarLabel().setShow(true).setColor("#8B008B"))
                .setData(rise);
        persistentTimeOption.setSeries(new SeriesOption[]{proportion,proportion2,proportion3});
        return ENGINE.renderJsonOption(persistentTimeOption);
    }
}
