//
//  DesignTokens.swift
//  AIStudyApp
//
//  知习 AI Design System v2.0 — Token 定义
//  主色 #3D7FFB (知习蓝), 渐变 #3D7FFB → #9DA7FD
//

import SwiftUI

// MARK: - Hex Color Extension

extension Color {
    init(hex: String, opacity: Double = 1.0) {
        let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int: UInt64 = 0
        Scanner(string: hex).scanHexInt64(&int)
        let r, g, b: UInt64
        switch hex.count {
        case 6:
            (r, g, b) = ((int >> 16) & 0xFF, (int >> 8) & 0xFF, int & 0xFF)
        case 8:
            (r, g, b) = ((int >> 24) & 0xFF, (int >> 16) & 0xFF, (int >> 8) & 0xFF)
        default:
            (r, g, b) = (0, 0, 0)
        }
        self.init(.sRGB, red: Double(r) / 255, green: Double(g) / 255, blue: Double(b) / 255, opacity: opacity)
    }
}

extension Color {
    init(light: Color, dark: Color) {
        #if canImport(UIKit)
        self.init(UIColor { traitCollection in
            traitCollection.userInterfaceStyle == .dark ? UIColor(dark) : UIColor(light)
        })
        #else
        self.init(light)
        #endif
    }
}

// MARK: - ZhiXi Design Tokens v2.0

extension Color {
    // ── 品牌主色 ──
    static let zxPrimary        = Color(hex: "#3D7FFB")
    static let zxPrimaryPressed = Color(hex: "#2B65D1")
    static let zxPrimaryDeep    = Color(hex: "#1E4FBB")
    static let zxPrimarySoft    = Color(hex: "#EBF0FE")
    static let zxOnPrimary      = Color.white

    // ── 渐变色 ──
    static let zxGradientStart  = Color(hex: "#3D7FFB")
    static let zxGradientEnd    = Color(hex: "#9DA7FD")

    // ── 页面背景 (暖白体系) ──
    static let zxCanvas         = Color(light: Color(hex: "#FAFAF8"), dark: Color(hex: "#1C1C1E"))
    static let zxSurface        = Color(light: Color(hex: "#F3F2F0"), dark: Color(hex: "#2C2C2E"))
    static let zxSurfaceSoft    = Color(light: Color(hex: "#FAFAF9"), dark: Color(hex: "#242426"))
    static let zxSurfaceElevated = Color(light: .white, dark: Color(hex: "#3A3A3C"))

    // ── 分割线 ──
    static let zxHairline       = Color(light: Color(hex: "#E8E6E1"), dark: Color(hex: "#38383A"))
    static let zxHairlineStrong = Color(light: Color(hex: "#D4D1CB"), dark: Color(hex: "#48484A"))

    // ── 文字 ──
    static let zxInkPrimary     = Color(light: Color(hex: "#1E1B18"), dark: Color(hex: "#FFFFFF"))
    static let zxInkSecondary   = Color(light: Color(hex: "#6B6560"), dark: Color(hex: "#A1A1A6"))
    static let zxInkTertiary    = Color(light: Color(hex: "#9F9993"), dark: Color(hex: "#636366"))

    // ── 功能色 ──
    static let zxAmber          = Color(hex: "#F59E0B")
    static let zxAmberSoft      = Color(hex: "#FEF3C7")
    static let zxAmberDeep      = Color(hex: "#92400E")
    static let zxCoral          = Color(hex: "#F87171")
    static let zxCoralSoft      = Color(hex: "#FEE2E2")
    static let zxCoralDeep      = Color(hex: "#991B1B")
    static let zxMint           = Color(hex: "#34D399")
    static let zxMintSoft       = Color(hex: "#D1FAE5")
    static let zxMintDeep       = Color(hex: "#065F46")
    static let zxSky            = Color(hex: "#60A5FA")
    static let zxSkySoft        = Color(hex: "#DBEAFE")

