package com.pixelized.headache.ui.theme.color

import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.Stable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.compositeOver
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import kotlin.math.ln

@Stable
data class HeadacheColors(
    val base: ColorScheme,
    val calendar: Calendar,
    val pill: Pill,
) {
    @Stable
    data class Calendar(
        val headache: Color,
        val onHeadache: Color,
    )

    @Stable
    data class Pill(
        val unknown: Color,
        val ibuprofene400: Color,
        val paracetamol1000: Color,
        val spifen400: Color,
        val eletriptan40: Color,
    )
}

@Composable
@Stable
fun headacheDarkColorScheme(
    base: ColorScheme = darkColorScheme(),
    calendar: HeadacheColors.Calendar = HeadacheColors.Calendar(
        headache = HeadacheColorPalette.Calendar.Headache,
        onHeadache = Color.White,
    ),
    pill: HeadacheColors.Pill = HeadacheColors.Pill(
        unknown = HeadacheColorPalette.Pill.Unknown,
        ibuprofene400 = HeadacheColorPalette.Pill.Ibuprofene400,
        paracetamol1000 = HeadacheColorPalette.Pill.Paracetamol1000,
        spifen400 = HeadacheColorPalette.Pill.Spifen400,
        eletriptan40 = HeadacheColorPalette.Pill.Eletriptan40,
    ),
) = HeadacheColors(
    base = base,
    calendar = calendar,
    pill = pill,
)

@Composable
@Stable
fun headacheLightColorScheme(
    base: ColorScheme = lightColorScheme(),
    calendar: HeadacheColors.Calendar = HeadacheColors.Calendar(
        headache = HeadacheColorPalette.Calendar.Headache,
        onHeadache = Color.White,
    ),
    pill: HeadacheColors.Pill = HeadacheColors.Pill(
        unknown = HeadacheColorPalette.Pill.Unknown,
        ibuprofene400 = HeadacheColorPalette.Pill.Ibuprofene400,
        paracetamol1000 = HeadacheColorPalette.Pill.Paracetamol1000,
        spifen400 = HeadacheColorPalette.Pill.Spifen400,
        eletriptan40 = HeadacheColorPalette.Pill.Eletriptan40,
    ),
) = HeadacheColors(
    base = base,
    calendar = calendar,
    pill = pill,
)

@ReadOnlyComposable
@Composable
fun calculateElevatedColor(
    color: Color,
    onColor: Color = MaterialTheme.colorScheme.surface,
    elevation: Dp,
): Color {
    return if (elevation > 0.dp) {
        val foregroundColor = calculateForegroundColor(onColor, elevation)
        foregroundColor.compositeOver(color)
    } else {
        color
    }
}

@ReadOnlyComposable
@Composable
fun calculateForegroundColor(color: Color, elevation: Dp): Color {
    val alpha = ((4.5f * ln(elevation.value + 1)) + 2f) / 100f
    return color.copy(alpha = alpha)
}