package com.bonus.common.core.utils.poi;

import com.itextpdf.text.DocumentException;
import org.apache.commons.io.IOUtils;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.DecimalFormat;

public interface DocumentUtil {

    /**
     * 创建柱状图，并插入到文档中
     *
     * @param title 图表标题
     * @param categoryAxisLabel 目录轴的标签
     * @param valueAxisLabel  数据轴的标签
     * @param  rowKeys 目录轴每行的key
     * @param  columnKeys 数据轴每列的key
     * @param data  数据
     * @return JFreeChart Jfreechart对象
     */
    public default JFreeChart createBarChart(String title, String categoryAxisLabel, String valueAxisLabel,
                                       String[] rowKeys, String[] columnKeys, double[][] data) throws IOException, DocumentException {
        //  使用 JFreeChart 创建一个柱状图
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (int i = 0; i < rowKeys.length; i++) {
            for (int j = 0; j < columnKeys.length; j++) {
                dataset.addValue(data[i][j], rowKeys[i], columnKeys[j]);
            }
        }

        JFreeChart barChart = ChartFactory.createBarChart(
                title,
                categoryAxisLabel,
                valueAxisLabel,
                dataset,
                PlotOrientation.VERTICAL,
                true, true, false);

        //将图表保存到字节数组输出流中
        ByteArrayOutputStream chartOut = new ByteArrayOutputStream();
        ChartUtils.writeChartAsPNG(chartOut, barChart, 400, 400);
        return barChart;
    }

    /**
     * 创建饼状图，并插入到文档中
     *
     * @param title 图表标题
     * @param categories 目录轴的标签
     * @param values  数据轴的标签、
     * @return JFreeChart Jfreechart对象
     */
    public default JFreeChart createPieChart(String title, String[] categories, double[] values) throws IOException, DocumentException {
        // 使用 JFreeChart 创建一个饼状图
        DefaultPieDataset dataset = new DefaultPieDataset();
        for (int i = 0; i < categories.length; i++) {
            dataset.setValue(categories[i], values[i]);
        }

        JFreeChart pieChart = ChartFactory.createPieChart(title, dataset, true, true, false);
        // 获取饼状图的 Plot 对象
        PiePlot plot = (PiePlot) pieChart.getPlot();
        // 设置标签格式
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
                "{2}", new DecimalFormat("0"), new DecimalFormat("0%")));

        plot.setSimpleLabels(true);
        return pieChart;
    }

    public default void fileDown(String outputDir, String fileName, HttpServletResponse response) throws IOException {
        try (FileInputStream is = new FileInputStream(new File(outputDir))) {
            response.setContentType("text/html;charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-disposition", "attachment;filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
            ServletOutputStream os = response.getOutputStream();
            IOUtils.copy(is, os);
        }
    }

}