    // ── 兼容旧版 (逐步迁移) ──
    static let zxBg0            = zxCanvas
    static let zxBg1            = zxSurface
    static let zxF0             = zxInkPrimary
    static let zxF05            = zxInkSecondary
    static let zxF04            = zxInkSecondary
    static let zxF03            = zxInkTertiary
    static let zxF007           = zxInkSecondary
    static let zxF006           = zxInkTertiary
    static let zxF0045          = zxInkTertiary
    static let zxF02            = zxInkTertiary
    static let zxF035           = zxInkTertiary
    static let zxPurple         = zxPrimary
    static let zxAccent         = Color(hex: "#9DA7FD")
    static let zxOrange         = zxAmber
    static let zxTeal           = Color(hex: "#2DD4BF")
    static let zxCyan           = Color(hex: "#4ECDC4")
    static let zxGreen          = zxMint
    static let zxYellow         = zxAmber
    static let zxRed            = zxCoral
    static let zxBorder008      = zxHairline
    static let zxBorder006      = zxHairline
    static let zxBorder004      = zxHairline
    static let zxBorder01       = zxHairlineStrong
    static let zxBorder015      = zxHairlineStrong
    static let zxFill003        = zxSurface
    static let zxFill004        = zxSurface
    static let zxFill005        = zxSurface
    static let zxFill006        = zxSurface
    static let zxFill008        = zxSurface
    static let zxFill01         = zxSurfaceSoft

    // ── 旧版彩色半透兼容 ──
    static func zxPurpleBG(_ a: Double = 0.10) -> Color { zxPrimarySoft.opacity(a * 1.5) }
    static func zxYellowBG(_ a: Double = 0.10) -> Color { zxAmberSoft.opacity(a * 2.0) }
    static func zxGreenBG(_ a: Double = 0.10) -> Color { zxMintSoft.opacity(a * 1.5) }
    static func zxRedBG(_ a: Double = 0.15) -> Color { zxCoralSoft.opacity(a * 1.5) }
    static func zxTealBG(_ a: Double = 0.10) -> Color { Color(hex: "#2DD4BF").opacity(a) }
    static func zxOrangeBG(_ a: Double = 0.10) -> Color { zxAmberSoft.opacity(a * 2.0) }
}

// MARK: - ZhiXi Gradients v2.0

enum ZXGradient {
    static let brand = LinearGradient(
        colors: [Color.zxGradientStart, Color.zxGradientEnd],
        startPoint: .topLeading, endPoint: .bottomTrailing
    )

    static let brandHorizontal = LinearGradient(
        colors: [Color.zxGradientStart, Color.zxGradientEnd],
        startPoint: .leading, endPoint: .trailing
    )

    static let ctaButton = LinearGradient(
        colors: [Color.zxGradientStart, Color.zxGradientEnd],
        startPoint: .topLeading, endPoint: .bottomTrailing
    )

    static let page = LinearGradient(
        colors: [Color.zxCanvas, Color.zxSurface],
        startPoint: .top, endPoint: .bottom
    )

    static let heroGlow = RadialGradient(
        colors: [Color.zxPrimarySoft, Color.clear],
        center: .top, startRadius: 0, endRadius: 300
    )

    static let thinkingCard = LinearGradient(
        colors: [Color.zxPrimarySoft, Color.zxSurface],
        startPoint: .topLeading, endPoint: .bottomTrailing
    )

    static let progressBar = LinearGradient(
        colors: [Color.zxPrimary, Color.zxGradientEnd],
        startPoint: .leading, endPoint: .trailing
    )

    // 兼容旧版
    static let brandPurple = brandHorizontal
    static let feedbackScore = thinkingCard
    static let profileCard = thinkingCard
    static let progressCard = thinkingCard
    static let splash = page
    static let ctaPurple = brandHorizontal
}

// MARK: - ZhiXi Radii

enum ZXRadius {
    static let xs: CGFloat     = 6
    static let sm: CGFloat     = 8
    static let md: CGFloat     = 10
    static let lg: CGFloat     = 14
    static let xl: CGFloat     = 20

    // 兼容旧版
    static let xl2: CGFloat    = 16
    static let xl3: CGFloat    = 20
    static let button: CGFloat = 10
    static let buttonLg: CGFloat = 14
    static let icon: CGFloat   = 8
    static let iconLg: CGFloat = 10
    static let avatar: CGFloat = 14
}

// MARK: - ZhiXi Spacing

enum ZXSpacing {
    static let xs: CGFloat     = 4
    static let sm: CGFloat     = 8
    static let md: CGFloat     = 12
    static let lg: CGFloat     = 16
    static let xl: CGFloat     = 20
    static let xxl: CGFloat    = 24
    static let xxxl: CGFloat   = 32
    static let section: CGFloat = 40

