package com.fastest.pass.login.utils

import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.navigation.navOptions
import com.fastest.pass.R
import com.fastest.pass.login.presentation.ui.LoginFragment
import kotlin.math.log

class LoginNavigation {

    fun navigate(loginFragment: LoginFragment) {
        loginFragment.lifecycleScope.launchWhenStarted {
            loginFragment.viewmodel.router.collect { router ->
                when (router) {
                    LoginRoute.OpenSignUp -> {
                        loginFragment.findNavController().navigate(R.id.signupFragment)
                    }
                    LoginRoute.OpenForgotPassword -> {
                        loginFragment.findNavController().navigate(R.id.forgotPasswordFragment)
                    }
                    LoginRoute.OpenNoneScreen -> {}
                    LoginRoute.GoBack -> {
                        loginFragment.findNavController().popBackStack(
                            destinationId = R.id.welcomeFragment,
                            inclusive = false
                        )
                    }
                    LoginRoute.OpenMasterLoginScreen -> {
                        loginFragment.findNavController().navigate(R.id.masterLoginFragment)
                    }
                    LoginRoute.OpenDashBoardScreen -> {
                        loginFragment.findNavController().navigate(
                            R.id.dashboardFragment,
                            null,
                            navOptions {
                                popUpTo(loginFragment.findNavController().graph.id) {
                                    inclusive = true
                                }
                                launchSingleTop = true
                            }
                        )
                    }
                }

                loginFragment.viewmodel.navigateTo(LoginRoute.OpenNoneScreen)
            }
        }
    }
}