import SwiftUI

// MARK: - 字体库枚举
/// 定义应用中可用的字体库
enum FontFamily: String, CaseIterable {
    case sankeiCute = "SankeiCutePopanime"  // 可爱风格字体
    case quicksand = "Quicksand x"      // 主题字体
    case quicksandBold = "Quicksand-Bold"
    case quicksandRegular = "Quicksand-Regular"
    // 后续添加新字体库时在这里添加新 case
    // 例如: case anotherFont = "AnotherFontName"
    
    /// 获取字体名称
    var name: String {
        return self.rawValue
    }
}

// MARK: - 文本样式枚举
/// 定义应用中使用的文本样式类型
enum TypographyStyle {
    case headline    // 大标题
    case title       // 标题
    case body        // 正文
    case subtitle    // 副标题
    case caption     // 说明文字
    case footnote    // 脚注
    case small       // 小号文字
}

// MARK: - 字体配置结构体
private struct TypographyConfig {
    let size: CGFloat
    let weight: UIFont.Weight
    let textStyle: UIFont.TextStyle
}

// MARK: - Typography 管理类
struct Typography {
    // MARK: - 配置
    
    /// 默认字体库
    private static let defaultFontFamily: FontFamily = .quicksand
    
    /// 文本样式配置表
    private static let styleConfig: [TypographyStyle: TypographyConfig] = [
    .headline: TypographyConfig(size: 24, weight: .bold, textStyle: .headline),
    .title: TypographyConfig(size: 20, weight: .semibold, textStyle: .title2),
    .body: TypographyConfig(size: 16, weight: .regular, textStyle: .body),
    .subtitle: TypographyConfig(size: 14, weight: .medium, textStyle: .subheadline),
    .caption: TypographyConfig(size: 12, weight: .light, textStyle: .caption1),
    .footnote: TypographyConfig(size: 11, weight: .regular, textStyle: .footnote),
    .small: TypographyConfig(size: 10, weight: .regular, textStyle: .headline)
]
    
    // MARK: - 公共方法
    
    /// 获取指定样式和字体库的字体
    /// - Parameters:
    ///   - style: 文本样式
    ///   - family: 字体库，默认为 nil 使用默认字体库
    /// - Returns: 配置好的 Font 对象
    static func font(for style: TypographyStyle, family: FontFamily? = nil, size: CGFloat? = nil) -> Font {
        let fontFamily = family ?? defaultFontFamily
        guard let config = styleConfig[style] else {
            return .body
        }
        
        // 尝试加载自定义字体
        if let customFont = UIFont(name: fontFamily.name, size: size ?? config.size) {
            let metrics = UIFontMetrics(forTextStyle: config.textStyle)
            let scaledFont = metrics.scaledFont(for: customFont)
            return Font(scaledFont)
        }
        
        // 如果自定义字体加载失败，回退到系统字体
        let systemFont = UIFont.systemFont(ofSize: size ?? config.size, weight: config.weight)
        let metrics = UIFontMetrics(forTextStyle: config.textStyle)
        let scaledFont = metrics.scaledFont(for: systemFont)
        return Font(scaledFont)
    }
    
    /// 获取所有可用的字体库
    /// - Returns: 可用的字体库数组
    static func availableFonts() -> [FontFamily] {
        return FontFamily.allCases
    }
    
    /// 检查字体是否可用
    /// - Parameter family: 要检查的字体库
    /// - Returns: 是否可用
    static func isFontAvailable(_ family: FontFamily) -> Bool {
        return UIFont(name: family.name, size: 16) != nil
    }
}
