// Hoofdstijl for SwiftUI. design.nielskorte.nl/hoofdstijl
// Same tokens as hoofdstijl.css (taken from the task board). Light and dark values follow the system appearance.
// Drop this file into an app target. Use: .foregroundStyle(Hoofdstijl.text), .background(Hoofdstijl.surface),
// .tint(Hoofdstijl.accent), RoundedRectangle(cornerRadius: Hoofdstijl.Radius.card).
// Type: the system font (SF Pro) at Apple's text styles; Hoofdstijl does not bundle a typeface.

import SwiftUI

enum Hoofdstijl {
    // Surfaces, back to front
    static let bg = Color(light: 0xF6F6F8, dark: 0x111116)
    static let panel = Color(light: 0xFAFAFB, dark: 0x14141A)
    static let surface = Color(light: 0xFFFFFF, dark: 0x1A1A22)
    static let raised = Color(light: 0xFFFFFF, dark: 0x20202A)

    // Text
    static let text = Color(light: 0x1C1C24, dark: 0xECECF2)
    static let muted = Color(light: 0x6B6B7C, dark: 0x9B9BAE)
    static let faint = Color(light: 0x9C9CAC, dark: 0x6A6A7E)

    // Lines and states
    static let line = Color(light: 0xE8E8EE, dark: 0x26262F)
    static let lineStrong = Color(light: 0xD9D9E3, dark: 0x33333F)
    static let hover = Color(light: 0xEFEFF3, dark: 0x20202A)
    static let press = Color(light: 0xE7E7ED, dark: 0x282833)

    // Accent: graphite, near-black in light and near-white in dark
    static let accent = Color(light: 0x24242E, dark: 0xECECF2)
    static let accentInk = Color(light: 0xFFFFFF, dark: 0x15151B)
    static let accentSoft = Color(light: 0xECECF1, dark: 0x2A2A35)
    static let accentLine = Color(light: 0xCDCDD8, dark: 0x4A4A5C)

    // Meaning
    static let info = Color(light: 0x3D63DD, dark: 0x8AA5FF)
    static let infoSoft = Color(light: 0xEAF0FF, dark: 0x1A2244)
    static let ok = Color(light: 0x23875A, dark: 0x5FCF96)
    static let okSoft = Color(light: 0xE6F5EE, dark: 0x14301F)
    static let danger = Color(light: 0xC93A3A, dark: 0xFF7B72)
    static let dangerSoft = Color(light: 0xFDECEC, dark: 0x3A1B1B)

    // Categorical colours (dots, tags, chart series), same in light and dark
    static let categorical: [Color] = [0x6B5BD6, 0x2F6FDE, 0x1A9C8F, 0x2F9E6B, 0xC9A227, 0xD2544A, 0xC7559B, 0x7A7A8C].map { Color(hex: $0) }

    enum Radius {
        static let small: CGFloat = 6
        static let card: CGFloat = 8
        static let panel: CGFloat = 10
        static let dialog: CGFloat = 12
    }

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

    enum Motion {
        static let quick = Animation.easeOut(duration: 0.12)
        static let pop = Animation.timingCurve(0.2, 0.8, 0.2, 1, duration: 0.16)
        static let slide = Animation.timingCurve(0.2, 0.8, 0.2, 1, duration: 0.22)
    }
}

extension Color {
    init(hex: UInt32) {
        self.init(.sRGB,
                  red: Double((hex >> 16) & 0xFF) / 255,
                  green: Double((hex >> 8) & 0xFF) / 255,
                  blue: Double(hex & 0xFF) / 255)
    }

    init(light: UInt32, dark: UInt32) {
        #if canImport(UIKit)
        self.init(UIColor { traits in
            let hex = traits.userInterfaceStyle == .dark ? dark : light
            return UIColor(red: CGFloat((hex >> 16) & 0xFF) / 255,
                           green: CGFloat((hex >> 8) & 0xFF) / 255,
                           blue: CGFloat(hex & 0xFF) / 255, alpha: 1)
        })
        #elseif canImport(AppKit)
        self.init(NSColor(name: nil) { appearance in
            let isDark = appearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
            let hex = isDark ? dark : light
            return NSColor(srgbRed: CGFloat((hex >> 16) & 0xFF) / 255,
                           green: CGFloat((hex >> 8) & 0xFF) / 255,
                           blue: CGFloat(hex & 0xFF) / 255, alpha: 1)
        })
        #else
        self.init(hex: light)
        #endif
    }
}
