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

import androidx.compose.runtime.Stable
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.admin.character.edit.GMCharacterEditPage
import com.pixelized.desktop.lwa.utils.extention.ARG

@Stable
object GMCharacterEditDestination {
    private const val ROUTE = "GameMasterCharacterEdit"
    private const val CHARACTER_ID = "id"

    @Stable
    fun baseRoute() = "$ROUTE?${CHARACTER_ID.ARG}"

    @Stable
    fun navigationRoute(characterSheetId: String?) = "$ROUTE?$CHARACTER_ID=$characterSheetId"

    @Stable
    fun arguments() = listOf(
        navArgument(CHARACTER_ID) {
            nullable = true
            type = NavType.StringType
        }
    )

    @Stable
    data class Argument(
        val characterSheetId: String?,
    ) {
        constructor(savedStateHandle: SavedStateHandle) : this(
            characterSheetId = savedStateHandle.get<String>(CHARACTER_ID),
        )
    }
}

fun NavGraphBuilder.composableGameMasterCharacterEditPage() {
    composable(
        route = GMCharacterEditDestination.baseRoute(),
        arguments = GMCharacterEditDestination.arguments(),
    ) {
        GMCharacterEditPage()
    }
}

fun NavHostController.navigateToGameMasterCharacterEditPage(
    characterSheetId: String?,
) {
    val route = GMCharacterEditDestination.navigationRoute(
        characterSheetId = characterSheetId,
    )
    navigate(route = route)
}