package com.hdkj.lt.utils.poi;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.ChartMultiSeriesRenderData;
import com.deepoove.poi.data.SeriesRenderData;
import com.deepoove.poi.util.PoitlIOUtils;
import com.hdkj.lt.base.exception.ExceptionCast;
import com.hdkj.lt.utils.file.FileUtils;
import com.hdkj.lt.core.hussar.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @Description:
 * @Author: wenhao
 * @Create: 2024/01/26 9:48
 * @Version: 1.0
 */
@Slf4j
public class WordUtils {

    private WordUtils() {
    }

    public static MultipartFile createToMultipartFile(String templateUrl, String fileName, Configure configure, HashMap<String, Object> dataMap) {
        DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem(fileName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
        try (InputStream is = WordUtils.class.getClassLoader().getResourceAsStream(templateUrl);
             OutputStream os = new BufferedOutputStream(fileItem.getOutputStream(), FileUtils.BUFFER_SIZE);
             XWPFTemplate template = XWPFTemplate.compile(is, configure).render(dataMap);) {
            if (Objects.isNull(is) || is.available() <= 0) {
                ExceptionCast.cast("Word模板配置有误！");
            }
            template.writeAndClose(os);
            PoitlIOUtils.closeQuietlyMulti(template, os, is);
            return new CommonsMultipartFile(fileItem);
        } catch (IOException e) {
            log.error("生成Word文档失败：{}", e.getMessage());
            ExceptionCast.cast(String.format("生成Word文档失败：%s", e.getMessage()));
        }
        return null;
    }

    public static File createToFile(String templateUrl, String fileName, Configure configure, HashMap<String, Object> dataMap) {
        //todo: 配置参数
//        String tempDir = SpringContextHolder.environment.getProperty("lt.temp-dir");
        String tempDir = "/temp-dir";
        File file = FileUtils.createFile(tempDir, fileName);
        try (InputStream is = WordUtils.class.getClassLoader().getResourceAsStream(templateUrl);
             XWPFTemplate template = XWPFTemplate.compile(is, configure).render(dataMap);
             OutputStream os = new BufferedOutputStream(Files.newOutputStream(file.toPath()), FileUtils.BUFFER_SIZE);) {
            if (Objects.isNull(is) || is.available() <= 0) {
                ExceptionCast.cast("Word模板配置有误！");
            }
            template.writeAndClose(os);
            PoitlIOUtils.closeQuietlyMulti(template, os, is);
            return file;
        } catch (IOException e) {
            log.error("生成Word文档失败：{}", e.getMessage());
            ExceptionCast.cast(String.format("生成Word文档失败：%s", e.getMessage()));
        }
        return null;
    }

    /**
     * 封装报表chart图表参数
     */
    private SeriesRenderData getSeriesRenderData(String name, Number[] values, SeriesRenderData.ComboType comboType) {
        SeriesRenderData renderData = new SeriesRenderData();
        renderData.setName(name);
        renderData.setValues(values);
        renderData.setComboType(comboType);
        return renderData;
    }

    /**
     * 报表chart图表参数赋值
     */
    private ChartMultiSeriesRenderData getChartSeriesRenderData(String chartTitle, String[] categories, List<SeriesRenderData> seriesDatas) {
        ChartMultiSeriesRenderData renderData = new ChartMultiSeriesRenderData();
        renderData.setChartTitle(chartTitle);
        renderData.setCategories(categories);
        renderData.setSeriesDatas(seriesDatas);
        return renderData;
    }

}
