import SwiftUI

/// Mirrors the Kotlin `WidgetPalette` in WidgetRenderer.kt so day/week widgets
/// look identical across platforms. All values are hex tokens from the in-app
/// LightAppTheme / DarkAppTheme — do not swap to system colors, the whole
/// point is platform-independent branding.
struct WidgetPalette {
  let background: Color
  let textPrimary: Color
  let textSecondary: Color
  let divider: Color
  let breakBlock: Color
  let watermarkOpacity: Double

  static let light = WidgetPalette(
    background: Color(red: 0xFC / 255, green: 0xF7 / 255, blue: 0xF5 / 255),
    textPrimary: Color(red: 0x11 / 255, green: 0x11 / 255, blue: 0x11 / 255),
    textSecondary: Color(red: 0x55 / 255, green: 0x55 / 255, blue: 0x55 / 255),
    divider: Color.black.opacity(0x22 / 255.0),
    breakBlock: Color.black.opacity(0x0C / 255.0),
    watermarkOpacity: 0.014
  )

  static let dark = WidgetPalette(
    background: Color(red: 0x1F / 255, green: 0x17 / 255, blue: 0x16 / 255),
    textPrimary: Color(red: 0xF1 / 255, green: 0xF1 / 255, blue: 0xF1 / 255),
    textSecondary: Color(red: 0xB0 / 255, green: 0xB0 / 255, blue: 0xB0 / 255),
    divider: Color.white.opacity(0x33 / 255.0),
    breakBlock: Color.white.opacity(0x14 / 255.0),
    watermarkOpacity: 0.025
  )

  static func resolve(themeMode: String, colorScheme: ColorScheme) -> WidgetPalette {
    let isDark: Bool
    switch themeMode {
    case "light": isDark = false
    case "dark": isDark = true
    default: isDark = colorScheme == .dark
    }
    return isDark ? .dark : .light
  }
}

private struct WidgetPaletteKey: EnvironmentKey {
  static let defaultValue = WidgetPalette.light
}

extension EnvironmentValues {
  var widgetPalette: WidgetPalette {
    get { self[WidgetPaletteKey.self] }
    set { self[WidgetPaletteKey.self] = newValue }
  }
}
