package com.ruoyi.web.controller.common;

import java.awt.*;
import java.io.File;
import java.io.InputStream;

public class FontLoader {
    
    /**
     * 从资源文件加载字体
     */
    public static Font loadFontFromResource(String fontPath, int style, float size) {
        try {
            // 从 classpath 加载字体文件
            InputStream is = FontLoader.class.getClassLoader().getResourceAsStream(fontPath);
            if (is == null) {
                // 如果资源中找不到，尝试从文件系统加载
                return loadFontFromFile(fontPath, style, size);
            }
            
            Font font = Font.createFont(Font.TRUETYPE_FONT, is);
            return font.deriveFont(style, size);
        } catch (Exception e) {
            System.err.println("加载字体失败: " + e.getMessage());
            // 返回默认字体
            return new Font("SansSerif", style, (int)size);
        }
    }
    
    /**
     * 从文件系统加载字体
     */
    public static Font loadFontFromFile(String fontPath, int style, float size) {
        try {
            File fontFile = new File(fontPath);
            Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
            // 注册到 GraphicsEnvironment
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(font);
            return font.deriveFont(style, size);
        } catch (Exception e) {
            System.err.println("加载字体文件失败: " + e.getMessage());
            return new Font("SansSerif", style, (int)size);
        }
    }
    
    /**
     * 获取可用的中文字体
     */
    public static String[] getAvailableChineseFonts() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontNames = ge.getAvailableFontFamilyNames();
        
        // 中文字体常见名称
        String[] chineseFontKeywords = {
            "宋体", "SimSun", "黑体", "SimHei", "微软雅黑", "Microsoft YaHei",
            "楷体", "KaiTi", "仿宋", "FangSong", "苹方", "PingFang",
            "文泉驿", "WenQuanYi", "Noto Sans CJK", "Source Han Sans"
        };
        
        java.util.List<String> chineseFonts = new java.util.ArrayList<>();
        for (String fontName : fontNames) {
            for (String keyword : chineseFontKeywords) {
                if (fontName.contains(keyword)) {
                    chineseFonts.add(fontName);
                    break;
                }
            }
        }
        
        return chineseFonts.toArray(new String[0]);
    }
    
    /**
     * 获取最佳可用字体
     */
    public static String getBestAvailableFont() {
        String[] chineseFonts = getAvailableChineseFonts();
        if (chineseFonts.length > 0) {
            return chineseFonts[0];
        }
        
        // 检查系统字体
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] allFonts = ge.getAvailableFontFamilyNames();
        
        // 尝试查找支持中文的字体
        for (String font : allFonts) {
            if (font.toLowerCase().contains("sans") || 
                font.toLowerCase().contains("serif") ||
                font.toLowerCase().contains("mono")) {
                return font;
            }
        }
        
        return "SansSerif"; // 默认字体
    }
}