package app.voltplan.cable.ui.navigation

import androidx.compose.runtime.Composable
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import app.voltplan.cable.ui.batteries.BatteryEditorScreen
import app.voltplan.cable.ui.bom.BillOfMaterialsScreen
import app.voltplan.cable.ui.chargers.ChargerEditorScreen
import app.voltplan.cable.ui.library.ComponentLibraryScreen
import app.voltplan.cable.ui.loads.CalculatorScreen
import app.voltplan.cable.ui.settings.SettingsScreen
import app.voltplan.cable.ui.system.SystemDetailScreen
import app.voltplan.cable.ui.systems.SystemsScreen

object Routes {
    const val SYSTEMS = "systems"
    const val SYSTEM = "system/{systemId}"
    const val CALCULATOR = "calculator/{systemId}?loadId={loadId}"
    const val BATTERY = "battery/{systemId}?batteryId={batteryId}"
    const val CHARGER = "charger/{systemId}?chargerId={chargerId}"
    const val BOM = "bom/{systemId}"
    const val LIBRARY = "library?systemId={systemId}"
    const val SETTINGS = "settings"

    fun system(id: String) = "system/$id"
    fun library(systemId: String? = null) = "library" + (systemId?.let { "?systemId=$it" } ?: "")
    fun calculator(systemId: String, loadId: String? = null) =
        "calculator/$systemId" + (loadId?.let { "?loadId=$it" } ?: "")
    fun battery(systemId: String, batteryId: String? = null) =
        "battery/$systemId" + (batteryId?.let { "?batteryId=$it" } ?: "")
    fun charger(systemId: String, chargerId: String? = null) =
        "charger/$systemId" + (chargerId?.let { "?chargerId=$it" } ?: "")
    fun bom(systemId: String) = "bom/$systemId"
}

@Composable
fun CableNavHost() {
    val nav = rememberNavController()

    NavHost(navController = nav, startDestination = Routes.SYSTEMS) {
        composable(Routes.SYSTEMS) {
            SystemsScreen(
                onOpenSystem = { nav.navigate(Routes.system(it)) },
                onOpenSettings = { nav.navigate(Routes.SETTINGS) },
                onOpenLibrary = { nav.navigate(Routes.library()) },
            )
        }

        composable(
            Routes.SYSTEM,
            arguments = listOf(navArgument("systemId") { type = NavType.StringType }),
        ) { entry ->
            val systemId = entry.arguments?.getString("systemId").orEmpty()
            SystemDetailScreen(
                systemId = systemId,
                onBack = { nav.popBackStack() },
                onOpenLoad = { loadId -> nav.navigate(Routes.calculator(systemId, loadId)) },
                onNewLoad = { nav.navigate(Routes.calculator(systemId)) },
                onEditBattery = { id -> nav.navigate(Routes.battery(systemId, id)) },
                onNewBattery = { nav.navigate(Routes.battery(systemId)) },
                onEditCharger = { id -> nav.navigate(Routes.charger(systemId, id)) },
                onNewCharger = { nav.navigate(Routes.charger(systemId)) },
                onOpenBom = { nav.navigate(Routes.bom(systemId)) },
                onOpenLibrary = { nav.navigate(Routes.library(systemId)) },
            )
        }

        composable(
            Routes.CALCULATOR,
            arguments = listOf(
                navArgument("systemId") { type = NavType.StringType },
                navArgument("loadId") { type = NavType.StringType; nullable = true; defaultValue = null },
            ),
        ) { entry ->
            CalculatorScreen(
                systemId = entry.arguments?.getString("systemId").orEmpty(),
                loadId = entry.arguments?.getString("loadId"),
                onBack = { nav.popBackStack() },
            )
        }

        composable(
            Routes.BATTERY,
            arguments = listOf(
                navArgument("systemId") { type = NavType.StringType },
                navArgument("batteryId") { type = NavType.StringType; nullable = true; defaultValue = null },
            ),
        ) { entry ->
            BatteryEditorScreen(
                systemId = entry.arguments?.getString("systemId").orEmpty(),
                batteryId = entry.arguments?.getString("batteryId"),
                onBack = { nav.popBackStack() },
            )
        }

        composable(
            Routes.CHARGER,
            arguments = listOf(
                navArgument("systemId") { type = NavType.StringType },
                navArgument("chargerId") { type = NavType.StringType; nullable = true; defaultValue = null },
            ),
        ) { entry ->
            ChargerEditorScreen(
                systemId = entry.arguments?.getString("systemId").orEmpty(),
                chargerId = entry.arguments?.getString("chargerId"),
                onBack = { nav.popBackStack() },
            )
        }

        composable(
            Routes.BOM,
            arguments = listOf(navArgument("systemId") { type = NavType.StringType }),
        ) { entry ->
            BillOfMaterialsScreen(
                systemId = entry.arguments?.getString("systemId").orEmpty(),
                onBack = { nav.popBackStack() },
            )
        }

        composable(
            Routes.LIBRARY,
            arguments = listOf(navArgument("systemId") { type = NavType.StringType; nullable = true; defaultValue = null }),
        ) { entry ->
            ComponentLibraryScreen(
                targetSystemId = entry.arguments?.getString("systemId"),
                onBack = { nav.popBackStack() },
                onOpenSystem = { systemId ->
                    nav.popBackStack()
                    nav.navigate(Routes.system(systemId))
                },
            )
        }

        composable(Routes.SETTINGS) {
            SettingsScreen(onBack = { nav.popBackStack() })
        }
    }
}
