package com.ims.eval.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileTypeUtil;
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.OpenOfficeDocumentConverter;

import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hlf
 * @date 2023/6/1 17:30
 * 文件说明:
 */
public class FileConversionUtil {

	//启动OpenOffice服务
	static {
		List<String> command = new ArrayList<>();
		//OpenOffice的安装目录下的soffice路径D:\OpenOffice 4\program\soffice.exe
		command.add("D:\\OpenOffice 4\\program\\soffice.exe");
		command.add("-headless");
		command.add("-accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard");
		command.add("-nofirststartwizard");
		command.forEach(v -> System.out.print(v + " "));
		System.out.println();
		ProcessBuilder builder = new ProcessBuilder();
		//正常信息和错误信息合并输出
		builder.redirectErrorStream(true);
		builder.command(command);
		//开始执行命令
		try {
			builder.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * office文件转换成pdf文件
	 *
	 * @param fromFilePath 要转换文件的文件路径
	 * @param saveFilePath 转换完后文件的保存路径
	 * @return 返回最后转换后的文件名
	 */
	public static String conversionToPdf(String fromFilePath, String saveFilePath) {
		String timesuffix = DateUtil.format(new Date(), "yyyyMMddHHmmss");
		String docFileName;
		String resultFileName;
		// 识别文件类型
		String fileType = "";
		try (FileInputStream fromFileInputStream = new FileInputStream(fromFilePath)) {
			fileType = FileTypeUtil.getType(fromFileInputStream);
		} catch (FileNotFoundException e) {
			return "待转换的文件不存在";
		} catch (IOException e) {
			return "文件读取失败";
		}
		if ("doc".equals(fileType)) {
			docFileName = "doc_" + timesuffix + ".doc";
			resultFileName = "doc_" + timesuffix + ".pdf";
		} else if ("docx".equals(fileType)) {
			docFileName = "docx_" + timesuffix + ".docx";
			resultFileName = "docx_" + timesuffix + ".pdf";
		} else if ("xls".equals(fileType)) {
			docFileName = "xls_" + timesuffix + ".xls";
			resultFileName = "xls_" + timesuffix + ".pdf";
		} else if ("xlsx".equals(fileType)) {
			docFileName = "xlsx_" + timesuffix + ".xlsx";
			resultFileName = "xlsx_" + timesuffix + ".pdf";
		} else if ("ppt".equals(fileType)) {
			docFileName = "ppt_" + timesuffix + ".ppt";
			resultFileName = "ppt_" + timesuffix + ".pdf";
		} else if ("pptx".equals(fileType)) {
			docFileName = "pptx_" + timesuffix + ".pptx";
			resultFileName = "pptx_" + timesuffix + ".pdf";
		} else {
			return "转换错误,文件后缀不是doc、docx、xls、xlsx、ppt、pptx";
		}
		// 将待转文件拷贝一份写入到saveFilePath下
		File docInputFile = new File(saveFilePath + File.separatorChar + docFileName);
		File resultOutputFile = new File(saveFilePath + File.separatorChar + resultFileName);
		if (resultOutputFile.exists()) {
			resultOutputFile.delete();
		}
		try (OutputStream os = Files.newOutputStream(docInputFile.toPath())) {
			try (FileInputStream fromFileInputStream = new FileInputStream(fromFilePath)) {
				int bytesRead = 0;
				byte[] buffer = new byte[1024 * 8];
				while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
					os.write(buffer, 0, bytesRead);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 连接OpenOffice服务。需提前开启OpenOffice服务,否则会报错
		OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
		try {
			connection.connect();
		} catch (Exception e) {
			System.out.println("连接OpenOffice服务失败,请检查是否启动OpenOffice服务");
		}
		// 转化,将saveFilePath下的拷贝的原始文件转化为pdf
		DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
		try {
			converter.convert(docInputFile, resultOutputFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 转换完之后删除拷贝的原始文件
		docInputFile.delete();
		connection.disconnect();
		return resultFileName;
	}
}