package com.cn.fdapfe.biz.service.impl;

import com.alibaba.fastjson2.JSON;
import com.cn.fdapfe.biz.domain.Data;
import com.cn.fdapfe.biz.domain.FaultPhysicalModel;
import com.cn.fdapfe.biz.mapper.FaultPhysicalModelMapper;
import com.cn.fdapfe.biz.service.IDataService;
import com.cn.fdapfe.common.utils.http.HttpClientUtils;
import com.cn.fdapfe.web.controller.common.CommonController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cn.fdapfe.biz.domain.ModelDataGen;
import com.cn.fdapfe.biz.mapper.ModelDataGenMapper;
import com.cn.fdapfe.biz.service.IModelDataGenService;
import com.cn.fdapfe.common.utils.DateUtils;
import com.cn.fdapfe.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.regex.Pattern;

/**
 * 物理模型数据生成管理Service业务层处理
 *
 * @author Allen
 * @date 2025-03-07
 */
@Service
public class ModelDataGenServiceImpl implements IModelDataGenService {
    private static final Logger logger = LoggerFactory.getLogger(ModelDataGenServiceImpl.class);

    @Resource
    private ModelDataGenMapper mapper;
    @Resource
    private IDataService dataService;
    @Resource
    private FaultPhysicalModelMapper faultPhysicalModelMapper;


    @Resource
    private CommonController commonController;
    private static final Pattern FILE_PATH_PATTERN = Pattern.compile(
            // Windows路径（支持斜杠或反斜杠）
            "^([a-zA-Z]:(?:[/\\\\](?:[^/\\\\:*?\"<>|\r\n]+))*[/\\\\]?[^/\\\\:*?\"<>|\r\n]*)$|" +
                    // Unix/Linux路径
                    "^(/(?:[^/]+/)*[^/]+)$"
    );

    @Override
    public List<ModelDataGen> selectList(ModelDataGen po) {
        QueryWrapper<ModelDataGen> wrapper = buildQueryWrapper(po);
        wrapper.orderByDesc("create_time");
        return mapper.selectList(wrapper);
    }

