package com.isidroid.c23.ui.screen.details

import androidx.activity.compose.BackHandler
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.ui.Modifier
import com.isidroid.c23.ui.screen.details.component.DetailsV2
import com.isidroid.c23.ui.screen.map._components.RequestLocationPermissionComponent
import com.isidroid.core.vm.SIDE_EFFECTS_KEY
import com.isidroid.ui.maps.model.MapMarker
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow

@Composable
fun JobDetailsScreen(
    state: State<JobDetailsContract.State>,
    effectFlow: Flow<JobDetailsContract.Effect>?,
    spotResultStateFlow: StateFlow<List<MapMarker>>,
    onEventSent: (event: JobDetailsContract.Event) -> Unit,
    onNavigationRequested: (navigationEffect: JobDetailsContract.Effect.Navigation) -> Unit,
    modifier: Modifier = Modifier,
) {
    RequestLocationPermissionComponent(
        requestLocationPermission = state.value.requestLocationPermission,
        onGranted = { onEventSent(JobDetailsContract.Event.LocationPermissionGranted) }
    )

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

    BackHandler { onEventSent(JobDetailsContract.Event.ToBack) }

//    DetailsV1(state, effectFlow, spotResultStateFlow, onEventSent, onNavigationRequested, modifier)
    DetailsV2(state, spotResultStateFlow, onEventSent, modifier)
}