/** * Quiz Runtime — controla o fluxo de quiz/simulado na página dedicada */ const PROGRESS_PREFIX = window.PROGRESS_PREFIX = 'quiz_progress_'; // Estado global do quiz (página de quiz dedicada) let _qState = { questions: [], qIndex: 0, acertos: 0, erros: 0, answered: false, flipped: false, }; function nextQuestion() { _qState.qIndex++; _qState.answered = false; _qState.flipped = false; if (_qState.qIndex >= _qState.questions.length) { showResult(); return; } const body = document.getElementById('quiz-body'); if (!body) return; body.classList.add('fade-out'); setTimeout(() => { body.classList.remove('fade-out'); _renderQuestion(_qState.questions[_qState.qIndex]); _saveProgress(window.QUIZ_KEY); }, 100); } function showResult() { const wrap = document.getElementById('quiz-wrap'); const result = document.getElementById('quiz-result'); if (wrap) wrap.style.display = 'none'; if (result) result.style.display = ''; _buildResult(); _clearProgress(window.QUIZ_KEY); window.scrollTo({ top: 0, behavior: 'smooth' }); } function restartQuiz() { _clearProgress(window.QUIZ_KEY); _qState.questions = prepareQuizQuestions(parseQuizBody( typeof RAW_QUIZ !== 'undefined' ? RAW_QUIZ : '' )); _qState.qIndex = 0; _qState.acertos = 0; _qState.erros = 0; _qState.answered = false; _qState.flipped = false; const wrap = document.getElementById('quiz-wrap'); const result = document.getElementById('quiz-result'); if (result) result.style.display = 'none'; if (wrap) wrap.style.display = ''; _renderQuestion(_qState.questions[0]); } function initQuiz() { // Só inicializa na página de quiz (onde existe #quiz-body) if (!document.getElementById('quiz-body')) return; const raw = typeof RAW_QUIZ !== 'undefined' ? RAW_QUIZ : ''; const key = window.QUIZ_KEY; if (!_loadProgress(key)) { _qState.questions = prepareQuizQuestions(parseQuizBody(raw)); _qState.qIndex = 0; _qState.acertos = 0; _qState.erros = 0; } _qState.answered = false; _qState.flipped = false; const wrap = document.getElementById('quiz-wrap'); const result = document.getElementById('quiz-result'); if (result) result.style.display = 'none'; if (wrap) wrap.style.display = ''; if (_qState.qIndex >= _qState.questions.length) { showResult(); return; } _renderQuestion(_qState.questions[_qState.qIndex]); } document.addEventListener('DOMContentLoaded', initQuiz);