package com.pixelized.headache.ui.navigation.main

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.navigation3.rememberViewModelStoreNavEntryDecorator
import androidx.navigation3.runtime.EntryProviderScope
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.runtime.rememberSaveableStateHolderNavEntryDecorator
import androidx.navigation3.ui.NavDisplay
import com.pixelized.headache.ui.navigation.destination.HomePageDestination
import com.pixelized.headache.ui.navigation.destination.calendarChooserDestinationEntry
import com.pixelized.headache.ui.navigation.destination.eventDestinationEntry
import com.pixelized.headache.ui.navigation.destination.homeDestinationEntry
import com.pixelized.headache.ui.navigation.destination.monthSummaryDestinationEntry
import com.pixelized.headache.ui.navigation.destination.yearSummaryDestinationEntry
import com.pixelized.headache.ui.navigation.home.HomeNavigator
import com.pixelized.headache.ui.page.home.HomePage
import dagger.hilt.android.scopes.ActivityRetainedScoped

val LocalMainNavigator = staticCompositionLocalOf<MainNavigator> {
    error("Local Navigation no yet ready")
}

@Composable
fun MainNavDisplay(
    modifier: Modifier = Modifier,
    mainNavigator: MainNavigator,
    homeNavigator: HomeNavigator,
) {
    CompositionLocalProvider(
        LocalMainNavigator provides mainNavigator,
    ) {
        NavDisplay(
            modifier = modifier,
            backStack = mainNavigator.backStack,
            entryDecorators = listOf(
                rememberSaveableStateHolderNavEntryDecorator(),
                rememberViewModelStoreNavEntryDecorator(),
            ),
            onBack = {
                mainNavigator.popBackstack()
            },
            entryProvider = entryProvider {

                homeDestinationEntry(navigator = homeNavigator)
                calendarChooserDestinationEntry()
                eventDestinationEntry()
                monthSummaryDestinationEntry()
                yearSummaryDestinationEntry()
            }
        )
    }
}