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.item.edit.GMItemEditPage
import com.pixelized.desktop.lwa.utils.extention.ARG

@Stable
object GMItemEditDestination {
    private const val ROUTE = "GameMasterItemEdit"
    private const val ITEM_ID = "id"

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

    @Stable
    fun navigationRoute(itemId: String?) = "$ROUTE?$ITEM_ID=$itemId"

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

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

fun NavGraphBuilder.composableGameMasterItemEditPage() {
    composable(
        route = GMItemEditDestination.baseRoute(),
        arguments = GMItemEditDestination.arguments(),
    ) {
        GMItemEditPage()
    }
}

fun NavHostController.navigateToGameMasterItemEditPage(
    itemId: String?,
) {
    val route = GMItemEditDestination.navigationRoute(
        itemId = itemId,
    )
    navigate(route = route)
}