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.map.edit.GMMapEditPage
import com.pixelized.desktop.lwa.ui.screen.admin.map.list.GMMapPage
import com.pixelized.desktop.lwa.utils.extention.ARG

object GMMapEditDestination {
    private const val ROUTE = "GameMasterMapEdit"
    private const val ARG_MAP_ID = "mapId"

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

    fun navigationRoute(mapId: String?) = "$ROUTE?$ARG_MAP_ID=$mapId"

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

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

fun NavGraphBuilder.composableGameMasterMapEditPage() {
    composable(
        route = GMMapEditDestination.baseRoute(),
        arguments = GMMapEditDestination.arguments(),
    ) {
        GMMapEditPage()
    }
}

fun NavHostController.navigateToGameMasterMapEditPage(
    mapId: String?,
) {
    val route = GMMapEditDestination.navigationRoute(
        mapId = mapId,
    )
    navigate(route = route)
}