/*
 * Copyright (C) 2026 SHX VPN
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 */

package org.strongswan.android.ui.compose.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Shapes
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

val ConnectionCardRadius = 16.dp
val ScreenPadding = 16.dp

val ProtonPurpleLight = Color(0xFF6D4AFF)
val ProtonPurpleDark = Color(0xFF9C7CFF)
val SuccessGreenLight = Color(0xFF148A45)
val SuccessGreenDark = Color(0xFF28A671)

private val LightColors = lightColorScheme(
	primary = ProtonPurpleLight,
	onPrimary = Color.White,
	primaryContainer = Color(0xFFEDE7FF),
	onPrimaryContainer = Color(0xFF2E1A7A),
	inversePrimary = ProtonPurpleDark,
	secondary = Color(0xFF625B71),
	onSecondary = Color.White,
	secondaryContainer = Color(0xFFE8DEF8),
	onSecondaryContainer = Color(0xFF1D192B),
	tertiary = SuccessGreenLight,
	onTertiary = Color.White,
	tertiaryContainer = Color(0xFFC8F2DA),
	onTertiaryContainer = Color(0xFF07391D),
	background = Color(0xFFF7F7F8),
	onBackground = Color(0xFF1B1B20),
	surface = Color(0xFFF7F7F8),
	onSurface = Color(0xFF1B1B20),
	surfaceVariant = Color(0xFFEBEBEF),
	onSurfaceVariant = Color(0xFF57575F),
	surfaceDim = Color(0xFFDADAE0),
	surfaceBright = Color(0xFFF7F7F8),
	surfaceContainerLowest = Color.White,
	surfaceContainerLow = Color(0xFFF2F2F4),
	surfaceContainer = Color.White,
	surfaceContainerHigh = Color(0xFFF2F2F4),
	surfaceContainerHighest = Color(0xFFEBEBEF),
	outline = Color(0xFF797981),
	outlineVariant = Color(0xFFD0D0D6),
	inverseSurface = Color(0xFF2F2F35),
	inverseOnSurface = Color(0xFFF2F2F4),
	error = Color(0xFFD8303C),
	onError = Color.White,
	errorContainer = Color(0xFFFFDAD6),
	onErrorContainer = Color(0xFF410002),
	scrim = Color.Black,
)

private val DarkColors = darkColorScheme(
	primary = ProtonPurpleDark,
	onPrimary = Color(0xFF150F35),
	primaryContainer = Color(0xFF3B2C88),
	onPrimaryContainer = Color(0xFFEDE7FF),
	inversePrimary = ProtonPurpleLight,
	secondary = Color(0xFFCCC2DC),
	onSecondary = Color(0xFF332D41),
	secondaryContainer = Color(0xFF4A4458),
	onSecondaryContainer = Color(0xFFE8DEF8),
	tertiary = SuccessGreenDark,
	onTertiary = Color(0xFF00391B),
	tertiaryContainer = Color(0xFF005228),
	onTertiaryContainer = Color(0xFFC8F2DA),
	background = Color(0xFF17171A),
	onBackground = Color(0xFFE6E6E9),
	surface = Color(0xFF17171A),
	onSurface = Color(0xFFE6E6E9),
	surfaceVariant = Color(0xFF2C2C31),
	onSurfaceVariant = Color(0xFFC0C0C8),
	surfaceDim = Color(0xFF17171A),
	surfaceBright = Color(0xFF3B3B41),
	surfaceContainerLowest = Color(0xFF121214),
	surfaceContainerLow = Color(0xFF1F1F23),
	surfaceContainer = Color(0xFF242428),
	surfaceContainerHigh = Color(0xFF2C2C31),
	surfaceContainerHighest = Color(0xFF36363B),
	outline = Color(0xFF8B8B93),
	outlineVariant = Color(0xFF3D3D43),
	inverseSurface = Color(0xFFE6E6E9),
	inverseOnSurface = Color(0xFF2C2C31),
	error = Color(0xFFFF8A8C),
	onError = Color(0xFF490005),
	errorContainer = Color(0xFF68000E),
	onErrorContainer = Color(0xFFFFDAD6),
	scrim = Color.Black,
)

@Immutable
data class ShxStatusColors(
	val connected: Color,
	val connectedContainer: Color,
	val onConnectedContainer: Color,
	val connecting: Color,
	val connectingContainer: Color,
	val onConnectingContainer: Color,
	val disconnected: Color,
	val disconnectedContainer: Color,
	val onDisconnectedContainer: Color,
)

private val LightStatusColors = ShxStatusColors(
	connected = SuccessGreenLight,
	connectedContainer = Color(0xFFDCF3E5),
	onConnectedContainer = Color(0xFF07391D),
	connecting = ProtonPurpleLight,
	connectingContainer = Color(0xFFEDE7FF),
	onConnectingContainer = Color(0xFF2E1A7A),
	disconnected = Color(0xFFC0362C),
	disconnectedContainer = Color(0xFFFFE1DE),
	onDisconnectedContainer = Color(0xFF410001),
)

private val DarkStatusColors = ShxStatusColors(
	connected = SuccessGreenDark,
	connectedContainer = Color(0xFF0B3B24),
	onConnectedContainer = Color(0xFFC8F2DA),
	connecting = ProtonPurpleDark,
	connectingContainer = Color(0xFF2E2360),
	onConnectingContainer = Color(0xFFEDE7FF),
	disconnected = Color(0xFFED6B62),
	disconnectedContainer = Color(0xFF4A1712),
	onDisconnectedContainer = Color(0xFFFFE1DE),
)

val LocalShxStatusColors = staticCompositionLocalOf { LightStatusColors }

private val ShxShapes = Shapes(
	extraSmall = RoundedCornerShape(6.dp),
	small = RoundedCornerShape(10.dp),
	medium = RoundedCornerShape(14.dp),
	large = RoundedCornerShape(16.dp),
	extraLarge = RoundedCornerShape(22.dp),
)

@Composable
fun ShxTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit)
{
	val colors = if (darkTheme) DarkColors else LightColors
	val statusColors = if (darkTheme) DarkStatusColors else LightStatusColors
	CompositionLocalProvider(LocalShxStatusColors provides statusColors) {
		MaterialTheme(
			colorScheme = colors,
			shapes = ShxShapes,
			content = content,
		)
	}
}
