package com.ruoyi.project.officeConvert;

import com.ruoyi.common.constant.Constants;
import org.apache.poi.sl.draw.Drawable;
import org.apache.poi.sl.usermodel.TextRun;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.hslf.usermodel.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class PptPreview {

    //pptx转为图片的方法
    public static List pptToImage(File pptFile, File imgFile,String newName) {
        //pptx转为图片 利用轮播图 返回 图片的路径地址
        List<String> list = new ArrayList<>();
        FileInputStream is = null ;
        int imgCount = 0;
        try {
            is = new FileInputStream(pptFile);
            XMLSlideShow xmlSlideShow = new XMLSlideShow(is);
            is.close();
            // 获取大小
            Dimension pgsize = xmlSlideShow.getPageSize();
            // 获取幻灯片
            List<XSLFSlide> slides = xmlSlideShow.getSlides();
            imgCount = slides.size();
            for (int i = 0 ; i < slides.size() ; i++) {
                // 解决乱码问题
                List<XSLFShape> shapes = slides.get(i).getShapes();
                for (XSLFShape shape : shapes) {
                    if (shape instanceof XSLFTextShape) {
                        XSLFTextShape sh = (XSLFTextShape) shape;
                        List<XSLFTextParagraph> textParagraphs = sh.getTextParagraphs();
                        for (XSLFTextParagraph xslfTextParagraph : textParagraphs) {
                            List<XSLFTextRun> textRuns = xslfTextParagraph.getTextRuns();
                            for (XSLFTextRun xslfTextRun : textRuns) {
                                xslfTextRun.setFontFamily("宋体");
                            }
                        }
                    }
                }
                //根据幻灯片大小生成图片

                BufferedImage img = new BufferedImage(pgsize.width * 3,pgsize.height * 3, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                graphics.setPaint(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,pgsize.height));
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                graphics.setRenderingHint(Drawable.BUFFERED_IMAGE, new WeakReference<>(img));
                graphics.scale(3 , 3);

                //每个文件路径对应自己文件名的文件夹，以区别防止文件名也重复
                // 最核心的代码
                slides.get(i).draw(graphics);
                //图片将要存放的路径
                String absolutePath = imgFile.getAbsolutePath()+"/"+ newName+(i + 1) + ".jpeg";
                File jpegFile = new File(absolutePath);
                // 图片路径存放
                list.add(Constants.RESOURCE_PREFIX+ "/temp/"+newName+(i + 1) + ".jpeg");
                /*//如果图片存在，则不再生成
                if (jpegFile.exists()) {
                    continue;
                }*/
                // 这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径
                FileOutputStream out = new FileOutputStream(jpegFile);
                // 写入到图片中去
                ImageIO.write(img, "jpeg", out);
                out.close();
            }
            System.out.print("PPT转换成图片 成功！");
            //log.error("PPT转换成图片 成功！");
            return list;
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.print("PPT转换成图片 发生异常！");
            // log.error("PPT转换成图片 发生异常！", e);
        }
        return list;
    }

    //ppt和pptx仅用的操作类不一样，其他逻辑一致
    public static List pptxToImage(File pptFile, File imgFile,String newName) {
        //ppt转为图片 利用轮播图 返回 图片的路径地址
        List<String> list = new ArrayList<>();
        FileInputStream is = null ;
        int imgCount = 0;
        try {
            is = new FileInputStream(pptFile);
            //使用hslf即可
            HSLFSlideShow ppt = new HSLFSlideShow(is);
            //及时关闭掉 输入流
            is.close();
            Dimension pgsize = ppt.getPageSize();
            List<HSLFSlide> slide = ppt.getSlides();
            for (int i = 0; i < slide.size(); i++) {

                List<HSLFShape> shapes = slide.get(i).getShapes();
                for (HSLFShape shape : shapes) {
                    if (shape instanceof HSLFTextShape) {
                        HSLFTextShape sh = (HSLFTextShape) shape;
                        List<HSLFTextParagraph> textParagraphs = sh.getTextParagraphs();
                        for (HSLFTextParagraph xslfTextParagraph : textParagraphs) {
                            List<HSLFTextRun> textRuns = xslfTextParagraph.getTextRuns();
                            for (HSLFTextRun xslfTextRun : textRuns) {
                                xslfTextRun.setFontFamily("宋体");
                            }
                        }
                    }
                }
                //根据幻灯片大小生成图片

                BufferedImage img = new BufferedImage(pgsize.width * 3,pgsize.height * 3, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                graphics.setPaint(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,pgsize.height));
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                graphics.setRenderingHint(Drawable.BUFFERED_IMAGE, new WeakReference<>(img));
                graphics.scale(3 ,3 );

                //每个文件路径对应自己文件名的文件夹，以区别防止文件名也重复
                // 最核心的代码
                slide.get(i).draw(graphics);
                //图片将要存放的路径
                String absolutePath = imgFile.getAbsolutePath()+"/"+ newName+(i + 1) + ".jpeg";
                File jpegFile = new File(absolutePath);
                // 图片路径存放
                list.add(Constants.RESOURCE_PREFIX+ "/temp/"+newName+(i + 1) + ".jpeg");
                /*//如果图片存在，则不再生成
                if (jpegFile.exists()) {
                    continue;
                }*/
                // 这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径
                FileOutputStream out = new FileOutputStream(jpegFile);
                // 写入到图片中去
                ImageIO.write(img, "jpeg", out);
                out.close();
            }
            System.out.print("PPT转换成图片 成功！");
            //log.error("PPT转换成图片 成功！");
            return list;
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.print("PPT转换成图片 发生异常！");
            // log.error("PPT转换成图片 发生异常！", e);
        }
        return list;
    }
}
