package com.isidroid.c23.ui.screen.home

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.LottieConstants
import com.airbnb.lottie.compose.rememberLottieComposition
import com.isidroid.c23.R
import com.isidroid.core.vm.SIDE_EFFECTS_KEY
import kotlinx.coroutines.flow.Flow

@Composable
fun HomeScreen(
    state: State<HomeContract.State>,
    effectFlow: Flow<HomeContract.Effect>?,
    onNavigationRequested: (navigationEffect: HomeContract.Effect.Navigation) -> Unit,
    modifier: Modifier = Modifier
) {

    LaunchedEffect(SIDE_EFFECTS_KEY) {
        effectFlow?.collect { effect ->
            when (effect) {
                is HomeContract.Effect.Navigation -> onNavigationRequested(effect)
            }
        }
    }

    if (state.value.isLoading) {
        val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.startapp))
        LottieAnimation(
            composition = composition,
//            progress = { progress },
            iterations = LottieConstants.IterateForever,
            modifier = modifier
                .fillMaxSize()
                .padding(48.dp)
        )
    }
}


