package org.dromara.system.api.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

/**
 * @author lilemy
 * @date 2025/4/23 10:42
 */
@Slf4j
public class Dxf2JsonUtil {

    /**
     * dxf转json
     *
     * @param exePath        dxf2json.exe路径
     * @param inputDXFPath   输入dxf文件路径
     * @param outputJSONPath 输出json文件路径
     * @param sourceEPSG     源坐标系
     * @param targetEPSG     目标坐标系
     */
    public static void dxf2json(String exePath, String inputDXFPath, String outputJSONPath, String sourceEPSG, String targetEPSG) {
        // 判断对应文件是否存在
        File exeFile = new File(exePath);
        if (!exeFile.exists()) {
            throw new ServiceException("转换程序不存在！");
        }
        File inputDXFFile = new File(inputDXFPath);
        if (!inputDXFFile.exists()) {
            throw new ServiceException("待转换 dxf 文件不存在！");
        }
        // 构造命令行参数
        List<String> parameters = buildParameter(exePath, inputDXFPath, outputJSONPath, sourceEPSG, targetEPSG);
        // 执行命令行
        ProcessBuilder builder = new ProcessBuilder(parameters);
        // 合并标准错误和输出
        builder.redirectErrorStream(true);
        try {
            Process process = builder.start();
            // 读取输出
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream(), "GBK")
            );
            String line;
            log.info("dxf 转 json 程序开始执行，程序路径：{}，输入 dxf 路径：{}，输出 json 文件路径：{}，源坐标系：{}，模板坐标系：{}",
                exePath, inputDXFPath, outputJSONPath, sourceEPSG, targetEPSG);
            while ((line = reader.readLine()) != null) {
                log.info("dxf 转 json 程序执行中：{}", line);
                JSONObject jsonObject = JSONUtil.parseObj(line);
                Integer code = jsonObject.get("code", Integer.class);
                if (code != 0 && code != 200) {
                    throw new ServiceException("dxf 转 json 程序执行出错！");
                }
            }
            int exitCode = process.waitFor();
            log.info("dxf 转 json 程序执行完毕，程序退出码：{}", exitCode);
            reader.close();
        } catch (IOException | InterruptedException e) {
            log.error("执行 dxf 转 json 命令行时出错", e);
        }
    }

    /**
     * 构造命令行参数
     *
     * @param exePath        dxf2json.exe路径
     * @param inputDXFPath   输入dxf文件路径
     * @param outputJSONPath 输出json文件路径
     * @param sourceEPSG     源坐标系
     * @param targetEPSG     目标坐标系
     * @return 命令行参数
     */
    public static List<String> buildParameter(String exePath,
                                              String inputDXFPath,
                                              String outputJSONPath,
                                              String sourceEPSG,
                                              String targetEPSG) {
        // 构造命令行
        return Arrays.asList(
            exePath,
            inputDXFPath,
            outputJSONPath,
            sourceEPSG,
            targetEPSG
        );
    }

}
