package com.youlai.boot.common.util;

import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
@Slf4j
public class SystemUtil {


    public static boolean isLinuxServer(){
        String os = System.getProperty("os.name").toLowerCase();
        return os.contains("nix") || os.contains("nux") || os.contains("mac");
    }

    // 检查字符串是否包含中文字符
    public static boolean containsChinese(String input) {
        if (input == null || input.isEmpty()) {
            return false;
        }

        for (char c : input.toCharArray()) {
            // 检查字符是否在中文字符范围内
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }


    public static String renameFile(String originalFileName) {
        String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String[] split = originalFileName.split("\\.");
        StringBuilder preName = new StringBuilder();
        for (int i = 0; i < split.length-1; i++) {
            preName.append(split[i]);
        }
        preName.append(timestamp);

        return preName+"."+ split[split.length-1];

    }


   public static void downloadFile(String path, HttpServletResponse response) {
        File file = new File(path);
        response.reset();
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setContentLength((int) file.length());

        try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));) {
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), StandardCharsets.UTF_8) );

            byte[] buff = new byte[1024];
            OutputStream os  = response.getOutputStream();
            int i = 0;
            while ((i = bis.read(buff)) != -1) {
                os.write(buff, 0, i);
                os.flush();
            }
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }



    // 检查单个字符是否为中文字符
    private static boolean isChinese(char c) {
        // Unicode 编码范围
        // 基本区：\u4E00-\u9FFF
        // 扩展A区：\u3400-\u4DBF
        // 扩展B区：\u20000-\u2A6DF
        // 扩展C区：\u2A700-\u2B73F
        // 扩展D区：\u2B740-\u2B81F
        // 扩展E区：\u2B820-\u2CEAF
        // 扩展F区：\uF900-\uFAFF
        // 兼容汉字：\u3300-\u33FF
        // 兼容扩展：\uFE30-\uFE4F
        // 兼容扩展补充：\u2F800-\u2FA1F

        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || (c >= 0xF900 && c <= 0xFAFF) // 兼容汉字补充
                || (c >= 0x2F800 && c <= 0x2FA1F)); // 兼容扩展补充
    }
}
