package app.voltplan.cable.ui.components

import androidx.annotation.DrawableRes
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
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.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import kotlinx.coroutines.delay

/**
 * Auto-advancing onboarding illustration carousel. Mirrors iOS `OnboardingCarouselView`:
 * cycles through the images every 8s with a horizontal slide. A single image stays static.
 */
@Composable
fun OnboardingCarousel(
    @DrawableRes images: List<Int>,
    modifier: Modifier = Modifier,
) {
    if (images.isEmpty()) return
    var index by remember(images) { mutableIntStateOf(0) }

    if (images.size > 1) {
        LaunchedEffect(images) {
            while (true) {
                delay(8_000)
                index = (index + 1) % images.size
            }
        }
    }

    AnimatedContent(
        targetState = index,
        transitionSpec = {
            slideInHorizontally(tween(800)) { it } togetherWith
                slideOutHorizontally(tween(800)) { -it }
        },
        label = "onboarding-carousel",
        modifier = modifier,
    ) { i ->
        Image(
            painter = painterResource(images[i]),
            contentDescription = null,
            contentScale = ContentScale.Fit,
            modifier = Modifier.fillMaxSize(),
        )
    }
}
