package com.isidroid.c23.ui.navigation.destinations

import androidx.compose.runtime.Composable
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.isidroid.c23.ui._component.BackCloseComponent
import com.isidroid.c23.ui.navigation.routeJobDetails
import com.isidroid.c23.ui.navigation.routeMap
import com.isidroid.c23.ui.navigation.routeSelectContent
import com.isidroid.c23.ui.screen.render_preview.RenderContract
import com.isidroid.c23.ui.screen.render_preview.RenderPreviewScreen
import com.isidroid.c23.ui.screen.render_preview.RenderViewModel
import com.isidroid.core.ext.navigateSingleTopTo
import timber.log.Timber

@Composable
fun RenderScreenDestination(navController: NavController) {
    val viewModel: RenderViewModel = hiltViewModel()

    RenderPreviewScreen(
        state = viewModel.viewState,
        renderResultsFlow = viewModel.renderResultsFlow,
        effectFlow = viewModel.effect,
        onEventSent = { event -> viewModel.setEvent(event) },
        onNavigationRequested = { effect ->
            when (effect) {
                RenderContract.Effect.Navigation.ToBack -> {
                    if (navController.previousBackStackEntry != null)
                        navController.popBackStack()
                    else
                        navController.navigateSingleTopTo(routeSelectContent())
                }

                RenderContract.Effect.Navigation.ToMap -> navController.navigate(routeMap())
                RenderContract.Effect.Navigation.ToSelectContent -> navController.navigateSingleTopTo(routeSelectContent())
                is RenderContract.Effect.Navigation.ToDetails -> navController.navigateSingleTopTo(routeJobDetails(effect.jobId))
            }
        }
    )
}