package com.lazy.emulate.ui.navigation

import androidx.compose.material3.windowsizeclass.WindowSizeClass
import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.lazy.emulate.data.PreferencesManager
import com.lazy.emulate.data.repository.GameRepository
import com.lazy.emulate.emulation.ConsoleType
import com.lazy.emulate.input.ButtonMappingManager
import com.lazy.emulate.input.ControllerManager
import com.lazy.emulate.ui.screens.controller.ControllerLayoutScreen
import com.lazy.emulate.ui.screens.controller.ControllerTestScreen
import com.lazy.emulate.ui.screens.game.GameScreen
import com.lazy.emulate.ui.screens.home.HomeScreen
import com.lazy.emulate.ui.screens.settings.ControllerSettingsScreen
import com.lazy.emulate.ui.screens.settings.SettingsScreen

@Composable
fun EmulateNavGraph(
    navController: NavHostController,
    windowSizeClass: WindowSizeClass,
    gameRepository: GameRepository,
    controllerManager: ControllerManager,
    preferencesManager: PreferencesManager,
    buttonMappingManager: ButtonMappingManager
) {
    NavHost(
        navController = navController,
        startDestination = Screen.Home.route
    ) {
        composable(Screen.Home.route) {
            HomeScreen(
                windowSizeClass = windowSizeClass,
                gameRepository = gameRepository,
                onGameSelected = { game ->
                    navController.navigate(Screen.Game.createRoute(game.id))
                },
                onSettingsClick = {
                    navController.navigate(Screen.Settings.route)
                }
            )
        }

        composable(Screen.Settings.route) {
            SettingsScreen(
                preferencesManager = preferencesManager,
                onBack = { navController.popBackStack() },
                onControllerSettings = { navController.navigate(Screen.ControllerSettings.route) }
            )
        }

        composable(Screen.ControllerSettings.route) {
            ControllerSettingsScreen(
                controllerManager = controllerManager,
                preferencesManager = preferencesManager,
                buttonMappingManager = buttonMappingManager,
                onBack = { navController.popBackStack() },
                onTestController = { navController.navigate(Screen.ControllerTest.route) },
                onControllerLayout = { consoleType ->
                    navController.navigate(Screen.ControllerLayout.createRoute(consoleType.name))
                }
            )
        }

        composable(Screen.ControllerTest.route) {
            ControllerTestScreen(
                controllerManager = controllerManager,
                onBack = { navController.popBackStack() }
            )
        }

        composable(
            route = Screen.ControllerLayout.route,
            arguments = listOf(navArgument("consoleType") { type = NavType.StringType })
        ) { backStackEntry ->
            val consoleTypeName = backStackEntry.arguments?.getString("consoleType") ?: return@composable
            val consoleType = ConsoleType.valueOf(consoleTypeName)
            ControllerLayoutScreen(
                consoleType = consoleType,
                onBack = { navController.popBackStack() }
            )
        }

        composable(
            route = Screen.Game.route,
            arguments = listOf(navArgument("gameId") { type = NavType.StringType })
        ) { backStackEntry ->
            val gameId = backStackEntry.arguments?.getString("gameId") ?: return@composable
            val game = gameRepository.games.value.find { it.id == gameId } ?: return@composable
            GameScreen(
                game = game,
                controllerManager = controllerManager,
                preferencesManager = preferencesManager,
                onBack = { navController.popBackStack() }
            )
        }
    }
}
