{% extends "base.html" %}
{% block extra_head %}
<link rel="stylesheet" href="{{ static_url('/static/css/post.css') }}">
<link rel="stylesheet" href="{{ static_url('/static/css/quiz.css') }}">
{% endblock %}
{% block content %}
<div class="quiz-detail-page">
    <a href="/quizzes" class="back-link">&larr; Back to Quizzes</a>

    <article class="quiz-detail">
        {% if quiz.status == 'draft' %}
        <div class="quiz-banner quiz-banner-draft" role="status">This quiz is a draft. Only you and administrators can see it.</div>
        {% endif %}

        <h1 class="quiz-detail-title rendered-title">{{ render_title(quiz.title, author_is_admin=is_admin(author)) }}</h1>

        <div class="quiz-detail-author">
            {% set _size = 32 %}{% set _size_class = "sm" %}{% set _user = author %}{% include "_avatar_link.html" %}
            <div>
                {% set _user = author %}{% set _class = none %}{% include "_user_link.html" %}
                <span class="meta-muted meta-muted-spaced">&middot; {{ dt_ago(quiz.created_at) if quiz.created_at else time_ago }}</span>
            </div>
        </div>

        {% if quiz.description %}
        {% if maturity_hidden(maturity, user) %}
        {% set _level = maturity %}{% include "_maturity_gate.html" %}
        {% else %}
        <div class="quiz-detail-desc rendered-content">{{ render_content(quiz.description, author_is_admin=is_admin(author)) }}</div>
        {% endif %}
        {% endif %}

        <div class="quiz-stats-strip">
            <div class="quiz-stat"><span class="quiz-stat-value">{{ quiz.question_count }}</span><span class="quiz-stat-label">Questions</span></div>
            <div class="quiz-stat"><span class="quiz-stat-value">{{ quiz.total_points }}</span><span class="quiz-stat-label">Points</span></div>
            <div class="quiz-stat"><span class="quiz-stat-value">{% if quiz.time_limit_seconds %}{{ format_duration(quiz.time_limit_seconds) }}{% else %}None{% endif %}</span><span class="quiz-stat-label">Time limit</span></div>
            <div class="quiz-stat"><span class="quiz-stat-value">{% if quiz.pass_percent %}{{ quiz.pass_percent }}%{% else %}-{% endif %}</span><span class="quiz-stat-label">Pass mark</span></div>
            <div class="quiz-stat"><span class="quiz-stat-value">{{ quiz.attempt_count }}</span><span class="quiz-stat-label">Attempts</span></div>
        </div>

        <div class="quiz-detail-actions">
            {% if quiz.status == 'published' %}
            {% if viewer_state == 'in_progress' and viewer_attempt_uid %}
            <a href="{{ quiz.url }}/attempts/{{ viewer_attempt_uid }}" class="btn btn-primary">Resume attempt</a>
            {% else %}
            <form method="POST" action="{{ quiz.url }}/attempts" class="inline-form">
                <button type="submit" class="btn btn-primary" {{ guest_disabled(user) }}>{% if viewer_state == 'done' %}Play again{% else %}Start quiz{% endif %}</button>
            </form>
            {% endif %}
            {% if viewer_state == 'done' and viewer_attempt_uid %}
            <a href="{{ quiz.url }}/attempts/{{ viewer_attempt_uid }}/results" class="btn btn-secondary">My results</a>
            {% endif %}
            {% endif %}
            {% if quiz.viewer_can_edit %}
            <a href="{{ quiz.url }}/edit" class="btn btn-secondary">Edit</a>
            {% endif %}
            <a href="{{ quiz.url }}/export" class="btn btn-secondary">Export JSON</a>
            {% set _type = "quiz" %}{% set _uid = quiz.uid %}{% set _my_vote = my_vote %}{% set _count = star_count %}{% set _btn_class = "btn btn-secondary" %}{% include "_star_vote.html" %}
            {% set _type = "quiz" %}{% set _uid = quiz.uid %}{% set _bookmarked = bookmarked %}{% include "_bookmark_button.html" %}
            {% set _type = "quiz" %}{% set _uid = quiz.uid %}{% set _owner = quiz.user_uid %}{% set _owner_name = (author or {}).get('username', '') %}{% set _class = "btn btn-secondary" %}{% include "_report_button.html" %}
            {% if quiz.viewer_owns or is_admin(user) %}
            <form method="POST" action="/quizzes/delete/{{ quiz.slug }}" class="inline-form">
                <button type="submit" class="btn btn-danger" data-confirm="Delete this quiz and every attempt on it?" data-confirm-danger>Delete</button>
            </form>
            {% endif %}
        </div>

        {% set _type = "quiz" %}{% set _uid = quiz.uid %}{% set _reactions = reactions %}{% include "_reaction_bar.html" %}
    </article>

    {% if leaderboard %}
    <section id="leaderboard" class="quiz-leaderboard" aria-label="Quiz leaderboard">
        <h2>Leaderboard</h2>
        <ol class="quiz-leaderboard-list">
            {% for entry in leaderboard %}
            <li class="quiz-leaderboard-row">
                <span class="quiz-rank">{{ entry.rank }}</span>
                {% set _size = 28 %}{% set _size_class = "sm" %}{% set _user = entry.user %}{% include "_avatar_link.html" %}
                <span class="quiz-leaderboard-name">{% set _user = entry.user %}{% set _class = none %}{% include "_user_link.html" %}</span>
                <span class="quiz-score-chip">{{ format_number(entry.score_percent) }}%</span>
                {% if entry.passed %}<span class="quiz-pass-badge">Passed</span>{% endif %}
            </li>
            {% endfor %}
        </ol>
    </section>
    {% endif %}

    {% include "_comment_section.html" %}
</div>
{% endblock %}
