package com.pixelized.desktop.lwa.ui.navigation.screen.destination

import androidx.lifecycle.SavedStateHandle
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.pixelized.desktop.lwa.ui.screen.levelup.LevelUpScreen
import com.pixelized.desktop.lwa.utils.extention.ARG

object LevelUpDestination {
    private const val ROUTE = "levelUp"

    private const val CHARACTER_SHEET_ID = "characterSheetId"

    fun baseRoute() = "$ROUTE?${CHARACTER_SHEET_ID.ARG}"

    fun navigationRoute(characterSheetId: String) = "$ROUTE?$CHARACTER_SHEET_ID=${characterSheetId}"

    fun arguments() = listOf(
        navArgument(CHARACTER_SHEET_ID) {
            nullable = false
            type = NavType.StringType
        },
    )

    data class Argument(
        val characterSheetId: String,
    ) {
        constructor(savedStateHandle: SavedStateHandle) : this(
            characterSheetId = savedStateHandle
                .get<String>(CHARACTER_SHEET_ID)
                ?: error("missing characterSheetId parameter"),
        )
    }
}

fun NavGraphBuilder.composableLevelUp() {
    composable(
        route = LevelUpDestination.baseRoute(),
        arguments = LevelUpDestination.arguments(),
    ) {
        LevelUpScreen()
    }
}

fun NavHostController.navigateToLevelScreen(
    characterSheetId: String,
) {
    val route = LevelUpDestination.navigationRoute(characterSheetId = characterSheetId)
    navigate(route = route)
}