{% extends 'base.html' %}

{% block content %}
    <span id="user-id">
        {% if request.user.is_authenticated %}
            User: {{ request.user }}
        {% else %}
            CID: {{ cid }}
        {% endif %}
    </span>

    {% include "exam_clock.html" %}

    <div class="no-select">
        <h2>
            {{ exam }}: Question
            [<span id="question-number">{{ pos|add:1 }}</span>/<span id="exam-length">{{ exam_length }}</span>]
        </h2>

        {% if exam.publish_results %}
            <div class="alert alert-primary review-mode-alert" role="alert">
                Exam is in review mode. Add question feedback
                <a href="#" onclick="return window.create_popup_window('{% url "feedback_create" question_type="physics" pk=question.pk %}')">here</a>.
            </div>
        {% elif cid_user_exam.completed %}
            <div class="alert alert-primary review-mode-alert" role="alert">
                Exam completed.
            </div>
        {% endif %}

        <div>
            <p><span id="question-stem">{{ question.stem|safe }}</span></p>
        </div>
        

        <div id="question-fragment"
             hx-trigger="load"
             hx-swap="innerHTML"
             hx-get="{% if cid %}{% url 'physics:exam_take_fragment' pk=exam.pk sk=pos cid=cid passcode=passcode %}{% else %}{% url 'physics:exam_take_fragment_user' pk=exam.pk sk=pos %}{% endif %}">
            <!-- HTMX will load the question form + controls here -->
        </div>
    </div>

    <h4>Questions</h4>
    <div id="menu-list"></div>

    {% include "physics/exam_take_help.html" %}

{% endblock %}

