package com.example.what_the_fantasy.ui.screens

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.what_the_fantasy.data.local.QuizStub
import com.example.what_the_fantasy.ui.components.NavBar
import com.example.what_the_fantasy.ui.states.CurrentUserState
import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel

val gradient = Brush.linearGradient(
    colors = listOf(Color(0xFF7B1FA2), Color(0xFF311B92)),
    start = Offset(0f, 1000f),
    end = Offset(1000f, 0f)
)

@Composable
fun QuizEndPage(
    idQuiz: Int,
    points: Int,
    navFavorite: () -> Unit,
    navAccueil: () -> Unit,
    navProfil:() -> Unit,
    navQuiz: () -> Unit,
    navSearch: () -> Unit,
    currentUserVM : CurrentUserViewModel,
    currentUserState : CurrentUserState,
) {
    NavBar(
        currentUserVM = currentUserVM,
        currentUserState = currentUserState,
        navControllerFavorite = navFavorite,
        navControllerAccueil = navAccueil,
        navControllerProfil = navProfil,
        navControllerQuiz = navQuiz,
        navControllerSearch = navSearch
    ) {
        Column(
            modifier = Modifier.fillMaxSize().background(Color(0xFF100C1B))
        ) {

            // Contenu principal
            Column(
                modifier = Modifier
                    .weight(0.8f)
                    .padding(horizontal = 50.dp, vertical = 20.dp)
                    .fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = "▶ Résultats ◀",
                    color = Color.White,
                    style = TextStyle(
                        fontSize = 25.sp,
                        fontWeight = FontWeight.Bold,
                        textAlign = TextAlign.Center
                    )
                )
                Spacer(modifier = Modifier.height(16.dp))
                Column(
                    modifier = Modifier
                        .background(color = MaterialTheme.colorScheme.onPrimary, shape = RoundedCornerShape(20.dp))
                        .padding(30.dp)
                        .fillMaxSize(),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.SpaceEvenly
                ) {
                    val quizName = if (idQuiz == -1) "Random Quiz" else QuizStub.getQuizById(idQuiz)?.name ?: "Quiz Inconnu"

                    Text(
                        text = quizName,
                        color = Color.White,
                        style = TextStyle(
                            fontSize = 25.sp,
                            fontWeight = FontWeight.Bold,
                            textAlign = TextAlign.Center
                        )
                    )
                    val nbQuestions = 10
                    Text(
                        text = "Nombres de Questions : $nbQuestions",
                        color = Color.White,
                        style = TextStyle(
                            fontSize = 15.sp,
                            fontWeight = FontWeight.Bold,
                            textAlign = TextAlign.Center
                        )
                    )
                    Text(
                        text = "Nombres de bonnes réponses : $points",
                        color = Color.White,
                        style = TextStyle(
                            fontSize = 15.sp,
                            fontWeight = FontWeight.Bold,
                            textAlign = TextAlign.Center
                        )
                    )

                    val pourcentage = (points.toDouble() / nbQuestions!!) * 100

                    val note = when {
                        pourcentage == 100.0 -> "S"
                        pourcentage >= 70.0 -> "A"
                        pourcentage >= 40.0 -> "B"
                        else -> "C"
                    }

                    println("Note obtenue : $note")

                    Box(
                        contentAlignment = Alignment.Center,
                        modifier = Modifier
                            .size(100.dp)
                            .background(Color.White, shape = RoundedCornerShape(50.dp))
                    ) {
                        Text(
                            text = note,
                            color = Color.Red,
                            style = TextStyle(
                                fontSize = 40.sp,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Center
                            )
                        )
                    }
                }
            }
        }
    }
}
