package wang.yaojia.webterm.designsystem

import androidx.compose.runtime.Immutable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

/**
 * # Tokens — the Compose-facing design vocabulary (mirrors iOS `DS`).
 *
 * Thin wrappers that lift [DesignSpec]'s raw numbers into Compose types
 * (`Color`, `Dp`). Screens/components reference these — never an inline hex, gap
 * or radius. Split into small objects that mirror the iOS `DS.*` groups.
 */

/**
 * The two adaptive palettes (dark = default) plus the theme-independent tokens
 * (accent, semantic status, terminal canvas). The scheme-specific surface/text
 * colors are consumed by [WebTermTheme] to build the Material 3 `ColorScheme`.
 */
public object WebTermColors {

    // Accent — theme-adaptive; injected once via the color scheme's `primary`.
    public val accentDark: Color = Color(DesignSpec.ACCENT_DARK)
    public val accentLight: Color = Color(DesignSpec.ACCENT_LIGHT)
    public val onAccent: Color = Color(DesignSpec.ON_ACCENT)
    /** Faint accent wash (selection/soft highlight) — web --accent-soft. */
    public val accentSoft: Color = Color(DesignSpec.ACCENT_DARK).copy(alpha = DesignSpec.OPACITY_ACCENT_SOFT)

    // Semantic status (theme-independent — same hex reads on desktop/iOS/Android).
    public val statusWorking: Color = Color(DesignSpec.STATUS_WORKING)
    public val statusWaiting: Color = Color(DesignSpec.STATUS_WAITING)
    public val statusStuck: Color = Color(DesignSpec.STATUS_STUCK)
    public val statusIdle: Color = Color(DesignSpec.STATUS_IDLE)
    public val statusUnknown: Color = Color(DesignSpec.STATUS_UNKNOWN)

    // Timeline event classes (A28).
    public val timelineTool: Color = Color(DesignSpec.TIMELINE_TOOL)
    public val timelineUser: Color = Color(DesignSpec.TIMELINE_USER)

    // Terminal canvas — FIXED, independent of app theme.
    public val terminalBackground: Color = Color(DesignSpec.TERMINAL_BACKGROUND)
    public val terminalForeground: Color = Color(DesignSpec.TERMINAL_FOREGROUND)
    public val terminalCaret: Color = Color(DesignSpec.TERMINAL_CARET)

    /** One appearance's surface + text colors. Consumed by [WebTermTheme]. */
    @Immutable
    public data class Scheme(
        val background: Color,
        val surface: Color,
        val card: Color,
        val hairline: Color,
        val textPrimary: Color,
        val textSecondary: Color,
        val textTertiary: Color,
        val accent: Color,
    )

    public val dark: Scheme = Scheme(
        background = Color(DesignSpec.DARK_BACKGROUND),
        surface = Color(DesignSpec.DARK_SURFACE),
        card = Color(DesignSpec.DARK_CARD),
        hairline = Color(DesignSpec.DARK_HAIRLINE),
        textPrimary = Color(DesignSpec.DARK_TEXT_PRIMARY),
        textSecondary = Color(DesignSpec.DARK_TEXT_SECONDARY),
        textTertiary = Color(DesignSpec.DARK_TEXT_TERTIARY),
        accent = accentDark,
    )

    public val light: Scheme = Scheme(
        background = Color(DesignSpec.LIGHT_BACKGROUND),
        surface = Color(DesignSpec.LIGHT_SURFACE),
        card = Color(DesignSpec.LIGHT_CARD),
        hairline = Color(DesignSpec.LIGHT_HAIRLINE),
        textPrimary = Color(DesignSpec.LIGHT_TEXT_PRIMARY),
        textSecondary = Color(DesignSpec.LIGHT_TEXT_SECONDARY),
        textTertiary = Color(DesignSpec.LIGHT_TEXT_TERTIARY),
        accent = accentLight,
    )
}

/** Spacing scale — 2·4·8·12·16·20·24 (mirrors `DS.Space`). */
public object Spacing {
    public val xs2: Dp = DesignSpec.SPACE_XS2.dp
    public val xs4: Dp = DesignSpec.SPACE_XS4.dp
    public val sm8: Dp = DesignSpec.SPACE_SM8.dp
    public val md12: Dp = DesignSpec.SPACE_MD12.dp
    public val lg16: Dp = DesignSpec.SPACE_LG16.dp
    public val xl20: Dp = DesignSpec.SPACE_XL20.dp
    public val xxl24: Dp = DesignSpec.SPACE_XXL24.dp
}

/** Corner radii (mirrors `DS.Radius`). */
public object Radius {
    public val sm8: Dp = DesignSpec.RADIUS_SM8.dp
    public val md12: Dp = DesignSpec.RADIUS_MD12.dp
    public val lg16: Dp = DesignSpec.RADIUS_LG16.dp
    public val pill: Dp = DesignSpec.RADIUS_PILL.dp
}

/** Border widths (mirrors `DS.Stroke`). */
public object Stroke {
    public val hairline: Dp = DesignSpec.STROKE_HAIRLINE.dp
}

/** Dimming multipliers (mirrors `DS.Opacity`). */
public object Opacity {
    public const val stale: Float = DesignSpec.OPACITY_STALE
    public const val exited: Float = DesignSpec.OPACITY_EXITED
    public const val pressed: Float = DesignSpec.OPACITY_PRESSED
}

/** Non-spacing layout constants (mirrors `DS.Layout`). */
public object LayoutTokens {
    public val minHitTarget: Dp = DesignSpec.MIN_HIT_TARGET.dp
}
