package com.example.what_the_fantasy.ui.navigations

import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import com.example.what_the_fantasy.data.services.ServicesStub
import com.example.what_the_fantasy.ui.screens.AccueilPage
import com.example.what_the_fantasy.ui.screens.FavoritePage
import com.example.what_the_fantasy.ui.screens.LoginPage
import com.example.what_the_fantasy.ui.screens.ProfilPage
import com.example.what_the_fantasy.ui.screens.QuizEndPage
import com.example.what_the_fantasy.ui.screens.QuizMenu
import com.example.what_the_fantasy.ui.screens.QuizPage
import com.example.what_the_fantasy.ui.screens.QuotePage
import com.example.what_the_fantasy.ui.screens.SearchPage
import com.example.what_the_fantasy.ui.screens.SignUpPage
import com.example.what_the_fantasy.ui.screens.SubmitQuotePage
import kotlinx.serialization.Serializable

@Serializable
data class Accueil(val userIndex: Int)
@Serializable
data object Login
@Serializable
data class Favorite(val userIndex: Int)
@Serializable
data class Profil(val userIndex: Int)
@Serializable
data class  QuizMenu(val userIndex: Int)
@Serializable
data class Quiz(val userIndex : Int, val idQuiz : Int)
@Serializable
data class QuizEnd(val userIndex : Int, val idQuiz : Int,val pts : Int)
@Serializable
data class Quote(val quoteId: Int, val userIndex : Int)
@Serializable
data object Search
@Serializable
data object SignUp
@Serializable
data object SubmitQuote


sealed class Destination(val route: String) {
    data object Login : Destination("Login")
    data object Accueil : Destination("Accueil")
    data object Favorite : Destination("Favorite")
    data object Profil : Destination("Profil/{userIndex}") {  // Ajout du paramètre userIndex
        fun createRoute(userIndex: Int) = "Profil/$userIndex"  // Fonction pour créer la route avec l'index
    }
    data object Quiz : Destination("Quiz")
    data object Quote : Destination("Quote/{quoteId}"){
        fun createRoute(quoteId : Int) = "Profil/$quoteId"
    }
    data object Search : Destination("Search")
    data object SignUp : Destination("SignUp")
    data object SubmitQuote : Destination("SubmitQuote")
}

@Composable
fun AppNavigator() {
    val navController = rememberNavController()
    val services = ServicesStub()
    NavHost(navController, startDestination = Login) {
        composable<Login> {
            LoginPage(
                navControllerSignUp = { navController.navigate(SignUp) },
                navControllerProfil = { userIndex ->
                    navController.navigate(Profil(userIndex)) {
                        // Vider pile de navigation pour empêcher le retour à la page Login
                        popUpTo(Login) { inclusive = true }
                    }
                },
                services = services
            )
        }
        composable<Accueil> {
            val accueil: Accueil = it.toRoute()
            AccueilPage(
                index = accueil.userIndex,
                navFavorite = { navController.navigate(Favorite(accueil.userIndex)) },
                navQuiz =  { navController.navigate(QuizMenu(accueil.userIndex)) },
                navProfil = { navController.navigate(Profil(accueil.userIndex)) }
        ) }
        composable<Favorite> {
            val favorite: Favorite = it.toRoute()
            FavoritePage(
                index = favorite.userIndex,
                navAccueil ={ navController.navigate( Accueil(favorite.userIndex) ) },
                navQuiz =  { navController.navigate(QuizMenu(favorite.userIndex)) },
                navProfil = { navController.navigate(Profil(favorite.userIndex)) },
                navQuote = { quoteId -> navController.navigate(Quote(quoteId,favorite.userIndex))}
        ) }
        composable<Profil>{
            val profil:Profil = it.toRoute()
            ProfilPage(
                index = profil.userIndex,
                navFavorite = { navController.navigate(Favorite(profil.userIndex)) },
                navAccueil ={ navController.navigate( Accueil(profil.userIndex) ) },
                navQuiz =  { navController.navigate( QuizMenu(profil.userIndex)) },
                navUnLog = {
                    navController.navigate(Login) {
                        // Vider pile de navigation pour empêcher le retour à la page profil
                        popUpTo(profil) { inclusive = true }
                    }
                },
                services = services
            )
        }
        composable<Quote>{
            val quote:Quote = it.toRoute()
            QuotePage(
                quoteId = quote.quoteId,
                index = quote.userIndex,
                navAccueil ={ navController.navigate( Accueil(quote.userIndex) ) },
                navQuiz =  { navController.navigate(QuizMenu(quote.userIndex)) },
                navProfil = { navController.navigate(Profil(quote.userIndex)) },
                navFavorite = { navController.navigate(Favorite(quote.userIndex)) },
                service = services
            )
        }
        composable<Search> { SearchPage() }
        composable<SignUp> { SignUpPage(
            navControllerLogin = {
                navController.navigate(Login){
                    // Vider pile de navigation pour empêcher le retour à la page Sign up
                    popUpTo(Login) { inclusive = true }
                }
            },services) }
        composable<SubmitQuote> { SubmitQuotePage() }

        composable<QuizMenu> {
            val quizMenu:QuizMenu=it.toRoute()
            QuizMenu(
                index = quizMenu.userIndex,
                navAccueil = { navController.navigate( Accueil(quizMenu.userIndex) ) },
                navFavorite = { navController.navigate(Favorite(quizMenu.userIndex)) },
                navProfil = { navController.navigate(Profil(quizMenu.userIndex)) },
                navControllerQuiz = { idQuiz ->
                    navController.navigate(Quiz(quizMenu.userIndex,idQuiz))
                }
            )
        }
        composable<Quiz> {
            val quiz:Quiz=it.toRoute()
            QuizPage(
                index = quiz.userIndex,
                navAccueil = { navController.navigate( Accueil(quiz.userIndex) ) },
                navFavorite = { navController.navigate(Favorite(quiz.userIndex)) },
                navProfil = { navController.navigate(Profil(quiz.userIndex)) },
                navQuiz =  { navController.navigate(QuizMenu(quiz.userIndex)) },
                navControllerQuizEnd = { idQuiz, pts ->
                    navController.navigate(QuizEnd(quiz.userIndex,idQuiz, pts))
                },
                idQuiz =  quiz.idQuiz
            )
        }
        composable<QuizEnd> {
            val quizEnd: QuizEnd = it.toRoute()
            QuizEndPage(
                idQuiz = quizEnd.idQuiz,
                points = quizEnd.pts,
                index = quizEnd.userIndex,
                navAccueil = { navController.navigate( Accueil(quizEnd.userIndex) ) },
                navFavorite = { navController.navigate(Favorite(quizEnd.userIndex)) },
                navProfil = { navController.navigate(Profil(quizEnd.userIndex)) },
                navQuiz =  { navController.navigate(QuizMenu(quizEnd.userIndex)) }
            )
        }
    }
}
