package com.fastest.pass.signup.utils

import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.navigation.navOptions
import com.fastest.pass.R
import com.fastest.pass.signup.presentation.ui.SignUpFragment

class SignUpNavigation {

    fun navigate(signUpFragment: SignUpFragment) {
        signUpFragment.lifecycleScope.launchWhenStarted {
            signUpFragment.viewmodel.router.collect { router ->
                when (router) {
                    SignUpRoute.GoBack -> {
                        signUpFragment.findNavController().popBackStack()
                    }
                    SignUpRoute.OpenNoneScreen -> {}
                    SignUpRoute.OpenMasterSignupScreen -> {
                        signUpFragment.findNavController().navigate(R.id.masterSignupFragment)
                    }
                    SignUpRoute.OpenLoginScreen -> {
                        signUpFragment.findNavController().navigate(R.id.loginFragment)
                    }
                    SignUpRoute.OpenDashBoardScreen -> {
                        signUpFragment.findNavController().navigate(
                            R.id.dashboardFragment,
                            null,
                            navOptions {
                                popUpTo(signUpFragment.findNavController().graph.id) {
                                    inclusive = true
                                }
                                launchSingleTop = true
                            }
                        )
                    }
                }

                signUpFragment.viewmodel.navigateTo(SignUpRoute.OpenNoneScreen)
            }
        }
    }
}