package com.example.what_the_fantasy.ui.screens

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
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.QuestionStub
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

@Composable
fun QuizRandom(
    navFavorite: () -> Unit,
    navAccueil: () -> Unit,
    navProfil:() -> Unit,
    navQuiz: () -> Unit,
    navSearch: () -> Unit,
    navControllerQuizEnd: (Int, Int) -> Unit,
    currentUserVM : CurrentUserViewModel,
    currentUserState : CurrentUserState,
) {
    val questions = QuestionStub.shuffleRandomQuestions
    var idCurrentQuestion by remember { mutableIntStateOf(0) }
    var pts by remember { mutableIntStateOf(0) }
    var lifes by remember { mutableIntStateOf(3) }

    val gradient = Brush.linearGradient(
        colors = listOf(MaterialTheme.colorScheme.onPrimary, MaterialTheme.colorScheme.onPrimary),
        start = Offset(0f, 1000f),
        end = Offset(1000f, 0f)
    )

    fun onAnswerSelected(answer: String) {
        val currentQuestion = questions[idCurrentQuestion]
        val correctAnswer = mapOf(
            "A" to currentQuestion.ansA,
            "B" to currentQuestion.ansB,
            "C" to currentQuestion.ansC,
            "D" to currentQuestion.ansD
        )[currentQuestion.correctAns]

        if (answer == correctAnswer) pts++
        else {
            lifes -= 1
            Log.d("Quiz Debug", "Lifes -1 :, $lifes")
        }
        if (idCurrentQuestion < questions.size - 1 && lifes > 0) idCurrentQuestion++
        else {
            navControllerQuizEnd(-1, pts)
            Log.d("Quiz Debug", "Game over lifes : $lifes")
        } // Retour menu
    }
    NavBar(
        navControllerFavorite = navFavorite,
        navControllerAccueil = navAccueil,
        navControllerProfil = navProfil,
        navControllerQuiz = navQuiz,
        navControllerSearch = navSearch,
        currentUserVM = currentUserVM,
        currentUserState = currentUserState,
    ){
        Column (
            modifier = Modifier.fillMaxSize().background(Color(0xFF100C1B))
        ) {
            // Contenu princiapl
            Column(
                modifier = Modifier
                    .weight(0.8f)
                    .fillMaxWidth()
                    .padding(horizontal = 50.dp, vertical = 20.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                val question = questions[idCurrentQuestion]

                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                ) {
                    if (questions != null) {
                        Text(
                            text = "▶ Random Quiz ◀",
                            color = Color.White,
                            style = TextStyle(
                                fontSize = 20.sp,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Center
                            )
                        )
                    }
                    Spacer(Modifier.height(20.dp))
                    Column(
                        modifier = Modifier
                            .background(brush = gradient, shape = RoundedCornerShape(20.dp))
                            .height(800.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Text(
                            "Question ${idCurrentQuestion + 1}",
                            color = Color.White,
                            fontSize = 18.sp,
                            modifier = Modifier
                                .padding(top = 20.dp)
                                .weight(0.1f),
                            style = TextStyle(
                                fontSize = 25.sp,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Center
                            )
                        )
                        Text(
                            question.question,
                            color = Color.White,
                            fontSize = 15.sp,
                            modifier = Modifier
                                .padding(horizontal = 25.dp)
                                .weight(0.1f),
                            textAlign = TextAlign.Center
                        )
                        Column(
                            modifier = Modifier
                                .weight(0.7f)
                                .fillMaxHeight()
                                .padding(vertical = 30.dp),
                            verticalArrangement = Arrangement.SpaceBetween

                        ) {
                            listOf(
                                question.ansA,
                                question.ansB,
                                question.ansC,
                                question.ansD
                            ).forEach { answer ->
                                Box(
                                    modifier = Modifier
                                        .width(220.dp)
                                        .height(50.dp)
                                        .background(Color.White, shape = RoundedCornerShape(16.dp))
                                        .clickable { onAnswerSelected(answer) }
                                        .padding(horizontal = 8.dp),
                                    contentAlignment = Alignment.Center
                                ) {
                                    Text(answer, color = Color.Black, fontSize = 18.sp)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}