<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quizizz Answer</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 20px;
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        label {
            font-size: 16px;
            margin-bottom: 10px;
            display: block;
        }
        input[type="text"] {
            width: 100%;
            padding: 12px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
            margin-bottom: 20px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            font-size: 16px;
            padding: 12px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
        }
        button:hover {
            background-color: #45a049;
        }
        .question {
            margin-bottom: 20px;
            padding: 15px;
            background-color: #f9f9f9;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        .question h3 {
            margin: 0;
            color: #333;
        }
        .answers {
            margin-top: 10px;
            color: #333;
        }
        .answers p {
            font-size: 16px;
            margin: 0;
        }
        .error {
            color: red;
            font-weight: bold;
            text-align: center;
        }

        /* Message Notification Style */
        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            background-color: #ff0000;
            color: white;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            display: none;
            align-items: center;
            font-size: 16px;
            z-index: 999;
            min-width: 200px;
        }
        .notification .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #fff;
            border-radius: 50%;
            width: 20px;
            height: 20px;
            margin-right: 10px;
            animation: spin 2s linear infinite;
        }

        .notification .check {
            font-size: 18px;
            margin-right: 10px;
            color: #fff;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>

    <h1>Quizizz Answer</h1>

    <div class="container">
        <label for="quizLink">ใส่ลิ้งค์ห้อง Quizizz:</label>
        <input type="text" id="quizLink" placeholder="https://quizizz.com/join/game/U2FsdGVkX1%252FtCczVPbJ%252FpR%252ByutDpX6sbbUMWScqLR6XkmafOsxHrYrwbjuBOvNzM%252Bq%252Bm02YFaFJm9sr4GHaGcQ%253D%253D">
        <button onclick="fetchQuizData()">Fetch Answers</button>

        <div id="output"></div>
    </div>

    <!-- Notification -->
    <div id="notification" class="notification">
        <div class="spinner"></div>
        <span id="notification-message">ลิ้งไม่ถูกต้อง</span>
    </div>

    <script>
        async function fetchQuizData() {
            const quizLink = document.getElementById("quizLink").value;
            const notification = document.getElementById("notification");
            const notificationMessage = document.getElementById("notification-message");
            const notificationIcon = notification.querySelector('.spinner');

            if (!quizLink) {
                // Show notification for missing link
                notificationMessage.textContent = "คุณยังไม่ได้ใส่ลิ้ง"; // You haven't entered a link
                notification.style.display = 'flex';
                
                // Hide the notification after 2-3 seconds
                setTimeout(() => {
                    notification.style.display = 'none';
                }, 3000); // Notification will disappear after 3 seconds
                
                return;
            }

            try {
                const response = await fetch('https://v3.schoolcheats.net/quizizz/answers', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ input: quizLink })
                });

                if (!response.ok) {
                    throw new Error(`Error: ${response.status} - ${response.statusText}`);
                }

                const data = await response.json();
                const questions = data.questions;

                if (!questions || questions.length === 0) {
                    document.getElementById("output").innerHTML = "<p class='error'>No Room found.</p>";
                    return;
                }

                // Change the notification to success (Green background and Check icon)
                notification.style.backgroundColor = '#4CAF50'; // Green background
                notificationIcon.classList.remove('spinner');
                notificationIcon.classList.add('check');
                notificationIcon.textContent = '✔';

                let output = '';
                questions.forEach((question, idx) => {
                    const queryText = cleanText(question.structure.query.text);
                    const options = question.structure.options.map(opt => cleanText(opt.text));
                    const correctIndex = question.structure.answer;
                    const correctAnswer = correctIndex !== null && correctIndex < options.length ? options[correctIndex] : "No correct answer";

                    output += `
                        <div class="question">
                            <h3>${idx + 1}: ${queryText}</h3>
                            <div class="answers">
                                <p><strong>คำตอบ :</strong> ${correctAnswer}</p>
                            </div>
                        </div>
                    `;
                });

                document.getElementById("output").innerHTML = output;

            } catch (error) {
                document.getElementById("output").innerHTML = `<p class='error'>Error: ${error.message}</p>`;
            } finally {
                // Hide the notification after fetching is complete
                setTimeout(() => {
                    notification.style.display = 'none';
                }, 2000); // Hide after 2 seconds
            }
        }

        function cleanText(text) {
            const div = document.createElement("div");
            div.innerHTML = text;
            text = div.textContent || div.innerText || '';
            text = text.replace(/<.*?>/g, ''); // remove HTML tags
            return text.trim();
        }
    </script>

</body>
</html>
