let currentQuestionIndex = 0;
let score = 0;
let currentDifficulty = "easy";
let questions = {};

function startGame(difficulty) {
    currentDifficulty = difficulty;
    currentQuestionIndex = 0;
    score = 0;
    document.getElementById("game-intro").style.display = "none";
    document.getElementById("game-area").style.display = "block";
    loadQuestion();
}

function loadQuestionsFromCSV(csvFilePath, callback) {
    fetch(csvFilePath)
        .then(response => response.text())
        .then(data => {
            const parsedQuestions = parseCSV(data);
            questions = groupQuestionsByDifficulty(parsedQuestions);
            callback();
        })
        .catch(error => console.error("Error loading CSV file:", error));
}

function parseCSV(data) {
    const rows = data.split("\n");
    return rows.map(row => {
        const [difficulty, question, options, correctAnswer, explanation] = row.split(",");
        return {
            difficulty,
            question,
            options: options.split("|"),
            correctAnswer,
            explanation
        };
    });
}

function groupQuestionsByDifficulty(parsedQuestions) {
    return parsedQuestions.reduce((acc, question) => {
        const { difficulty } = question;
        if (!acc[difficulty]) {
            acc[difficulty] = [];
        }
        acc[difficulty].push(question);
        return acc;
    }, {});
}

function loadQuestion() {
    const questionData = questions[currentDifficulty][currentQuestionIndex];
    document.getElementById("question").innerText = questionData.question;
    const optionsContainer = document.getElementById("options");
    optionsContainer.innerHTML = "";

    questionData.options.forEach(option => {
        const button = document.createElement("button");
        button.innerText = option;
        button.onclick = () => checkAnswer(option);
        optionsContainer.appendChild(button);
    });

    document.getElementById("feedback").innerText = "";
    document.getElementById("next-button").style.display = "none";
}

function checkAnswer(selectedOption) {
    const questionData = questions[currentDifficulty][currentQuestionIndex];
    const feedback = document.getElementById("feedback");

    if (selectedOption === questionData.correctAnswer) {
        feedback.innerText = "Correct! " + questionData.explanation;
        feedback.style.color = "green";
        score++;
    } else {
        feedback.innerText = "Incorrect. " + questionData.explanation;
        feedback.style.color = "red";
    }

    document.getElementById("next-button").style.display = "block";
}

function nextQuestion() {
    currentQuestionIndex++;
    if (currentQuestionIndex < questions[currentDifficulty].length) {
        loadQuestion();
    } else {
        endGame();
    }
}

function endGame() {
    document.getElementById("game-area").style.display = "none";
    document.getElementById("game-over").style.display = "block";
    document.getElementById("final-score").innerText = score;
}

function restartGame() {
    document.getElementById("game-over").style.display = "none";
    document.getElementById("game-intro").style.display = "block";
}

loadQuestionsFromCSV("questions.csv", () => {
    console.log("Questions loaded successfully.");
});
