package com.isidroid.core.ext

import androidx.navigation.NavController
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.onEach
import timber.log.Timber

fun NavController.navigateSingleTopTo(
    route: String,
    isSaveState: Boolean? = null,
    isLaunchSingleTop: Boolean = true,
    isRestoreState: Boolean? = null,
    isInclusive: Boolean = true
) {
    navigate(route) {
        popUpTo(graph.findStartDestination().id) {
            inclusive = isInclusive
            if (isSaveState != null)
                saveState = isSaveState
        }

        isLaunchSingleTop?.also { launchSingleTop = it }
        isRestoreState?.also { restoreState = it }
    }
}


/**
 * Just for printing route paths. Need to be used in LaunchedEffect as follows
 *
 *  LaunchedEffect(Unit) { navController.printCurrentDestination() }
 */
suspend fun NavHostController?.printCurrentDestination(
    tag: String = "navigation_graph",
    printFullDebug: Boolean = false
) {
    this ?: return

    visibleEntries.onEach { entry ->
        val message = entry.mapNotNull {
            val route = it.destination.route

            if (route == null)
                null
            else
                buildString {
                    append(route.let { s ->
                        if (printFullDebug)
                            s
                        else {
                            val index = s.indexOf("?")
                            if (index > 0) s.take(index) else s
                        }
                    })

//                    if (it.arguments != null && it.arguments?.isEmpty != true)
//                        append(": ")
//                            .append("arguments=${it.arguments.debug}")
                }
        }


        Timber.tag(tag).i("$message")
    }.collect()
}


