package com.fastest.pass.masterlogin.utils

import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.navigation.navOptions
import com.fastest.pass.R
import com.fastest.pass.masterlogin.presentation.ui.MasterLoginFragment

class MasterLoginNavigation {

    fun navigate(masterLoginFragment: MasterLoginFragment) {
        masterLoginFragment.lifecycleScope.launchWhenStarted {
            masterLoginFragment.viewmodel.router.collect { router ->
                when (router) {
                    MasterLoginRoute.OpenNoneScreen -> {}

                    MasterLoginRoute.OpenDashBoardScreen -> {

                        masterLoginFragment.findNavController().navigate(
                            R.id.dashboardFragment,
                            null,
                            navOptions {
                                // Clear the entire back stack before navigating to DashboardFragment
                                popUpTo(masterLoginFragment.findNavController().graph.id) {
                                    inclusive = true // This will pop all fragments in the stack
                                }
                                launchSingleTop = true // Ensures DashboardFragment is the only one on the back stack
                            }
                        )
                    }
                    MasterLoginRoute.GoBackToLoginScreen -> {
                        masterLoginFragment.findNavController().popBackStack()
                    }
                }

                masterLoginFragment.viewmodel.navigateTo(MasterLoginRoute.OpenNoneScreen)
            }
        }
    }
}