{# HTMX fragment will load question-specific JS; nothing required here #}

{% block css %}
    <style type="text/css">
        /* Color variables and dark-mode support
           - Uses CSS variables for easy theming
           - Honors system preference via prefers-color-scheme
           - Also supports a manual `.dark-mode` class on body or a parent
        */


        :root {
            --bg: #050607;
            --text: #e6eef6;
            --muted: #9aa4ad;
            --card-bg: #0f1720;
            --card-border: #1f2a33;
            --primary: #6ea8ff;
            --success: #36d07a;
            --danger: #ff6b6b;
            --feedback-bg: #071322;
            --shadow: 0 2px 6px rgba(0,0,0,0.6);
        }
        body, .exam-take-container { background: var(--bg); color: var(--text); }

        /* Manual override: add `dark-mode` to body or a parent to force dark theme */
        body.dark-mode, .dark-mode { color: var(--text); background: var(--bg); }

        /* Layout and responsiveness */
        .exam-take-container { max-width: 980px; margin: 0 auto; padding: 1rem; }
        .no-select { user-select: none; }

          .flex-container { display: flex; align-items: center; gap: 0.75rem; }
          /* fieldWrapper is used around each label + input; ensure it stretches the full width */
          .fieldWrapper { width: 100%; display: flex; align-items: center; gap: 0.75rem; }

          /* We keep two main columns inside each fieldWrapper:
              - the label (question-text) which should take the bulk of space and sit to the left on wide screens
              - the input container (.flex-1) which holds the input and a post-input label; the post-input should float right
          */
          .flex-8 { flex: 8; }
             .flex-1 { flex: 1; display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; }

             /* Left-align label text on wide screens so the question text appears on the left, input between, and the
                 post-input label floats to the right. Padding keeps visual separation. */
             .question-text { margin: 0; text-align: left; padding-right: 0.5rem; }

        /* Answer items styled as cards */
        .physics-answer-list { list-style: none; padding: 0; margin: 0; }
        .physics-answer-list li {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            padding: 0.75rem 1rem;
            border: 1px solid var(--card-border);
            border-radius: 0.5rem;
            background: var(--card-bg);
            box-shadow: var(--shadow);
            color: var(--text);
        }
        .physics-answer-list li + li { margin-top: 0.75rem; }

        .question-text { margin: 0; }

        /* Post-input badge (shows True/False text next to inputs) */
    .postinput { display: inline-block; min-width: 3rem; text-align: right; color: var(--muted); }
    .physics-answer-list input { margin: 0 0.25rem 0 0; }
        /* show a compact post-label on the right of the input container */
        .physics-answer-list input:checked + .postinput::after {
            content: 'True'; color: var(--success); font-weight: 600; margin-left: 0.4rem;
        }
        .physics-answer-list input:not(:checked) + .postinput::after {
            content: 'False'; color: var(--muted); font-weight: 600; margin-left: 0.4rem;
        }

        /* Correctness badges and highlights */
        .answer-true::before,
        .answer-false::before {
            display: inline-block;
            margin-left: auto;
            padding: 0.18rem 0.5rem;
            border-radius: 0.25rem;
            font-size: 0.75rem;
            font-weight: 600;
            color: #fff;
        }
        .answer-true::before { content: "Correct"; background: var(--success); }
        .answer-false::before { content: "Not correct"; background: var(--muted); }

        .answer-correct { background-color: rgba(25,135,84,0.06); border-color: rgba(25,135,84,0.18); }
        .answer-incorrect { background-color: rgba(220,53,69,0.04); border-color: rgba(220,53,69,0.12); }

        /* Feedback block */
        .feedback {
            padding: 0.85rem 1rem;
            display: block;
            margin-top: 0.5rem;
            border-left: 4px solid var(--primary);
            background: var(--feedback-bg);
            color: var(--text);
            border-radius: 0.25rem;
        }

        /* Question menu (jump buttons) */
        #menu-list { margin-top: 1rem; display:flex; flex-wrap:wrap; gap:0.4rem; }
        button.question-menu-item {
            padding: 0.35rem 0.6rem;
            border-radius: 0.35rem;
            border: 1px solid var(--card-border);
            background: var(--card-bg);
            color: var(--text);
            cursor: pointer;
            font-size: 0.9rem;
        }
        button.question-menu-item.current-question {
            background: var(--primary); color: #fff; border-color: var(--primary);
            box-shadow: 0 1px 3px rgba(13,110,253,0.18);
        }

        /* Control buttons spacing */
        .mt-2 .save { margin-right: 0.5rem; }
        .mt-2 .save.btn.btn-default { background: var(--feedback-bg); border: 1px solid var(--card-border); color: var(--text); }

    /* Overview button - make it stand out using the primary color */
    #overview-button { background: var(--primary); border-color: var(--primary); color: #fff; }
    #overview-button:hover { filter: brightness(0.95); box-shadow: 0 3px 8px rgba(13,110,253,0.18); }

    /* Previous / Next button colors */
    .mt-2 button[name="previous"] { background: var(--info); border-color: var(--info); color: #fff; }
    .mt-2 button[name="previous"]:hover { filter: brightness(0.95); box-shadow: 0 2px 6px rgba(0,0,0,0.08); }

    .mt-2 button[name="next"] { background: var(--info); border-color: var(--info); color: #fff; }
    .mt-2 button[name="next"]:hover { filter: brightness(0.95); box-shadow: 0 3px 8px rgba(13,110,253,0.18); }

        /* Small helper: make labels wrap nicely on small screens */
        @media (max-width: 576px) {
            /* On small screens, stack the label and input for readability */
            .flex-container { flex-direction: column; align-items: stretch; }
            .fieldWrapper { flex-direction: column; align-items: stretch; }
            .question-text { text-align: left; padding-right: 0; margin-bottom: 0.4rem; }
            .flex-1 { justify-content: flex-start; gap: 0.5rem; }
            .postinput { margin-top: 0.4rem; text-align: left; }
            .physics-answer-list li { padding: 0.6rem; }
        }
    </style>
{% endblock %}

{% block js %}
<script>
/* HTMX swap helpers: preserve scroll and container height to avoid page jumping
     Listens for htmx:beforeSwap and htmx:afterSwap on the question fragment container.
*/
(function(){
    function getTarget(evt){
        return (evt.detail && (evt.detail.target || evt.detail.elt)) || evt.target;
    }

    document.addEventListener('htmx:beforeSwap', function(evt){
        try{
            var target = getTarget(evt);
            if(!target || target.id !== 'question-fragment') return;
            // save scroll position
            target.__savedScrollY = window.scrollY || window.pageYOffset || 0;
            // preserve current height to avoid layout shift while fragment is swapping
            target.style.minHeight = target.offsetHeight + 'px';
        }catch(e){ console && console.error && console.error(e); }
    });

    document.addEventListener('htmx:afterSwap', function(evt){
        try{
            var target = getTarget(evt);
            if(!target || target.id !== 'question-fragment') return;
            var y = target.__savedScrollY;
            if(typeof y === 'number') window.scrollTo(0, y);
            // remove the minHeight after a short delay to allow content to layout
            setTimeout(function(){ target.style.minHeight = ''; }, 60);
            // notify fragment-loaded so fragment init code can listen if required
            target.dispatchEvent(new CustomEvent('fragment:loaded', { bubbles: true }));
        }catch(e){ console && console.error && console.error(e); }
    });
})();
</script>
<script>
// Global listener for server-triggered save confirmations.
// The view sets an `HX-Trigger` header named `saved` when an HTMX save occurs.
document.addEventListener('saved', function(evt){
    try {
        // Prefer toastr if available
        if (typeof toastr !== 'undefined' && toastr && typeof toastr.success === 'function') {
            toastr.success('Saved');
            return;
        }

        // Lightweight fallback toast
        const id = 'htmx-save-toast';
        let el = document.getElementById(id);
        if (!el) {
            el = document.createElement('div');
            el.id = id;
            el.style.position = 'fixed';
            el.style.top = '1rem';
            el.style.right = '1rem';
            el.style.zIndex = 2000;
            el.style.padding = '0.6rem 0.9rem';
            el.style.background = 'rgba(40,167,69,0.95)';
            el.style.color = '#fff';
            el.style.borderRadius = '0.4rem';
            el.style.boxShadow = '0 2px 8px rgba(0,0,0,0.2)';
            el.style.fontWeight = '600';
            el.style.opacity = '0';
            el.style.transition = 'opacity 220ms ease-in-out';
            document.body.appendChild(el);
        }
        el.textContent = 'Saved';
        // show
        requestAnimationFrame(function(){ el.style.opacity = '1'; });
        // hide after 1.6s
        setTimeout(function(){ el.style.opacity = '0'; }, 1600);
    } catch (e) { console && console.error && console.error(e); }
});
</script>
{% endblock %}
