package com.ruoyi.platform.utils;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.Map;


//import org.ho.yaml.Yaml;

@Component
public class YamlUtils {

    /**
     * 将Map对象转换为YAML格式并写入指定路径的文件中
     *
     * @param data     Map对象
     * @param path     文件路径
     * @param fileName 文件名
     */
    public static void generateYamlFile(Map<String, Object> data, String path, String fileName) {
        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);


        // 创建Yaml实例
        Yaml yaml = new Yaml(options);

        File directory = new File(path);
        if (!directory.exists()) {
            boolean isCreated = directory.mkdirs();
            if (!isCreated) {
                throw new RuntimeException("创建路径失败: " + path);
            }
        }

        String fullPath = path + "/" + fileName + ".yaml";

        try (FileWriter writer = new FileWriter(fullPath)) {
            String dump = yaml.dump(data);

            yaml.dump(data, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


//    public static void generateYamlFile1(Object data, String path, String fileName) {
//        try {
//            YAMLFactory yamlFactory = new YAMLFactory();
//            yamlFactory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
//            yamlFactory.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);
////
//            ObjectMapper objectMapper = new ObjectMapper(yamlFactory);
////            ObjectMapper objectMapper = new ObjectMapper();
//            objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
//            String s = objectMapper.writeValueAsString(data);
//
//            File directory = new File(path);
//            if (!directory.exists()) {
//                boolean isCreated = directory.mkdirs();
//                if (!isCreated) {
//                    throw new RuntimeException("创建路径失败: " + path);
//                }
//            }
//
//            try {
//                DumperOptions options = new DumperOptions();
//                options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
//                // 创建Yaml实例
//                Yaml yaml = new Yaml(options);
//
//                String fullPath = path + "/" + fileName + ".yaml";
//                FileWriter writer = new FileWriter(fullPath);
//
//                yaml.dump(s, writer);
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//
//
//        } catch (JsonProcessingException e) {
//            throw new RuntimeException(e);
//        }
//    }


    /**
     * 读取YAML文件并将其内容转换为Map<String, Object>
     *
     * @param filePath YAML文件路径
     * @return 包含YAML内容的Map<String, Object>
     */
    public static Map<String, Object> loadYamlFile(String filePath) {
        Yaml yaml = new Yaml();
        try (InputStream inputStream = new FileInputStream(new File(filePath))) {
            return yaml.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean isNumeric(String str) {
        try {
            double num = Double.parseDouble(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
