package com.ruoyi.common.utils.bean;

import org.jodconverter.office.DefaultOfficeManagerBuilder;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.io.File;
import java.util.regex.Pattern;

/**
 * 项目启动 启动LibreOffice 服务
 *
 * @author lihao16
 */
@Component
public class OfficeManagerBean {

    private static String officeHomeDir = null;

    private static OfficeManager officeManager;

    private static final Logger log = LoggerFactory.getLogger(OfficeManagerBean.class);

    @Bean
    public OfficeManager officeManager() {
        String libreOfficePath = getOfficeHome();

        DefaultOfficeManagerBuilder builder = new DefaultOfficeManagerBuilder();
        builder.setOfficeHome(new File(libreOfficePath));
        // 端口号
        builder.setPortNumber(9900);
        // 设置任务执行超时为5分钟
        builder.setTaskExecutionTimeout(1000 * 60 * 5L);
        // 设置任务队列超时为24小时
        builder.setTaskQueueTimeout(1000 * 60 * 60 * 24L);

        officeManager = builder.build();
        startService(officeManager);
        return officeManager;
    }

    @PreDestroy
    public void destroyOfficeManager() throws OfficeException {
        this.stopService(officeManager);
    }

    private void stopService(OfficeManager officeManager) throws OfficeException {
        if (null != officeManager) {
            officeManager.stop();
        }
        log.info("关闭office转换成功!");
    }

    private static void startService(OfficeManager officeManager) {

        try {
            if (!officeManager.isRunning()) {
                System.out.println(officeManager.isRunning());
                log.info("LibreOffice is not running,starting now...");
                // 准备启动服务
                officeManager.start(); // 启动服务
            }
            log.info("office转换服务启动成功");
        } catch (Exception ce) {
            log.error("office转换服务启动失败!详细信息:{}", ce);
        }
    }

    private static String getOfficeHome() {

        if (null != officeHomeDir) {
            return officeHomeDir;
        } else {
            String osName = System.getProperty("os.name");
            if (Pattern.matches("Windows.*", osName)) {
                officeHomeDir = "C:/Program Files/LibreOffice";
                return officeHomeDir;
            } else if (Pattern.matches("Linux.*", osName)) {
                officeHomeDir = "/opt/libreoffice7.5";
                return officeHomeDir;
            } else if (Pattern.matches("Mac.*", osName)) {
                officeHomeDir = "/Application/openOfficeSoft";
                return officeHomeDir;
            }
            return null;
        }
    }

    /**
     *
     *@name    设置libreOffice安装目录
     *@description 相关说明：如果libreOffice安装目录为默认目录，则不需要设置，否则需要设置
     *@time    创建时间:2018年9月17日下午1:52:36
     *@param officeHome
     *@author   作者
     *@history 修订历史（历次修订内容、修订人、修订时间等）
     */
    public static void setOfficeHome(String officeHome) {
        officeHomeDir = officeHome;
    }

}
