package com.example.what_the_fantasy.ui.navigations

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
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.*
import com.example.what_the_fantasy.ui.theme.colorNavBar
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 OneQuote(val quoteId: Int, val userIndex: Int)

@Serializable
data class Quote(val quoteId: Int, val userIndex: Int)

@Serializable
data object Search

@Serializable
data object SignUp

@Serializable
data class SubmitQuote(val userIndex: Int)

@Serializable
data class RecapSubmit(val userIndex: Int,
                       val quoteContent : String,
                       val character : String,
                        val source : String)


@Composable
fun AppNavigator() {
    val navController = rememberNavController()
    val services = ServicesStub()

    Scaffold(
        modifier = Modifier.fillMaxSize(),
        containerColor = MaterialTheme.colorScheme.onPrimary
    ) { paddingValues ->
        Box(modifier = Modifier.padding(paddingValues)) {
            NavHost(navController, startDestination = Login) {
                composable<Login> {
                    LoginPage(
                        navControllerSignUp = { navController.navigate(SignUp) },
                        navControllerProfil = { userIndex ->
                            navController.navigate(Profil(userIndex)) {
                                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)) },
                        navQuote = { quoteId ->
                            navController.navigate(
                                OneQuote(
                                    quoteId,
                                    accueil.userIndex
                                )
                            )
                        },
                        services = services
                    )
                }
                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(
                                OneQuote(
                                    quoteId,
                                    favorite.userIndex
                                )
                            )
                        },
                        services = services
                    )
                }
                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)) },
                        navSubmitQuote = { navController.navigate(SubmitQuote(profil.userIndex)) },
                        navUnLog = {
                            navController.navigate(Login) {
                                popUpTo(profil) { inclusive = true }
                            }
                        },
                        services = services
                    )
                }
                composable<OneQuote> {
                    val quote: OneQuote = 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) {
                                popUpTo(Login) { inclusive = true }
                            }
                        },
                        services = services
                    )
                }
                composable<SubmitQuote> {
                    val submitQuote: SubmitQuote = it.toRoute()
                    SubmitQuotePage(
                        index = submitQuote.userIndex,
                        navAccueil = { navController.navigate(Accueil(submitQuote.userIndex)) },
                        navFavorite = { navController.navigate(Favorite(submitQuote.userIndex)) },
                        navProfil = { navController.navigate(Profil(submitQuote.userIndex)) },
                        navControllerQuiz = { idQuiz ->
                            navController.navigate(Quiz(submitQuote.userIndex, idQuiz))
                        },
                        navRecap = { quoteContent, character, source ->
                            navController.navigate(
                                RecapSubmit(
                                    submitQuote.userIndex,
                                    quoteContent,
                                    character,
                                    source
                                )
                            )
                        }
                    )
                }
                composable<RecapSubmit> {
                    val recapSubmit: RecapSubmit = it.toRoute()
                    RecapSubmitPage(
                        index = recapSubmit.userIndex,
                        quoteContent = recapSubmit.quoteContent,
                        character = recapSubmit.character,
                        source = recapSubmit.source,
                        navAccueil = { navController.navigate(Accueil(recapSubmit.userIndex)) },
                        navFavorite = { navController.navigate(Favorite(recapSubmit.userIndex)) },
                        navProfil = { navController.navigate(Profil(recapSubmit.userIndex)) }
                    )
                }

                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)) }
                    )
                }
            }
        }
    }
}

