package tech.easyflow.common.util;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

public class FileUtil {

    public static String calcByte(Long sizeInBytes) {
        if (sizeInBytes == null) {
            return "";
        }

        String sizeFormatted;
        if (sizeInBytes >= 1024 * 1024 * 1024) {
            // Convert to GB
            double sizeInGB = sizeInBytes / (1024.0 * 1024.0 * 1024.0);
            sizeFormatted = String.format("%.2f GB", sizeInGB);
        } else if (sizeInBytes >= 1024 * 1024) {
            // Convert to MB
            double sizeInMB = sizeInBytes / (1024.0 * 1024.0);
            sizeFormatted = String.format("%.2f MB", sizeInMB);
        } else if (sizeInBytes >= 1024) {
            // Convert to KB
            double sizeInKB = sizeInBytes / 1024.0;
            sizeFormatted = String.format("%.2f KB", sizeInKB);
        } else {
            // Keep in bytes
            sizeFormatted = sizeInBytes + " bytes";
        }
        return sizeFormatted;
    }


    /**
     * 按文件名提取平台支持的文档扩展名。
     *
     * @param fileName 文件名或文件路径
     * @return 小写扩展名；无法识别时返回 {@code null}
     */
    public static String getFileTypeByExtension(String fileName) {
        if (fileName == null) {
            return null;
        }
        String normalizedFileName = fileName.toLowerCase(Locale.ROOT);
        if (normalizedFileName.endsWith(".txt")) {
            return "txt";
        } else if (normalizedFileName.endsWith(".pdf")) {
            return "pdf";
        } else if (normalizedFileName.endsWith(".md")) {
            return "md";
        } else if (normalizedFileName.endsWith(".docx")) {
            return "docx";
        } else if (normalizedFileName.endsWith(".xlsx")) {
            return "xlsx";
        } else if (normalizedFileName.endsWith(".csv")) {
            return "csv";
        } else if (normalizedFileName.endsWith(".ppt")) {
            return "ppt";
        } else if (normalizedFileName.endsWith(".pptx")) {
            return "pptx";
        } else {
            return null;
        }
    }

    /**
     * url解编码
     * @param url
     * @return
     */
    public static String getDecodedUrl(String url) {
        String encodedUrl = URLEncoder.encode(url, StandardCharsets.UTF_8)
                .replace("+", "%20") // 空格转 %20
                .replace("%2F", "/"); // 保留路径分隔符 /

        try {
            URI validUri = new URI(encodedUrl);
            return validUri.toString();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
}
