package com.tt.honeyDue.widget

import androidx.compose.ui.graphics.Color
import androidx.glance.unit.ColorProvider

/**
 * Static color palette used by Glance widgets.
 *
 * iOS renders widgets with the in-app `Color.appPrimary`, `.appAccent`,
 * `.appError`, etc. Those are dynamic per-theme on iOS and also
 * dark-mode-aware; however, the **widget** process (both on iOS
 * WidgetKit and Android Glance) uses a single design palette hard-coded
 * to the "Teal" theme — matching the brand. Keep this module in sync
 * with `iosApp/HoneyDue/HoneyDue.swift`'s `priorityColor` logic and
 * `Color.appPrimary` / `Color.appAccent` / `Color.appError`.
 *
 * Priority "level" mapping mirrors the backend seed in
 * `MyCribAPI_GO/internal/testutil/testutil.go`:
 *  - 1 = Low     → PRIMARY
 *  - 2 = Medium  → YELLOW_MEDIUM
 *  - 3 = High    → ACCENT
 *  - 4 = Urgent  → ERROR
 *
 * iOS uses the task's *name* string ("urgent"/"high"/"medium") to pick
 * the color; we don't carry the name down in `WidgetTaskDto` so we key
 * off the numeric level (which matches 1:1 with the seed IDs).
 */
object WidgetColors {

    // -- Base palette (Teal theme, light-mode values from ThemeColors.kt) --

    /** iOS `Color.appPrimary` (Teal theme light). */
    val PRIMARY: Color = Color(0xFF07A0C3)

    /** iOS `Color.appSecondary` (Teal theme light). */
    val SECONDARY: Color = Color(0xFF0055A5)

    /** iOS `Color.appAccent` (BrightAmber). */
    val ACCENT: Color = Color(0xFFF5A623)

    /** iOS `Color.appError` (PrimaryScarlet). */
    val ERROR: Color = Color(0xFFDD1C1A)

    /** iOS inline literal "medium" yellow: `Color(red: 0.92, green: 0.70, blue: 0.03)`. */
    val YELLOW_MEDIUM: Color = Color(0xFFEBB308)

    /** iOS `Color.appBackgroundPrimary` (cream). */
    val BACKGROUND_PRIMARY: Color = Color(0xFFFFF1D0)

    /** iOS `Color.appBackgroundSecondary`. */
    val BACKGROUND_SECONDARY: Color = Color(0xFFFFFFFF)

    /** iOS `Color.appTextPrimary`. */
    val TEXT_PRIMARY: Color = Color(0xFF111111)

    /** iOS `Color.appTextSecondary`. */
    val TEXT_SECONDARY: Color = Color(0xFF666666)

    /** iOS `Color.appTextOnPrimary`. */
    val TEXT_ON_PRIMARY: Color = Color(0xFFFFFFFF)

    // -- Mapping helpers --

    /**
     * Pick a priority indicator color for a given priority level.
     * Unknown levels fall through to [PRIMARY] to match iOS default.
     */
    fun colorForPriority(priorityLevel: Int): Color = when (priorityLevel) {
        4 -> ERROR         // Urgent
        3 -> ACCENT        // High
        2 -> YELLOW_MEDIUM // Medium
        1 -> PRIMARY       // Low
        else -> PRIMARY    // iOS default branch
    }

    /**
     * Overdue indicator used by the "Overdue" stat pill:
     *  - true  → ERROR (scarlet)
     *  - false → TEXT_SECONDARY (muted)
     *
     * iOS: `entry.overdueCount > 0 ? Color.appError : Color.appTextSecondary`.
     */
    fun colorForOverdue(isOverdue: Boolean): Color =
        if (isOverdue) ERROR else TEXT_SECONDARY

    /**
     * The left priority-bar color for a task row. Overdue tasks always get
     * [ERROR] regardless of priority, matching iOS
     * `OrganicTaskRowView.priorityColor`.
     */
    fun taskRowColor(priorityLevel: Int, isOverdue: Boolean): Color =
        if (isOverdue) ERROR else colorForPriority(priorityLevel)

    /**
     * Due-date pill text color. iOS:
     * `.foregroundStyle(task.isOverdue ? Color.appError : Color.appAccent)`
     */
    fun dueDateTextColor(isOverdue: Boolean): Color =
        if (isOverdue) ERROR else ACCENT

    // -- Glance ColorProvider convenience accessors --

    val primary: ColorProvider get() = ColorProvider(PRIMARY)
    val accent: ColorProvider get() = ColorProvider(ACCENT)
    val error: ColorProvider get() = ColorProvider(ERROR)
    val textPrimary: ColorProvider get() = ColorProvider(TEXT_PRIMARY)
    val textSecondary: ColorProvider get() = ColorProvider(TEXT_SECONDARY)
    val textOnPrimary: ColorProvider get() = ColorProvider(TEXT_ON_PRIMARY)
    val backgroundPrimary: ColorProvider get() = ColorProvider(BACKGROUND_PRIMARY)
    val backgroundSecondary: ColorProvider get() = ColorProvider(BACKGROUND_SECONDARY)
}