    @Override
    public ModelDataGen selectOne(String id) {
        QueryWrapper<ModelDataGen> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ModelDataGen::getDataGenId, id);
        return mapper.selectOne(wrapper);
    }
    /**
     * 将指定目录下的指定文件拷贝、重命名并移动到目标文件夹
     * @param sourceDirectory 源文件所在的目录
     * @param sourceFileName 源文件的名称
     * @param targetDirectory 目标文件夹
     * @param newFileName 重命名后的新文件名
     * @throws IOException 如果文件操作过程中出现 I/O 错误
     */
    public static String copyRenameAndMoveFile(String sourceDirectory, String sourceFileName,
                                             String targetDirectory, String newFileName) throws IOException {
        // 构建源文件的路径
        Path sourcePath = Paths.get(sourceDirectory, sourceFileName);
        // 构建目标文件的路径
        Path targetPath = Paths.get(targetDirectory, newFileName);

        // 检查源文件是否存在
        if (Files.exists(sourcePath)) {
            // 创建目标文件夹（如果不存在）
            Files.createDirectories(targetPath.getParent());
            // 拷贝文件并使用新的文件名
            Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
            // 可以选择删除源文件以实现移动效果
             Files.delete(sourcePath);

        } else {
            System.out.println("源文件不存在: " + sourcePath);
        }
        return targetPath.toString();
    }


    @Override
    public int add(ModelDataGen po) throws IOException {
        FaultPhysicalModel model = faultPhysicalModelMapper.selectById(po.getModelId());
        ObjectMapper mapper = new ObjectMapper();
        LinkedHashMap[] array = mapper.readValue(po.getPostApiData(), LinkedHashMap[].class);
        for (LinkedHashMap item : array) {
            Map map = processMap(item);
            Map returnMap = HttpClientUtils.sendPostRequest(model.getModelPath(), map);
        }
        return 1;
    }

    private Map processMap(LinkedHashMap<String, Object> map) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            // 检查值是否为字符串且符合文件路径格式
            if (value instanceof String) {
                String filePath = (String) value;
                boolean validFilePath = isValidFilePath(filePath);
                if (validFilePath) {
                    // 转换为File对象并更新map
                    File file = new File(filePath);
                    map.put(key, file);
                    System.out.println("字段 '" + key + "' 已转换为File对象: " + file);
                }
            }
        }
        return map;
    }

    /**
     * 判断字符串是否为有效的文件路径
     */
    private boolean isValidFilePath(String path) {
        if (path != null || !path.trim().isEmpty()) {
            return FILE_PATH_PATTERN.matcher(path).matches();
        }
        return false;
    }

    public static String extractLastPart(String url,String dom) {
        int lastSlashIndex = url.lastIndexOf(dom);
        if (lastSlashIndex != -1 && lastSlashIndex < url.length() - 1) {
            return url.substring(lastSlashIndex + 1);
        }
        return "";
    }
    @Override
    public int update(ModelDataGen po) {
        ModelDataGen one = selectOne(po.getDataGenId());
        setCreateInfo(po);
        return one != null ? mapper.updateById(po) : mapper.insert(po);
    }

    @Override
    public int delete(String[] ids) {
        if (ids == null || ids.length == 0) {
            return 0;
        }
        return ids.length == 1 ? mapper.deleteById(ids[0]) : mapper.deleteBatchIds(Arrays.asList(ids));
    }

    public static void startExe(String exePath) throws IOException, InterruptedException {
        ProcessBuilder processBuilder = new ProcessBuilder(exePath);
        Process process = processBuilder.start();
        int exitCode = process.waitFor();
        logger.info("程序退出码: {}", exitCode);
    }

    public static void writeToTxt(String filePath, String content) throws IOException {
        try (FileWriter writer = new FileWriter(filePath, false)) {
            writer.write("");
            writer.write(content);
            writer.flush();
        }
    }

    public static boolean sendPostRequest(String apiUrl, String jsonBody) throws IOException {
        URL url = new URL(apiUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        // 显式指定UTF-8编码
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        connection.setDoOutput(true);

        byte[] requestBody = jsonBody.getBytes(StandardCharsets.UTF_8);
        connection.setRequestProperty("Content-Length", String.valueOf(requestBody.length));

        try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
            outputStream.write(requestBody);
            outputStream.flush();
        }

        int responseCode = connection.getResponseCode();
        StringBuilder response = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                        responseCode >= 200 && responseCode < 300 ?
                                connection.getInputStream() : connection.getErrorStream(),
                        StandardCharsets.UTF_8
                )
        )) {
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
        }
        logger.info("API 请求响应: {}", response);
        // 判断响应状态码是否在200 - 299之间
        return responseCode >= 200 && responseCode < 300;
    }

    private QueryWrapper<ModelDataGen> buildQueryWrapper(ModelDataGen po) {
        QueryWrapper<ModelDataGen> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(po.getDataGenName()), "data_gen_name", po.getDataGenName())
                .eq(StringUtils.isNotEmpty(po.getModelId()), "model_id", po.getModelId())
                .eq(StringUtils.isNotEmpty(po.getStatus()), "status", po.getStatus())
                .eq(StringUtils.isNotEmpty(po.getDataGenId()), "data_gen_id", po.getDataGenId())
                .eq(StringUtils.isNotEmpty(po.getCreateBy()), "create_by", po.getCreateBy());
        if (po.getStartTime() != null) {
            wrapper.ge("start_time", po.getStartTime());
        }
        if (po.getEndTime() != null) {
            wrapper.le("end_time", po.getEndTime());
        }
        return wrapper;
    }

    private void setUpdateInfo(ModelDataGen po) {
        po.setUpdateBy(SecurityUtils.getUsername());
        po.setUpdateTime(DateUtils.getNowDate());
    }

    private void setCreateInfo(ModelDataGen po) {
        po.setCreateTime(DateUtils.getNowDate());
        po.setCreateBy(SecurityUtils.getUsername());
    }
}