package de.krisenvorrat.app.ui.navigation

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import de.krisenvorrat.app.ui.camera.CameraScreen
import de.krisenvorrat.app.ui.category.CategoryListScreen
import de.krisenvorrat.app.ui.dashboard.DashboardScreen
import de.krisenvorrat.app.ui.item.ItemFormScreen
import de.krisenvorrat.app.ui.item.ItemListScreen
import de.krisenvorrat.app.ui.location.LocationListScreen
import de.krisenvorrat.app.ui.messaging.ChatScreen
import de.krisenvorrat.app.ui.messaging.UserListScreen
import de.krisenvorrat.app.ui.settings.SettingsScreen
import de.krisenvorrat.app.ui.warnings.WarningsScreen

@Composable
internal fun KrisenvorratNavGraph(
    navController: NavHostController,
    modifier: Modifier = Modifier
) {
    NavHost(
        navController = navController,
        startDestination = Screen.Dashboard,
        modifier = modifier
    ) {
        composable<Screen.Dashboard> {
            DashboardScreen()
        }

        composable<Screen.ItemList> {
            ItemListScreen(
                onAddItem = {
                    navController.navigate(Screen.ItemForm())
                },
                onItemClick = { itemId ->
                    navController.navigate(Screen.ItemForm(itemId = itemId))
                },
                onCategoriesClick = {
                    navController.navigate(Screen.CategoryManagement)
                },
                onLocationsClick = {
                    navController.navigate(Screen.LocationManagement)
                },
                onCameraClick = if (de.krisenvorrat.app.BuildConfig.FEATURE_CAMERA_ENABLED) {
                    { navController.navigate(Screen.CameraCapture) }
                } else {
                    {}
                }
            )
        }

        composable<Screen.ItemForm> {
            ItemFormScreen(
                onNavigateBack = {
                    navController.popBackStack()
                }
            )
        }

        if (de.krisenvorrat.app.BuildConfig.FEATURE_CAMERA_ENABLED) {
            composable<Screen.CameraCapture> {
                CameraScreen(
                    onNavigateToItemForm = { prefillJson ->
                        navController.navigate(Screen.ItemForm(prefillJson = prefillJson))
                    },
                    onNavigateBack = { navController.popBackStack() }
                )
            }
        }

        composable<Screen.CategoryManagement> {
            CategoryListScreen(
                onNavigateBack = {
                    navController.popBackStack()
                }
            )
        }

        composable<Screen.LocationManagement> {
            LocationListScreen(
                onNavigateBack = {
                    navController.popBackStack()
                }
            )
        }

        composable<Screen.Warnings> {
            WarningsScreen()
        }

        composable<Screen.Settings> {
            SettingsScreen()
        }

        composable<Screen.UserList> {
            UserListScreen(
                onUserClick = { id, username ->
                    navController.navigate(Screen.Chat(recipientId = id, recipientUsername = username))
                }
            )
        }

        composable<Screen.Chat> {
            ChatScreen(
                onNavigateBack = { navController.popBackStack() }
            )
        }
    }
}
