package cn.wise.sc.energy.power.plant.business.service.impl;

import cn.afterturn.easypoi.entity.ImageEntity;
import jdk.nashorn.internal.ir.ReturnNode;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author neo.shu
 * @since 2020/9/28 14:13
 * 二叉树报告service
 */
@Service
public class BtreeReportServiceImpl {


    public Map fetchData() {

        return null;
    }


    public Map<String, List<SpiderWebPoint>> simulateSpiderData() {
        Map<String, List<SpiderWebPoint>> map = new HashMap<>();
        List<SpiderWebPoint> a = new ArrayList<>();
        a.add(new SpiderWebPoint("热工参数", 3.0));
        a.add(new SpiderWebPoint("热工参数", 3.0));
        a.add(new SpiderWebPoint("热工参数", 3.0));
        return  null;
    }
    /**
     * 生成雷达图
     * @param title
     * @param data
     * @return
     */
    public ImageEntity spiderWebPlot(String title, Map<String, List<SpiderWebPoint>> data) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        data.forEach((key,value)->{
            value.forEach(
                    item->{
                        dataset.setValue(item.getValue(),key,item.getColumnKey());
                    }
            );
        });
        SpiderWebPlot spiderwebplot = new SpiderWebPlot(dataset);

        JFreeChart jfreechart = new JFreeChart(title, TextTitle.DEFAULT_FONT, spiderwebplot, false);
        LegendTitle legendtitle = new LegendTitle(spiderwebplot);
        legendtitle.setPosition(RectangleEdge.TOP);
        jfreechart.addSubtitle(legendtitle);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ChartUtils.writeChartAsPNG(out, jfreechart, 400, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ImageEntity(out.toByteArray(), 200, 200);
    }
    @Data
    @AllArgsConstructor
    class SpiderWebPoint {
        private String columnKey;
        private double value;
    }
}
