package shop.alien.util.openoffice;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import shop.alien.util.ip.IpConnectTestUtil;

import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * windows下使用openoffice把office文件转换成pdf
 *
 * @author ssk
 */
public class OpenOfficeUtil {

    public static void main(String[] args) {
        try {
            // OpenOffice的安装目录
            String OpenOfficeHOME = "D:\\software\\openOffice";
            // 如果从文件中读取的URL地址最后一个字符不是 '\'，则添加'\'
            if (OpenOfficeHOME.charAt(OpenOfficeHOME.length() - 1) != '\\') {
                OpenOfficeHOME += "\\";
            }
            // 启动OpenOffice的服务
            String command = OpenOfficeHOME + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
            Process pro = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            System.out.println("openoffice:服务开启失败");
        }
        List<Boolean> list = new ArrayList<>();
        int count = 0;
        list.add(officeToPDF("D:\\MyTest\\1.txt", "D:\\MyTest\\new\\TXT_1.pdf"));
        list.add(officeToPDF("D:\\MyTest\\test.doc", "D:\\MyTest\\new\\DOC_test.pdf"));
        list.add(officeToPDF("D:\\MyTest\\test.docx", "D:\\MyTest\\new\\DOCX_test.pdf"));
        list.add(officeToPDF("D:\\MyTest\\test.xls", "D:\\MyTest\\new\\XLS_test.pdf"));
        list.add(officeToPDF("D:\\MyTest\\test.xlsx", "D:\\MyTest\\new\\XLSX_test.pdf"));
        list.add(officeToPDF("D:\\MyTest\\test.ppt", "D:\\MyTest\\new\\PPT_test.pdf"));
        list.add(officeToPDF("D:\\MyTest\\test.pptx", "D:\\MyTest\\new\\PPTX_test.pdf"));
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == true) {
                count++;
            }
        }
        if (list.size() == count) {
            String srcFilePath = "D:\\MyTest\\new";
            String destFilePathAndName = srcFilePath;
//            ZipUtil zipUtil = new ZipUtil();
//            zipUtil.compress(srcFilePath, destFilePathAndName);
        } else {
            System.err.println("有文件未转换成功");
        }
    }

    public static boolean officeToPDF(String sourceFile, String destFile) {
        try {
            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                // 找不到源文件, 则返回false
                System.err.println("文件不存在!");
            }
            // 如果目标路径不存在, 则新建该路径
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            // 如果目标文件存在，则删除
            if (outputFile.exists()) {
                outputFile.delete();
            }
            //测试是否开启
            IpConnectTestUtil testIpConnectUtil = new IpConnectTestUtil();
            //等待开启
            if (!testIpConnectUtil.ipTest("127.0.0.1", 8100)) {
                //服务未开启,直接停止
                System.err.println("openoffice:服务未开启!");
            }
            System.out.println("openoffice:服务已开启");
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
            connection.connect();
            // 用于测试openOffice连接时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("连接时间:" + sdf.format(new Date()));
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            // 传入文件
            converter.convert(inputFile, outputFile);
            // 测试word转PDF的转换时间
            System.out.println("转换时间:" + sdf.format(new Date()));
            connection.disconnect();
            return true;
        } catch (ConnectException e) {
            System.err.println("openOffice连接失败！请检查IP,端口");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}