package com.zaojiao.app.ui

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.core.os.trace
import androidx.navigation.NavDestination
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navOptions
import com.zaojiao.app.navigation.HomeDestination
import com.zaojiao.app.feat.home.navigation.homeCourse
import com.zaojiao.app.feat.home.navigation.homeIndex
import com.zaojiao.app.feat.home.navigation.homePersonal
import com.zaojiao.app.feat.home.navigation.navigateToHomeCourse
import com.zaojiao.app.feat.home.navigation.navigateToHomeIndex
import com.zaojiao.app.feat.home.navigation.navigateToHomePersonal
import kotlinx.coroutines.CoroutineScope

@Composable
fun rememberAppState(
    coroutineScope: CoroutineScope = rememberCoroutineScope(),
    navController: NavHostController = rememberNavController(),
): AppState {
    return remember(
        navController,
        coroutineScope,
    ) {
        AppState(
            navController,
            coroutineScope,
        )
    }
}

@Stable
class AppState(
    val navController: NavHostController,
    val coroutineScope: CoroutineScope,
) {
    val currentDestination: NavDestination?
        @Composable get() = navController
            .currentBackStackEntryAsState().value?.destination

    val currentHomeDestination: HomeDestination?
        @Composable get() = when (currentDestination?.route) {
            homeIndex -> HomeDestination.INDEX
            homeCourse -> HomeDestination.COURSE
            homePersonal -> HomeDestination.PERSONAL
            else -> null
        }

    val homeDestinations: List<HomeDestination> = HomeDestination.values().asList()


    fun navigateToHomeDestination(destination: HomeDestination) {
        trace("Navigation: ${destination.name}") {
            val topLevelNavOptions = navOptions {
                // Pop up to the start destination of the graph to
                // avoid building up a large stack of destinations
                // on the back stack as users select items
                popUpTo(navController.graph.findStartDestination().id) {
                    saveState = true
                }
                // Avoid multiple copies of the same destination when
                // reselecting the same item
                launchSingleTop = true
                // Restore state when reselecting a previously selected item
                restoreState = true
            }

            when (destination) {
                HomeDestination.INDEX -> navController.navigateToHomeIndex(topLevelNavOptions)
                HomeDestination.COURSE -> navController.navigateToHomeCourse(topLevelNavOptions)
                HomeDestination.PERSONAL -> navController.navigateToHomePersonal(topLevelNavOptions)
            }
        }
    }
}
