package com.tt.honeyDue.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

/**
 * Creates a Material3 ColorScheme from ThemeColors
 * Matches iOS theme system with support for light/dark mode
 */
fun ThemeColors.toColorScheme(isDark: Boolean): ColorScheme {
    return if (isDark) {
        darkColorScheme(
            primary = darkPrimary,
            onPrimary = darkTextOnPrimary,
            primaryContainer = darkPrimary.copy(alpha = 0.3f),
            onPrimaryContainer = darkTextPrimary,

            secondary = darkSecondary,
            onSecondary = darkTextOnPrimary,
            secondaryContainer = darkSecondary.copy(alpha = 0.3f),
            onSecondaryContainer = darkTextPrimary,

            tertiary = darkAccent,
            onTertiary = darkTextOnPrimary,
            tertiaryContainer = darkAccent.copy(alpha = 0.3f),
            onTertiaryContainer = darkTextPrimary,

            error = darkError,
            onError = Color.White,
            errorContainer = darkError.copy(alpha = 0.3f),
            onErrorContainer = darkTextPrimary,

            background = darkBackgroundPrimary,
            onBackground = darkTextPrimary,

            surface = darkBackgroundSecondary,
            onSurface = darkTextPrimary,
            surfaceVariant = darkBackgroundSecondary.copy(alpha = 0.8f),
            onSurfaceVariant = darkTextSecondary,

            outline = darkTextSecondary.copy(alpha = 0.4f),
            outlineVariant = darkTextSecondary.copy(alpha = 0.2f)
        )
    } else {
        lightColorScheme(
            primary = lightPrimary,
            onPrimary = lightTextOnPrimary,
            primaryContainer = lightPrimary.copy(alpha = 0.15f),
            onPrimaryContainer = lightPrimary,

            secondary = lightSecondary,
            onSecondary = lightTextOnPrimary,
            secondaryContainer = lightSecondary.copy(alpha = 0.15f),
            onSecondaryContainer = lightSecondary,

            tertiary = lightAccent,
            onTertiary = lightTextOnPrimary,
            tertiaryContainer = lightAccent.copy(alpha = 0.15f),
            onTertiaryContainer = lightAccent,

            error = lightError,
            onError = Color.White,
            errorContainer = lightError.copy(alpha = 0.15f),
            onErrorContainer = lightError,

            background = lightBackgroundPrimary,
            onBackground = lightTextPrimary,

            surface = lightBackgroundSecondary,
            onSurface = lightTextPrimary,
            surfaceVariant = lightBackgroundSecondary.copy(alpha = 0.5f),
            onSurfaceVariant = lightTextSecondary,

            outline = lightTextSecondary.copy(alpha = 0.4f),
            outlineVariant = lightTextSecondary.copy(alpha = 0.2f)
        )
    }
}

/**
 * Main theme composable - integrates with ThemeManager for dynamic theming.
 * Matches iOS multi-theme system.
 *
 * When [useDynamicColor] is true and the platform supports it (Android 12+),
 * the wallpaper-derived Material You scheme is used instead of [themeColors].
 * Falls back to [themeColors] on every other platform / OS version, so this
 * flag is always safe to pass.
 */
@Composable
fun HoneyDueTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    themeColors: ThemeColors = AppThemes.Default, // Can be overridden with ThemeManager.currentTheme
    useDynamicColor: Boolean = false,
    content: @Composable () -> Unit
) {
    val dynamicScheme = if (useDynamicColor) rememberDynamicColorScheme(darkTheme) else null
    val colorScheme = dynamicScheme ?: themeColors.toColorScheme(darkTheme)

    MaterialTheme(
        colorScheme = colorScheme,
        typography = AppTypography,
        shapes = AppShapes,
        content = content
    )
}

// Extension properties for easy access to theme colors in composables
val ColorScheme.textPrimary: Color
    @Composable get() = onBackground

val ColorScheme.textSecondary: Color
    @Composable get() = onSurfaceVariant

val ColorScheme.backgroundPrimary: Color
    @Composable get() = background

val ColorScheme.backgroundSecondary: Color
    @Composable get() = surface

val ColorScheme.accent: Color
    @Composable get() = tertiary
