package app.closer.ui.components

import android.provider.Settings
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.layout.heightIn
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import app.closer.ui.brand.CloserBrandCopy
import kotlinx.coroutines.delay

@Composable
fun BrandMessageRotator(
    modifier: Modifier = Modifier,
    messages: List<String> = CloserBrandCopy.privacyMessages,
    color: Color = MaterialTheme.colorScheme.onSurfaceVariant,
    style: TextStyle = MaterialTheme.typography.bodyMedium,
    intervalMillis: Long = 4_500L,
    /** This message lingers [primaryHoldMultiplier]× longer than the rest (the flagship slogan). */
    primaryMessage: String? = CloserBrandCopy.primaryMessage,
    primaryHoldMultiplier: Int = 3
) {
    if (messages.isEmpty()) return

    val context = LocalContext.current
    val reduceMotion = remember(context) {
        runCatching {
            Settings.Global.getFloat(
                context.contentResolver,
                Settings.Global.ANIMATOR_DURATION_SCALE,
                1f
            ) == 0f
        }.getOrDefault(false)
    }
    var index by remember(messages) { mutableIntStateOf(0) }

    LaunchedEffect(messages, intervalMillis, reduceMotion, primaryMessage, primaryHoldMultiplier) {
        if (!reduceMotion && messages.size > 1) {
            while (true) {
                // Hold the flagship slogan on screen longer than the rest.
                val hold = if (messages[index] == primaryMessage) {
                    intervalMillis * primaryHoldMultiplier
                } else {
                    intervalMillis
                }
                delay(hold)
                index = (index + 1) % messages.size
            }
        }
    }

    AnimatedContent(
        targetState = messages[index],
        modifier = modifier.heightIn(min = 44.dp),
        contentAlignment = Alignment.Center,
        transitionSpec = {
            fadeIn(tween(320)) togetherWith fadeOut(tween(220))
        },
        label = "closerBrandMessage"
    ) { message ->
        Text(
            text = message,
            style = style,
            color = color,
            textAlign = TextAlign.Center,
            maxLines = 3
        )
    }
}
