package com.ponzischeme89.memby.ui.theme

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.em
import androidx.tv.material3.LocalTextStyle
import androidx.tv.material3.MaterialTheme
import androidx.tv.material3.darkColorScheme

// The scheme is the same near-blacks the screens actually paint (see DesignTokens.kt), so
// a component that falls back to a theme colour lands on the surface it is sitting on
// rather than one shade beside it.
//
// Built per composition rather than held as a constant, because the palette is the
// gateway's answer and changes under a running app. `darkColorScheme` reads the tokens, so
// this function is what carries a theme change into every component that never names a
// colour of its own.
private fun embyColors() = darkColorScheme(
    primary = MembyAccent,
    onPrimary = androidx.compose.ui.graphics.Color.White,
    surface = MembySurfaceRaised,
    background = MembySurface,
)

@Composable
fun MembyTheme(content: @Composable () -> Unit) {
    // Use Android's complete Roboto-backed sans family rather than pinning every label
    // to sans-serif-medium. The generic family lets Compose select real regular,
    // medium, semibold and bold faces from each Text's FontWeight, restoring hierarchy
    // and making body copy substantially easier to scan from TV distance.
    //
    // A platform family also has every script Android supports, adds no APK weight, and
    // cannot flash or fail while a downloadable font is fetched.
    val appTextStyle = LocalTextStyle.current.copy(
        fontFamily = FontFamily.SansSerif,
        fontWeight = FontWeight.Normal,
        // Slightly open tracking and leading keep small metadata and multi-line
        // summaries from feeling cramped without making large headings look loose.
        letterSpacing = 0.006.em,
        lineHeight = 1.22.em,
    )
    MaterialTheme(colorScheme = embyColors()) {
        CompositionLocalProvider(LocalTextStyle provides appTextStyle) {
            content()
        }
    }
}
