import SwiftUI
import AppKit

// Proat designsysteem. Eén bron voor kleur, typografie, ruimte, elevatie.
// Teal verschijnt alleen op live/actief/merk-momenten; de rest is neutraal.

// MARK: - Kleur

extension Color {
    /// Dynamisch (light/dark) via NSColor-provider.
    init(proatLight light: String, dark: String) {
        let ns = NSColor(name: nil) { appearance in
            let isDark = appearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
            return NSColor(hex: isDark ? dark : light)
        }
        self.init(nsColor: ns)
    }

    init(hexString: String) {
        self.init(nsColor: NSColor(hex: hexString))
    }

    // Merk
    static let proatTeal = Color(hexString: "4FB8B0")
    static let proatTealTint = Color(proatLight: "4FB8B0", dark: "6BC9C0").opacity(0.16)

    // Neutrale schaal (light/dark)
    static let proatBgBase   = Color(proatLight: "FBFBFA", dark: "0E1112")
    static let proatBgRaised = Color(proatLight: "FFFFFF", dark: "181B1D")
    static let proatBgSunken = Color(proatLight: "F2F2F0", dark: "0A0C0D")
    static let proatBorder   = Color(proatLight: "E5E5E2", dark: "262A2C")
    static let proatText1    = Color(proatLight: "1A1D1E", dark: "F4F5F5")
    static let proatText2    = Color(proatLight: "5C6360", dark: "9AA0A0")
    static let proatText3    = Color(proatLight: "9AA0A0", dark: "5C6360")

    // Semantisch
    static let proatOK      = Color(hexString: "10B981")
    static let proatWarn    = Color(hexString: "F59E0B")
    static let proatError   = Color(hexString: "EF4444")
}

extension LinearGradient {
    /// Merk-gradient: alleen op live/actief/merk-momenten.
    static let proatTeal = LinearGradient(
        colors: [Color(hexString: "5BC4BB"), Color(hexString: "4FB8B0"), Color(hexString: "1A5C5E")],
        startPoint: .topLeading,
        endPoint: .bottomTrailing
    )
}

extension NSColor {
    convenience init(hex: String) {
        var s = hex.trimmingCharacters(in: .whitespacesAndNewlines)
        if s.hasPrefix("#") { s.removeFirst() }
        var rgb: UInt64 = 0
        Scanner(string: s).scanHexInt64(&rgb)
        self.init(
            srgbRed: CGFloat((rgb & 0xFF0000) >> 16) / 255,
            green: CGFloat((rgb & 0x00FF00) >> 8) / 255,
            blue: CGFloat(rgb & 0x0000FF) / 255,
            alpha: 1
        )
    }
}

// MARK: - Typografie

enum ProatFont {
    static let display = Font.system(size: 34, weight: .bold)
    static let titel   = Font.system(size: 22, weight: .semibold)
    static let sectie  = Font.system(size: 16, weight: .semibold)
    static let body    = Font.system(size: 14, weight: .regular)
    static let klein   = Font.system(size: 12, weight: .regular)
    static let mono    = Font.system(size: 13, weight: .regular, design: .monospaced)
}

// MARK: - Ruimte, radii

enum ProatSpacing {
    static let xs: CGFloat = 8
    static let s: CGFloat = 12
    static let m: CGFloat = 16
    static let l: CGFloat = 24
    static let xl: CGFloat = 32
    static let xxl: CGFloat = 48
}

enum ProatRadius {
    static let knop: CGFloat = 8
    static let kaart: CGFloat = 12
    static let sheet: CGFloat = 16
    static let pill: CGFloat = 20
}

// MARK: - Elevatie + glas

extension View {
    func proatShadowRust() -> some View {
        shadow(color: .black.opacity(0.06), radius: 1, x: 0, y: 1)
    }
    func proatShadowKaart() -> some View {
        shadow(color: .black.opacity(0.08), radius: 8, x: 0, y: 4)
    }
    func proatShadowFloat() -> some View {
        shadow(color: .black.opacity(0.16), radius: 20, x: 0, y: 12)
    }

    /// Solide kaart voor het hoofdvenster (geen glas — rust, geen flikker).
    func proatCard(padding: CGFloat = ProatSpacing.m) -> some View {
        self
            .padding(padding)
            .background(Color.proatBgRaised)
            .clipShape(RoundedRectangle(cornerRadius: ProatRadius.kaart, style: .continuous))
            .overlay(
                RoundedRectangle(cornerRadius: ProatRadius.kaart, style: .continuous)
                    .stroke(Color.proatBorder, lineWidth: 1)
            )
            .proatShadowKaart()
    }
}

// MARK: - Merk-mark

/// Subtiele noaber.ai-merkpresentie: het AppIcon-glyph met optioneel "noaber.ai"-bijschrift.
/// Bedoeld voor lege/placeholder-toestanden en hero-momenten — ingetogen, teal-discipline.
struct ProatBrandMark: View {
    /// Grootte van het icoon in punten (standaard 36).
    var size: CGFloat = 36
    /// Toon "noaber.ai"-bijschrift onder het icoon.
    var showCaption: Bool = true
    /// Doorzichtigheid van het geheel (lege staten mogen subtiel zijn).
    var opacity: Double = 0.85

    var body: some View {
        VStack(spacing: ProatSpacing.xs) {
            brandImage
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: size, height: size)
                .clipShape(RoundedRectangle(cornerRadius: size * 0.22, style: .continuous))

            if showCaption {
                Text("noaber.ai")
                    .font(ProatFont.klein)
                    .foregroundColor(.proatText3)
            }
        }
        .opacity(opacity)
        .accessibilityElement(children: .ignore)
        .accessibilityLabel(Text("noaber.ai"))
    }

    private var brandImage: Image {
        if let ns = NSImage(named: "AppIcon") {
            return Image(nsImage: ns)
        }
        return Image(systemName: "waveform.circle.fill")
    }
}

/// Glas-laag — ALLEEN mini-recorder, opdrachtenbalk, menubalk.
struct ProatGlassBackground: NSViewRepresentable {
    func makeNSView(context: Context) -> NSVisualEffectView {
        let v = NSVisualEffectView()
        v.material = .hudWindow
        v.blendingMode = .behindWindow
        v.state = .active
        return v
    }
    func updateNSView(_ v: NSVisualEffectView, context: Context) {}
}
