package com.example.what_the_fantasy.ui.screens

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
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

@Composable
fun QuizMenu(
    currentUserVM : CurrentUserViewModel,
    currentUserState : CurrentUserState,
    navFavorite: () -> Unit,
    navAccueil: () -> Unit,
    navProfil:() -> Unit,
    navControllerQuiz: (Int) -> Unit,
    navSearch: () -> Unit,
    navControllerRandomQuiz:() -> Unit

) {
    NavBar(onQuiz = true,
        currentUserVM = currentUserVM,
        currentUserState = currentUserState,
        navControllerFavorite = navFavorite,
        navControllerAccueil = navAccueil,
        navControllerProfil = navProfil,
        navControllerSearch = navSearch
    ) {

        Row(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colorScheme.background)
                .horizontalScroll(state = rememberScrollState())
        ) {
            // Contenu princiapl
            Column(
                modifier = Modifier
                    .width(400.dp)
                    .fillMaxSize()
                    .padding(20.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = "▶ Menu des Quiz ◀",
                    color = MaterialTheme.colorScheme.onBackground,
                    style = TextStyle(
                        fontSize = 25.sp,
                        fontWeight = FontWeight.Bold,
                        textAlign = TextAlign.Center
                    )
                )
                Spacer(Modifier.height(20.dp))
                Column(
                    modifier = Modifier
                        .background(MaterialTheme.colorScheme.primary, shape = RoundedCornerShape(20.dp))
                        .fillMaxSize()
                        .padding(vertical = 30.dp)
                        .verticalScroll(rememberScrollState()),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    val quizzes = QuizStub.allQuizzes.chunked(2)
                    val context = LocalContext.current
                    for (rowQuizzes in quizzes) {
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.SpaceEvenly
                        ) {
                            for (quiz in rowQuizzes) {
                                val imageResId = context.resources.getIdentifier(
                                    quiz.img,
                                    "drawable",
                                    context.packageName
                                )
                                Column(
                                    modifier = Modifier
                                        .size(width = 150.dp, height = 145.dp)
                                        .clickable { navControllerQuiz(quiz.id) },
                                ) {
                                    Image(
                                        painter = painterResource(id = imageResId),
                                        contentDescription = quiz.name,
                                        modifier = Modifier
                                            .size(width = 150.dp, height = 100.dp)
                                            .clip(shape = RoundedCornerShape(20.dp)),
                                        contentScale = ContentScale.Crop
                                    )
                                    Spacer(Modifier.height(10.dp))
                                    Text(
                                        text = quiz.name,
                                        style = TextStyle(
                                            fontSize = 17.sp,
                                            fontWeight = FontWeight.Medium,
                                            textAlign = TextAlign.Center,
                                            color = MaterialTheme.colorScheme.onPrimary
                                        )
                                    )
                                }
                            }
                        }
                        Spacer(Modifier.height(30.dp))
                    }
                }
            }

            Column(
                modifier = Modifier
                    .width(400.dp)
                    .fillMaxSize()
                    .padding(20.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = "▶ Mini-Jeux ◀",
                    color = MaterialTheme.colorScheme.onBackground,
                    style = TextStyle(
                        fontSize = 25.sp,
                        fontWeight = FontWeight.Bold,
                        textAlign = TextAlign.Center
                    )
                )
                Spacer(Modifier.height(20.dp))
                Column(
                    modifier = Modifier
                        .background(MaterialTheme.colorScheme.onPrimary, shape = RoundedCornerShape(20.dp))
                        .fillMaxSize()
                        .padding(vertical = 30.dp)
                        .verticalScroll(rememberScrollState()),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    val context = LocalContext.current
                    val imageResId = context.resources.getIdentifier(
                        "quiz",
                        "drawable",
                        context.packageName
                    )
                    Column(
                        modifier = Modifier
                            .size(width = 150.dp, height = 145.dp)
                            .clickable { navControllerRandomQuiz() },
                    ) {
                        Image(
                            painter = painterResource(id = imageResId),
                            contentDescription = "Random Quiz",
                            modifier = Modifier
                                .size(width = 150.dp, height = 100.dp)
                                .clip(shape = RoundedCornerShape(20.dp)),
                            contentScale = ContentScale.Crop
                        )
                        Spacer(Modifier.height(10.dp))
                        Text(
                            text = "Random Quiz",
                            style = TextStyle(
                                fontSize = 17.sp,
                                fontWeight = FontWeight.Medium,
                                textAlign = TextAlign.Center,
                                color = MaterialTheme.colorScheme.primary
                            )
                        )
                    }
                }
            }
        }


    }
}