<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WCST Human Experiment</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        :root {
            --primary-color: #3498db;
            --secondary-color: #2ecc71;
            --background-color: #f4f4f4;
            --text-color: #333;
            --correct-color: #2ecc71;
            --incorrect-color: #e74c3c;
        }

        body {
            font-family: 'Arial', sans-serif;
            background-color: var(--background-color);
            color: var(--text-color);
            line-height: 1.4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            padding: 1.5rem;
            max-width: 800px;
            width: 90%;
        }

        h1 {
            color: var(--primary-color);
            text-align: center;
            margin-bottom: 1rem;
            font-size: 1.8em;
        }

        h2 {
            font-size: 1.2em;
            margin-top: 0.5rem;
            margin-bottom: 1rem;
        }

        #setup-form, #experiment-container {
            display: flex;
            flex-direction: column;
            gap: 1rem;
        }

        label {
            font-weight: bold;
            font-size: 0.9em;
        }

        input[type="number"] {
            padding: 0.4rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 0.9rem;
        }

        button {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 0.6rem 1.2rem;
            font-size: 0.9rem;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #2980b9;
        }

        button:disabled {
            background-color: #bdc3c7;
            cursor: not-allowed;
        }

        #image-container {
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 250px;
            background-color: #f9f9f9;
            border-radius: 8px;
            overflow: hidden;
            margin-bottom: 0.8rem;
        }

        #stimulus-image {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain;
        }

        #feedback {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
            font-weight: bold;
            font-size: 1.4rem;
            padding: 0.8rem 1.2rem;
            border-radius: 8px;
            transition: all 0.3s ease;
            opacity: 0;
            pointer-events: none;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            min-width: 120px;
        }

        #feedback.show {
            opacity: 1;
        }

        #feedback.correct {
            background-color: var(--correct-color);
            color: white;
        }

        #feedback.incorrect {
            background-color: var(--incorrect-color);
            color: white;
        }

        #response-buttons {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 1rem;
            margin-top: 0.8rem;
        }

        #response-buttons button {
            background-color: var(--secondary-color);
        }

        #response-buttons button:hover:not(:disabled) {
            background-color: #27ae60;
        }

        .fade-in {
            animation: fadeIn 0.5s ease-in;
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>WCST Human Experiment</h1>
        <div id="setup-form">
            <label for="subject-number">Subject Number (Participant ID):</label>
            <input type="number" id="subject-number" value="{{ next_subject }}" required>
            <label for="session-number">Session Number (Dataset Type, 1-20):</label>
            <input type="number" id="session-number" value="{{ next_session }}" min="1" max="20" required>
            <button onclick="startExperiment()">Start Experiment</button>
        </div>
        <div id="experiment-container" style="display: none;">
            <h2>Trial <span id="trial-number"></span> / <span id="total-trials"></span></h2>
            <div id="image-container">
                <img id="stimulus-image" src="" alt="Stimulus">
                <div id="feedback"></div>
            </div>
            <p id="prompt"></p>
            <div id="response-buttons">
                <button onclick="submitResponse(1)">1</button>
                <button onclick="submitResponse(2)">2</button>
                <button onclick="submitResponse(3)">3</button>
                <button onclick="submitResponse(4)">4</button>
            </div>
        </div>
    </div>

    <script>
        let sessionNumber;
        let canRespond = false;

        function startExperiment() {
            const subjectNumber = $('#subject-number').val();
            const sessionNumber = $('#session-number').val();

            if (sessionNumber < 1 || sessionNumber > 20) {
                alert("Session number must be between 1 and 20");
                return;
            }

            $.post('/', { subject_number: subjectNumber, session_number: sessionNumber }, function(data) {
                $('#setup-form').hide();
                $('#experiment-container').show().addClass('fade-in');
                updateTrial(data);
                // 更新显示的 subject 和 session 编号
                $('#subject-number').val(data.subject_number);
                $('#session-number').val(data.session_number);
                enableResponseButtons();
                canRespond = true;
            });
        }

        function submitResponse(response) {
            if (!canRespond) return;

            canRespond = false;
            disableResponseButtons();

            $.ajax({
                url: '/submit_response',
                method: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({ response: response, session_number: sessionNumber }),
                success: function(data) {
                    if (data.complete) {
                        $('#experiment-container').html('<h2>实验结束</h2><p>感谢您的参与!</p>');
                    } else {
                        showFeedback(data.feedback);
                        setTimeout(function() {
                            $('#feedback').removeClass('show correct incorrect');
                            updateTrial(data);
                        }, 500);
                    }
                }
            });
        }

        function updateTrial(data) {
            $('#trial-number').text(data.trial_number);
            $('#total-trials').text(data.total_trials);
            $('#stimulus-image').attr('src', data.image_path).addClass('fade-in');
            $('#prompt').text(data.prompt).addClass('fade-in');
            enableResponseButtons();
            canRespond = true;
        }

        function showFeedback(feedback) {
            const feedbackElement = $('#feedback');
            feedbackElement.text(feedback);
            if (feedback.toLowerCase().includes('correct')) {
                feedbackElement.addClass('correct').removeClass('incorrect');
            } else {
                feedbackElement.addClass('incorrect').removeClass('correct');
            }
            feedbackElement.addClass('show');
        }

        function enableResponseButtons() {
            $('#response-buttons button').prop('disabled', false);
        }

        function disableResponseButtons() {
            $('#response-buttons button').prop('disabled', true);
        }
    </script>
</body>
</html>