# GravidApp.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2023 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */
    package dk.nsp.mingraviditet.ui.main

    import androidx.compose.animation.AnimatedVisibility
    import androidx.compose.animation.slideInVertically
    import androidx.compose.animation.slideOutVertically
    import androidx.compose.foundation.layout.Box
    import androidx.compose.foundation.layout.WindowInsets
    import androidx.compose.foundation.layout.consumeWindowInsets
    import androidx.compose.foundation.layout.fillMaxSize
    import androidx.compose.foundation.layout.padding
    import androidx.compose.material3.Icon
    import androidx.compose.material3.Scaffold
    import androidx.compose.material3.Surface
    import androidx.compose.runtime.Composable
    import androidx.compose.runtime.State
    import androidx.compose.runtime.collectAsState
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.graphics.Color
    import androidx.compose.ui.res.painterResource
    import androidx.compose.ui.res.stringResource
    import androidx.compose.ui.text.style.TextOverflow
    import androidx.compose.ui.unit.dp
    import androidx.hilt.navigation.compose.hiltViewModel
    import androidx.navigation.NavDestination
    import androidx.navigation.NavDestination.Companion.hierarchy
    import dk.nsp.mingraviditet.core.designsystem.component.GravidText
    import dk.nsp.mingraviditet.core.designsystem.component.MinGraviditetNavigationBar
    import dk.nsp.mingraviditet.core.designsystem.component.MinGraviditetNavigationBarItem
    import dk.nsp.mingraviditet.core.designsystem.extensions.bold
    import dk.nsp.mingraviditet.core.designsystem.theme.GravidTheme
    import dk.nsp.mingraviditet.feature.authentication.navigation.authenticateNavigationRoute
    import dk.nsp.mingraviditet.feature.authentication.onboarding.navigation.onboardingNavigationRoute
    import dk.nsp.mingraviditet.feature.authentication.signup.consentDialogRoute
    import dk.nsp.mingraviditet.feature.authentication.signup.signupNavigationRoute
    import dk.nsp.mingraviditet.feature.journal.navigation.fullScreenTableRoute
    import dk.nsp.mingraviditet.ui.main.navigation.GravidNavHost
    import dk.nsp.mingraviditet.ui.main.navigation.TopLevelDestination
    import dk.nsp.mingraviditet.ui.main.navigation.icon

    @Composable
    fun GravidApp(
        startDestination: String,
        appState: GravidAppState = rememberGravidAppState(),
        mainActivityViewModel: MainActivityViewModel = hiltViewModel(),
    ) {
        val appDestinations: State<List<TopLevelDestination>> =
            mainActivityViewModel.navDestinations.collectAsState()

        Surface(
            color = GravidTheme.colorScheme.bgNeutralPrimary,
            tonalElevation = 0.dp,
            modifier = Modifier.fillMaxSize(),
        ) {
            Scaffold(
                containerColor = Color.Transparent,
                contentColor = GravidTheme.colorScheme.textPrimary,
                contentWindowInsets = WindowInsets(
                    left = 0,
                    top = 0,
                    right = 0,
                    bottom = 0,
                ),
                bottomBar = {
                    val currentDestination = appState.currentDestination

                    val showBottomNavigation = when (currentDestination?.route) {
                        null, onboardingNavigationRoute, consentDialogRoute, signupNavigationRoute, authenticateNavigationRoute, fullScreenTableRoute -> false
                        else -> true
                    }
                    AnimatedVisibility(
                        visible = showBottomNavigation,
                        enter = slideInVertically { it },
                        exit = slideOutVertically { it },
                    ) {
                        MGBottomBar(
                            destinations = appDestinations.value,
                            onNavigateToDestination = { destination, selected ->
                                appState.navigateToTopLevelDestination(
                                    topLevelDestination = destination,
                                    restoreTabStack = !selected,
                                )
                            },
                            currentDestination = currentDestination,
                        )
                    }
                },
            ) { padding ->
                Box(
                    Modifier
                        .fillMaxSize()
                        .padding(padding)
                        .consumeWindowInsets(padding),
                ) {
                    GravidNavHost(
                        appState = appState,
                        startDestination = startDestination,
                    )
                }
            }
        }
    }

    private fun NavDestination?.isTopLevelDestinationInHierarchy(destination: TopLevelDestination) =
        this?.hierarchy?.any {
            it.route?.contains(destination.name, true) ?: false
        } ?: false

    @Composable
    fun MGBottomBar(
        destinations: List<TopLevelDestination>,
        onNavigateToDestination: (TopLevelDestination, selected: Boolean) -> Unit,
        currentDestination: NavDestination?,
        modifier: Modifier = Modifier,
    ) {
        MinGraviditetNavigationBar(modifier = modifier) {
            destinations.forEach { destination ->
                val isSelected: Boolean =
                    currentDestination.isTopLevelDestinationInHierarchy(destination)
                val textColor = if (isSelected) {
                    GravidTheme.colorScheme.textInteractivePrimary
                } else {
                    GravidTheme.colorScheme.textSecondary
                }
                MinGraviditetNavigationBarItem(
                    selected = isSelected,
                    onClick = { onNavigateToDestination(destination, isSelected) },
                    icon = {
                        Icon(
                            painterResource(id = destination.icon(isSelected)),
                            contentDescription = destination.name,
                        )
                    },
                    label = {
                        GravidText(
                            text = stringResource(id = destination.titleTextId),
                            style = GravidTheme.typography.textXxs.bold,
                            color = textColor,
                            maxLines = 1,
                            overflow = TextOverflow.Ellipsis,
                        )
                    },
                )
            }
        }
    }