package com.njcn.echarts.util;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.njcn.echarts.json.BarGenerator;
import com.njcn.echarts.json.LineGenerator;
import com.njcn.echarts.json.PieGenerator;
import com.njcn.web.utils.RestTemplateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年08月18日 10:27
 */
@Component
@RequiredArgsConstructor
public class DrawPicUtil {

    //目前写死，后续作为配置
    @Value("${DrawPicUtil.URL:http://127.0.0.1:3008/generate-chart}")
    private String URL;


    /**
     * 请求目标服务器绘制echart图
     *
     * @param option 图形参数
     * @return 图形的base64数据
     */
    private String drawPic(String option) {
        return drawPic(option, 0, 0);
    }

    /**
     * 请求目标服务器绘制echart图
     *
     * @param option 图形参数
     * @param width  图形宽度
     * @param height 图形高度
     * @return 图形的base64数据
     */
    private String drawPic(String option, int width, int height) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("width", width == 0 ? 950 : width);
        jsonObject.set("height", height == 0 ? 400 : height);
        jsonObject.set("option", JSONUtil.parseObj(option));
        ResponseEntity<String> picResult = RestTemplateUtil.post(URL, jsonObject, String.class);
        return Objects.requireNonNull(picResult.getBody()).indexOf("image/png") > 0 ? picResult.getBody() : "";
    }

    /***
     * @author hongawen
     * 绘制波形图
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawWavePic(String title, List<List<Float>> aValue, List<List<Float>> bValue, List<List<Float>> cValue, String unit, Float max, Float min, String a, String b, String c, List<String> colors, Boolean isOpen) {
        return drawWavePic(title, aValue, bValue, cValue, unit, max, min, a, b, c, colors, isOpen, 0, 0);
    }

    /***
     * 绘制波形图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawWavePic(String title, List<List<Float>> aValue, List<List<Float>> bValue, List<List<Float>> cValue, String unit, Float max, Float min, String a, String b, String c, List<String> colors, Boolean isOpen, int width, int height) {
        String instantJson = LineGenerator.generateWaveOption(title, aValue, bValue, cValue, unit, max, min, a, b, c, colors, isOpen);
        return drawPic(instantJson, width, height);
    }

    public String drawWavePic(String title, List<List<Float>> aValue, List<List<Float>> bValue, List<List<Float>> cValue, String unit, Float max, Float min, String a, String b, String c, List<String> colors, Boolean isOpen, Double lastTime, List<Float> sharedNullBoundaryXValues,Integer nPush) {
        String instantJson = LineGenerator.generateWaveOption(title, aValue, bValue, cValue, unit, max, min, a, b, c, colors, isOpen, lastTime, sharedNullBoundaryXValues,nPush);
        return drawPic(instantJson, 0, 0);
    }


    /***
     * 绘制itic曲线图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     * @param originData 原始数据
     */
    public String drawItic(List<List<Double>> originData) {
        return drawItic(originData, 0, 0);
    }

    /***
     * 绘制itic曲线图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     * @param originData 原始数据
     */
    public String drawItic(List<List<Double>> originData, int width, int height) {
        String iticJson = LineGenerator.generateIticOption(BusinessDataUtil.dealIticData(originData));
        return drawPic(iticJson, width, height);
    }

    /***
     * 绘制f47曲线图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     * @param originData 原始数据
     */
    public String drawF47(List<List<Double>> originData) {
        return drawF47(originData, 0, 0);
    }

    /***
     * 绘制f47曲线图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     * @param originData 原始数据
     */
    public String drawF47(List<List<Double>> originData, int width, int height) {
        String f47Json = LineGenerator.generateF47Option(BusinessDataUtil.dealF47Data(originData));
        return drawPic(f47Json, width, height);
    }

    /***
     * 绘制暂降幅值概率分布图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventAmplitude(List<String> ylinedata, List<String> ybardata) {
        return drawEventAmplitude(ylinedata, ybardata, 0, 0);
    }

    /***
     * 绘制暂降幅值概率分布图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventAmplitude(List<String> ylinedata, List<String> ybardata, int width, int height) {
        String eventAmplitudeJson = LineGenerator.generateEventAmplitudeOption(ylinedata, ybardata);
        return drawPic(eventAmplitudeJson, width, height);
    }

    /***
     * 绘制持续时间概率分布图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawPersistentTime(List<String> ylinedata, List<String> ybardata) {
        return drawPersistentTime(ylinedata, ybardata, 0, 0);
    }

    /***
     * 绘制持续时间概率分布图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawPersistentTime(List<String> ylinedata, List<String> ybardata, int width, int height) {
        String persistentTimeJson = LineGenerator.generatePersistentTimeOption(ylinedata, ybardata);
        return drawPic(persistentTimeJson, width, height);
    }


    /***
     * 绘制生成月份统计图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawMonth(List<String> xName, List<Integer> times, String year, Integer flag) {
        return drawMonth(xName, times, year, flag, 0, 0);
    }

    /***
     * 绘制生成月份统计图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawMonth(List<String> xName, List<Integer> times, String year, Integer flag, int width, int height) {
        String monthJson = BarGenerator.generateMonthOption(xName, times, year, flag);
        return drawPic(monthJson, width, height);
    }

    /***
     * 绘制生成暂降原因图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawReason(List<String> xname, List<Map<String, Object>> map2) {
        return drawReason(xname, map2, 0, 0);
    }

    /***
     * 绘制生成暂降原因图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawReason(List<String> xname, List<Map<String, Object>> map2, int width, int height) {
        String monthJson = PieGenerator.generateReasonOption(xname, map2);
        return drawPic(monthJson, width, height);
    }

    /***
     * 绘制生成暂降类型图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawType(List<String> xname, List<Map<String, Object>> map2) {
        return drawType(xname, map2, 0, 0);
    }

    /***
     * 绘制生成暂降类型图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawType(List<String> xname, List<Map<String, Object>> map2, int width, int height) {
        String monthJson = PieGenerator.generateTypeOption(xname, map2);
        return drawPic(monthJson, width, height);
    }


    /***
     * 绘制生成暂降密度图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventDensity(Number[][] data) {
        return drawEventDensity(data, 0, 0);
    }

    /***
     * 绘制生成暂降类型图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventDensity(Number[][] data, int width, int height) {
        String eventDensityJson = BarGenerator.generateEventDensity(data);
        return drawPic(eventDensityJson, width, height);
    }

    /***
     * 绘制生成区域统计图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventRegion(List<String> xData, List<Integer> yData) {
        return drawEventRegion(xData, yData, 0, 0);
    }

    /***
     * 绘制生成区域统计图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventRegion(List<String> xData, List<Integer> yData, int width, int height) {
        String eventDensityJson = LineGenerator.generateRegionOption(xData, yData);
        return drawPic(eventDensityJson, width, height);
    }

    /***
     * 绘制生成电压暂降，已关联未关联得次数图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventAssociatedUnassociated(List<String> eventValue, List<Integer> pereventValue, List<Integer> pereventValue1, String xName) {
        return drawEventAssociatedUnassociated(eventValue, pereventValue, pereventValue1, xName, 0, 0);
    }

    /***
     * 绘制生成电压暂降，已关联未关联得次数图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventAssociatedUnassociated(List<String> eventValue, List<Integer> pereventValue, List<Integer> pereventValue1, String xName, int width, int height) {
        String eventDensityJson = LineGenerator.generateAssociatedUnassociatedOption(eventValue, pereventValue, pereventValue1, xName);
        return drawPic(eventDensityJson, width, height);
    }

    /***
     * 绘制生成区域统计图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventVoltageLevel(List<String> xData, List<Integer> yData) {
        return drawEventVoltageLevel(xData, yData, 0, 0);
    }

    /***
     * 绘制生成区域统计图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventVoltageLevel(List<String> xData, List<Integer> yData, int width, int height) {
        String eventDensityJson = LineGenerator.generateVoltageLevelOption(xData, yData);
        return drawPic(eventDensityJson, width, height);
    }

    /***
     * 绘制生成设备运行情况图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventRegionDevice(List<String> xData, List<Integer> onlineData, List<Integer> offlineData, String subtext, String xName) {
        return drawEventRegionDevice(xData, onlineData, offlineData, subtext, xName, 0, 0);
    }

    /***
     * 绘制生成设备运行情况图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventRegionDevice(List<String> xData, List<Integer> onlineData, List<Integer> offlineData, String subtext, String xName, int width, int height) {
        String eventDensityJson = LineGenerator.generateRegionDeviceOption(xData, onlineData, offlineData, subtext, xName);
        return drawPic(eventDensityJson, width, height);
    }

    /***
     * 绘制生成暂态事件次数图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventFrequency(List<String> xData, List<Integer> dip, List<Integer> interruptions, List<Integer> rise, String subtext, String xName) {
        return drawEventFrequency(xData, dip, interruptions, rise, subtext, xName, 0, 0);
    }

    /***
     * 绘制生成暂态事件次数图
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawEventFrequency(List<String> xData, List<Integer> dip, List<Integer> interruptions, List<Integer> rise, String subtext, String xName, int width, int height) {
        String eventDensityJson = LineGenerator.generateFrequencyOption(xData, dip, interruptions, rise, subtext, xName);
        return drawPic(eventDensityJson, width, height);
    }

    /***
     * 绘制App越线指标图(单相)
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawSteadyTargetPic(String title,Map<String ,List<List<Object>>>  values,String limitName,List<List<Object>> limit) {
        return drawSteadyTargetPic(title, values, limitName, limit, 0, 0);
    }

    /***
     * 绘制App越线指标图(单相)
     * @author hongawen
     * @date 2023/6/21 11:01
     * @return String base64数据
     */
    public String drawSteadyTargetPic(String title,Map<String ,List<List<Object>>>  values,String limitName,List<List<Object>> limit,int width, int height) {
        String instantJson = LineGenerator.generateSteadyTargetOption(title, values, limitName, limit);
        return drawPic(instantJson, width, height);
    }

}
