package com.nokia.tsl_data.util;

import org.springframework.util.StringUtils;

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

/**
 * 文本工具
 */
public class TextUtil {

    // 判断是否为空字符串
    public static boolean isBlank(String s) {
        return !StringUtils.hasText(s);
    }

    // 使用utf8解码并读取文件
    public static List<String> readLines(String path) {
        return readLines(path, "utf-8");
    }

    // 使用utf8解码并读取文件
    public static List<String> readLinesWithUTF8(String path) {
        return readLines(path, "utf-8");
    }

    // 使用gbk读取文件
    public static List<String> readLinesWithGBK(String path) {
        return readLines(path, "gbk");
    }

    // 按行读取文件
    public static List<String> readLines(String path, String charsetName) {
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(path), charsetName))) {
            return readLines(reader);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    // 按行读取文件
    private static List<String> readLines(BufferedReader reader) {
        List<String> result = new ArrayList<>();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                result.add(line);
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    public static String readText(String path) {
        return readText(path, "utf-8");
    }

    public static String readTextWithUTF8(String path) {
        return readText(path, "utf-8");
    }

    public static String readTextWithGBK(String path) {
        return readText(path, "gbk");
    }

    // 读取完整文件
    public static String readText(String path, String charsetName) {
        try {
            return new String(Files.readAllBytes(Paths.get(path)), charsetName);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("读取文本文件出错: " + e.getMessage());
        }
    }

    public static void writeToFile(String text, String path) {
        writeToFile(text, path, "utf-8");
    }

    public static void writeToFileWithUTF8(String text, String path) {
        writeToFile(text, path, "utf-8");
    }

    public static void writeToFileWithGBK(String text, String path) {
        writeToFile(text, path, "gbk");
    }

    public static void writeToFile(String text, String path, String charsetName) {
        writeToFile(text, new File(path), charsetName);
    }

    // 写入文件本舰
    public static void writeToFile(String text, File file, String charsetName) {
        try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), charsetName)) {
            writer.write(text);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("写入文本文件出错：" + e.getMessage());
        }
    }
}
