import android.util.Log
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.airbnb.lottie.compose.*

@Composable
fun LottieAnimationComponent(
    modifier: Modifier = Modifier,
    assetName: String,
    playOnce: Boolean = true
) {
    val composition by rememberLottieComposition(LottieCompositionSpec.Asset(assetName))

    var isPlaying by remember { mutableStateOf(false) }

    // Cek apakah composition berhasil dimuat sebelum menampilkan animasi
    if (composition != null) {
        val progress by animateLottieCompositionAsState(
            composition = composition,
            isPlaying = isPlaying,
            restartOnPlay = !playOnce
        )

        LottieAnimation(
            composition = composition,
            progress = { progress },
            modifier = modifier
        )

        // Jika playOnce = false, jalankan animasi otomatis
        LaunchedEffect(Unit) {
            isPlaying = true
        }
    } else {
        Log.e("LottieAnimation", "Failed to load Lottie animation: $assetName")
    }
}
