package com.isidroid.c23.ui.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.composable
import com.isidroid.c23.constant.Argument
import com.isidroid.c23.ext.BasicNavHost
import com.isidroid.c23.ext.isDebug
import com.isidroid.c23.ext.makeNavArgument
import com.isidroid.c23.ui.navigation.destinations.ContentScreenDestination
import com.isidroid.c23.ui.navigation.destinations.HomeScreenDestination
import com.isidroid.c23.ui.navigation.destinations.JobDetailsDestination
import com.isidroid.c23.ui.navigation.destinations.MapScreenDestination
import com.isidroid.c23.ui.navigation.destinations.PrintJobsScreenDestination
import com.isidroid.c23.ui.navigation.destinations.RenderScreenDestination
import timber.log.Timber

@Composable
fun AppNavHost(
    navController: NavHostController,
    modifier: Modifier = Modifier
) {
    val startDestination = if (isDebug())
        routeHome(uris = "{${Argument.URI}}")
    else
        routeHome(uris = "{${Argument.URI}}")

    BasicNavHost(
        navController = navController,
        startDestination = startDestination,
        modifier = modifier,
    ) {
        composable(
            route = routeHome(uris = "{${Argument.URI}}"),
            arguments = listOf(makeNavArgument(Argument.URI, NavType.StringType)),
            content = { HomeScreenDestination(navController) }
        )

        composable(
            route = routeSelectContent(),
            content = { ContentScreenDestination(navController) }
        )

        composable(
            route = routePrintJobs(),
            content = { PrintJobsScreenDestination(navController) }
        )

        composable(
            route = routeRenderPreview(uris = "{${Argument.URI}}"),
            arguments = listOf(makeNavArgument(Argument.URI, NavType.StringType)),
            content = { RenderScreenDestination(navController) }
        )

        composable(
            route = routeMap(
                lat = "{${Argument.LATITUDE}}",
                lng = "{${Argument.LONGITUDE}}",
                spotCode = "{${Argument.SPOT_CODE}}",
                uris = "{${Argument.URI}}"
            ),
            arguments = listOf(
                makeNavArgument(Argument.LATITUDE, NavType.StringType),
                makeNavArgument(Argument.LONGITUDE, NavType.StringType),
                makeNavArgument(Argument.SPOT_CODE, NavType.StringType),
                makeNavArgument(Argument.URI, NavType.StringType)
            ),
            content = { MapScreenDestination(navController) }
        )

        composable(
            route = routeJobDetails(id = "{${Argument.ID}}"),
            arguments = listOf(
                makeNavArgument(Argument.ID, NavType.StringType, nullable = true)
            ),
            content = { JobDetailsDestination(navController) }
        )
    }
}