    static let pageHPadding: CGFloat = 20
    static let cardPadding: CGFloat = 16
    static let statusBarH: CGFloat = 44
    static let tabBarH: CGFloat = 83
    static let homeIndicatorH: CGFloat = 34

    // 兼容旧版
    static let ss: CGFloat = 2
    static let xl2: CGFloat = 14
    static let xl3: CGFloat = 16
    static let xl4: CGFloat = 20
    static let xl5: CGFloat = 24
    static let xl6: CGFloat = 28
}

// MARK: - ZhiXi Sizing

enum ZXSize {
    static let iconBtn: CGFloat     = 36
    static let iconSm: CGFloat      = 14
    static let iconMd: CGFloat      = 16
    static let iconLg: CGFloat      = 18
    static let tabIcon: CGFloat     = 22
    static let listIcon: CGFloat    = 40
    static let libraryIcon: CGFloat = 44
    static let quickActionH: CGFloat = 72
    static let inputH: CGFloat      = 50
    static let buttonH: CGFloat     = 50
    static let buttonLgH: CGFloat   = 52
    static let buttonXlH: CGFloat   = 56
    static let progressH: CGFloat   = 6
    static let badgeSize: CGFloat   = 28
    static let avatarSm: CGFloat    = 36
    static let avatarMd: CGFloat    = 64
    static let avatarLg: CGFloat    = 80
    static let sendBtn: CGFloat     = 30
    static let scoreBox: CGFloat    = 36
    static let weakBox: CGFloat     = 40
    static let topBar: CGFloat      = 3
    static let searchIconBtn: CGFloat = 36
}

// MARK: - ZhiXi Typography

enum ZXFont {
    static let hero = (size: CGFloat(34), weight: Font.Weight.bold, spacing: CGFloat(-0.5))
    static let title1 = (size: CGFloat(28), weight: Font.Weight.bold, spacing: CGFloat(-0.3))
    static let title2 = (size: CGFloat(22), weight: Font.Weight.semibold, spacing: CGFloat(-0.2))
    static let title3 = (size: CGFloat(20), weight: Font.Weight.semibold, spacing: CGFloat(-0.1))
    static let headline = (size: CGFloat(17), weight: Font.Weight.semibold, spacing: CGFloat(0))
    static let body = (size: CGFloat(17), weight: Font.Weight.regular, lineHeight: CGFloat(1.47))
    static let bodySmall = (size: CGFloat(15), weight: Font.Weight.regular, lineHeight: CGFloat(1.47))
    static let callout = (size: CGFloat(14), weight: Font.Weight.medium, spacing: CGFloat(0))
    static let caption = (size: CGFloat(13), weight: Font.Weight.regular)
    static let caption2 = (size: CGFloat(11), weight: Font.Weight.medium, spacing: CGFloat(0.5))
    static let score = (size: CGFloat(12), weight: Font.Weight.heavy)
    static let scoreLg = (size: CGFloat(22), weight: Font.Weight.heavy, lineHeight: CGFloat(1))

    // 兼容旧版
    static let titleLarge = (size: CGFloat(22), weight: Font.Weight.bold, spacing: CGFloat(-0.5))
    static let titleMedium = (size: CGFloat(20), weight: Font.Weight.bold, spacing: CGFloat(-0.4))
    static let sectionTitle = (size: CGFloat(15), weight: Font.Weight.bold)
    static let subsectionTitle = (size: CGFloat(14), weight: Font.Weight.bold)
    static let date = (size: CGFloat(12), weight: Font.Weight.medium)
    static let description = (size: CGFloat(12), weight: Font.Weight.regular, spacing: CGFloat(0.4))
    static let labelXs = (size: CGFloat(9), weight: Font.Weight.regular)
}

// MARK: - Dynamic Type Scaled Font

struct ZXScaledFont: ViewModifier {
    @ScaledMetric var size: CGFloat
    let weight: Font.Weight

    init(size: CGFloat, weight: Font.Weight = .regular) {
        _size = ScaledMetric(wrappedValue: size, relativeTo: .body)
        self.weight = weight
    }

    func body(content: Content) -> some View {
        content.font(.system(size: size, weight: weight))
    }
}

extension View {
    func zxFontScaled(size: CGFloat, weight: Font.Weight = .regular) -> some View {
        modifier(ZXScaledFont(size: size, weight: weight))
    }
}
