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.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
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.viewModels.AuthUserViewModel
import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel
import com.example.what_the_fantasy.ui.viewModels.SignInUserViewModel
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 QuizRandom(val userIndex: Int)

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

@Serializable
data class Search(val userIndex: Int,val type : String = "contenue", val search: String = "")

@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()

    //ViewModel pour l'authentification
    val authUserVM : AuthUserViewModel = viewModel()
    val authState by authUserVM.userState.collectAsState()

    //ViewModel pour l'inscription
    val signInUserVM : SignInUserViewModel = viewModel()
    val signInState by signInUserVM.userState.collectAsState()

    //ViewModel pour l'utilisateur
    val currentUserVM : CurrentUserViewModel = viewModel()
    val currentUserState by currentUserVM.currentUserState.collectAsState()

    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 }
                            }
                        },
                        authUserVM = authUserVM,
                        authState = authState,
                        initialierCurrentUser ={currentUserVM.initialiseCurrentUser(it)}
                    )
                }

                composable<Accueil> {
                    //val accueil: Accueil = it.toRoute()
                    AccueilPage(
                        navFavorite = { navController.navigate(Favorite(currentUserState.id)) },
                        navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) },
                        navProfil = { navController.navigate(Profil(currentUserState.id)) },
                        navQuote = { quoteId ->
                            navController.navigate(
                                OneQuote(
                                    quoteId,
                                    currentUserState.id
                                )
                            )
                        },
                        navSearch = { navController.navigate(Search(currentUserState.id))},
                        services = services,
                        currentUserVM = currentUserVM,
                        currentUserState = currentUserState,
                    )
                }
                composable<Favorite> {
                    //val favorite: Favorite = it.toRoute()
                    FavoritePage(
                        navAccueil = { navController.navigate(Accueil(currentUserState.id)) },
                        navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) },
                        navProfil = { navController.navigate(Profil(currentUserState.id)) },
                        navQuote = { quoteId ->
                            navController.navigate(
                                OneQuote(
                                    quoteId,
                                    currentUserState.id
                                )
                            )
                        },
                        navSearch = { navController.navigate(Search(currentUserState.id))},
                        services = services,
                        currentUserVM = currentUserVM,
                        currentUserState = currentUserState,
                    )
                }
                composable<Profil> {
                    val profil: Profil = it.toRoute()
                    ProfilPage(
                        navFavorite = { navController.navigate(Favorite(currentUserState.id)) },
                        navAccueil = { navController.navigate(Accueil(currentUserState.id)) },
                        navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) },
                        navSubmitQuote = { navController.navigate(SubmitQuote(currentUserState.id)) },
                        navUnLog = {
                            navController.navigate(Login) {
                                popUpTo(profil) { inclusive = true }
                            }
                        },
                        currentUserVM  = currentUserVM,
                        currentUserState = currentUserState,
                        navSearch = { navController.navigate(Search(profil.userIndex))},
                    )
                }
                composable<OneQuote> {
                    val quote: OneQuote = it.toRoute()
                    QuotePage(
                        quoteId = quote.quoteId,
                        navAccueil = { navController.navigate(Accueil(currentUserState.id)) },
                        navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) },
                        navProfil = { navController.navigate(Profil(currentUserState.id)) },
                        navFavorite = { navController.navigate(Favorite(currentUserState.id)) },
                        service = services,
                        currentUserVM  = currentUserVM,
                        currentUserState = currentUserState,
                        navSearch = { navController.navigate(Search(currentUserState.id))}
                    )
                }
                composable<Search> {
                    val search: Search = it.toRoute()
                    SearchPage(
                        navAccueil = { navController.navigate(Accueil(search.userIndex)) },
                        navFavorite = { navController.navigate(Favorite(search.userIndex)) },
                        navQuiz = { navController.navigate(QuizMenu(search.userIndex)) },
                        navProfil = { navController.navigate(Profil(search.userIndex)) },
                        navQuote = {  quoteId -> navController.navigate(OneQuote(quoteId,search.userIndex)) },
                        navSearch = {type,newSearch -> navController.navigate(Search(search.userIndex,type,newSearch))},
                        services = services,
                        type = search.type,
                        search = search.search,
                        currentUserVM = currentUserVM,
                        currentUserState = currentUserState,
                        )
                }
                composable<SignUp> {
                    SignUpPage(
                        navControllerLogin = {
                            navController.navigate(Login) {
                                popUpTo(Login) { inclusive = true }
                            }
                        },
                        signInUserVM = signInUserVM,
                        signInState = signInState
                    )
                }
                composable<SubmitQuote> {
                    //val submitQuote: SubmitQuote = it.toRoute()
                    SubmitQuotePage(
                        navAccueil = { navController.navigate(Accueil(currentUserState.id)) },
                        navFavorite = { navController.navigate(Favorite(currentUserState.id)) },
                        navProfil = { navController.navigate(Profil(currentUserState.id)) },
                        navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) },
                        navRecap = { quoteContent, character, source ->
                            navController.navigate(
                                RecapSubmit(
                                    currentUserState.id,
                                    quoteContent,
                                    character,
                                    source
                                )
                            )
                        },
                        currentUserVM  = currentUserVM,
                        currentUserState = currentUserState,
                    )
                }
                composable<RecapSubmit> {
                    val recapSubmit: RecapSubmit = it.toRoute()
                    RecapSubmitPage(
                        quoteContent = recapSubmit.quoteContent,
                        character = recapSubmit.character,
                        source = recapSubmit.source,
                        navAccueil = { navController.navigate(Accueil(currentUserState.id)) },
                        navFavorite = { navController.navigate(Favorite(currentUserState.id)) },
                        navProfil = { navController.navigate(Profil(currentUserState.id)) },
                        navSearch = { navController.navigate(Search(currentUserState.id))},
                        navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) },
                        currentUserVM  = currentUserVM,
                        currentUserState = currentUserState,
                    )
                }

                composable<QuizMenu> {
                    //val quizMenu: QuizMenu = it.toRoute()
                    QuizMenu(
                        currentUserVM = currentUserVM,
                        currentUserState = currentUserState,
                        navAccueil = { navController.navigate(Accueil(currentUserState.id)) },
                        navFavorite = { navController.navigate(Favorite(currentUserState.id)) },
                        navProfil = { navController.navigate(Profil(currentUserState.id)) },
                        navSearch = { navController.navigate(Search(currentUserState.id))},
                        navControllerQuiz = { idQuiz ->
                            navController.navigate(Quiz(currentUserState.id, idQuiz))
                        },
                        navControllerRandomQuiz = { navController.navigate(QuizRandom(currentUserState.id)) }

                    )
                }
                composable<Quiz> {
                    val quiz: Quiz = it.toRoute()
                    QuizPage(
                        navAccueil = { navController.navigate(Accueil(currentUserState.id)) },
                        navFavorite = { navController.navigate(Favorite(currentUserState.id)) },
                        navProfil = { navController.navigate(Profil(currentUserState.id)) },
                        navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) },
                        navControllerQuizEnd = { idQuiz, pts ->
                            navController.navigate(QuizEnd(currentUserState.id, idQuiz, pts))
                        },
                        navSearch = { navController.navigate(Search(currentUserState.id))},
                        idQuiz = quiz.idQuiz,
                        currentUserVM  = currentUserVM,
                        currentUserState = currentUserState,
                    )
                }
                composable<QuizEnd> {
                    val quizEnd: QuizEnd = it.toRoute()
                    QuizEndPage(
                        idQuiz = quizEnd.idQuiz,
                        points = quizEnd.pts,
                        navAccueil = { navController.navigate(Accueil(currentUserState.id)) },
                        navFavorite = { navController.navigate(Favorite(currentUserState.id)) },
                        navProfil = { navController.navigate(Profil(currentUserState.id)) },
                        navQuiz = { navController.navigate(QuizMenu(currentUserState.id)) },
                        navSearch = { navController.navigate(Search(currentUserState.id))},
                        currentUserVM  = currentUserVM,
                        currentUserState = currentUserState,
                    )
                }

                composable<QuizRandom> {
                    val quizRandom: QuizRandom = it.toRoute()
                    QuizRandom(
                        navAccueil = { navController.navigate(Accueil(quizRandom.userIndex)) },
                        navFavorite = { navController.navigate(Favorite(quizRandom.userIndex)) },
                        navProfil = { navController.navigate(Profil(quizRandom.userIndex)) },
                        navQuiz = { navController.navigate(QuizMenu(quizRandom.userIndex)) },
                        navSearch = { navController.navigate(Search(quizRandom.userIndex)) },
                        navControllerQuizEnd = { idQuiz, pts ->
                            navController.navigate(QuizEnd(quizRandom.userIndex, idQuiz, pts))
                        },
                        currentUserVM  = currentUserVM,
                        currentUserState = currentUserState,
                    )
                }
            }
        }
    }
}

