{% from 'forms/_form.html' import form_header, form_rows, form_footer %}
{% from 'events/abstracts/reviewing/_common.html' import render_tracks %}
{% from 'events/reviews/_common.html' import render_instruction_box, render_score, render_user_avatar %}

{% macro _render_abstract_reviewing_state(abstract) %}
    {% if abstract.reviewed_for_tracks|length > 1 %}
        {% set conflicting_msg = _("Proposed to accept in different tracks") %}
    {% else %}
        {% set conflicting_msg = _("All reviewers proposed to accept this abstract "
                                    "but as different contribution types") %}
    {% endif %}
    {% set mapping_title = {'not_started': _("No reviewer has reviewed this abstract yet"),
                            'in_progress': _("Not reviewed in all tracks"),
                            'positive': _("All reviewers proposed to accept this abstract"),
                            'conflicting': conflicting_msg,
                            'negative': _("No reviewer proposed to accept this abstract"),
                            'mixed': _("Not all reviewers proposed to accept this abstract")} %}
    {% set mapping_icon = {'positive': 'icon-checkmark',
                           'conflicting': 'icon-checkmark',
                           'negative': 'icon-cross',
                           'mixed': 'icon-tilde',
                           'in_progress': 'icon-circle-small',
                           'not_started': 'icon-circle-small'} %}
    {% set state = abstract.reviewing_state %}
    <span class="global-state-icon flexrow f-a-center f-j-center {{ state.css_class }}">
        <span class="flexrow {{ mapping_icon[state.name] }}"></span>
    </span>
    <div class="f-self-stretch flexcol">
        <div class="summary-title semantic-text {{ state.css_class }}">{{ state.title }}</div>
        <div class="summary-description">{{ mapping_title[state.name] }}</div>
    </div>
{% endmacro %}

{% macro _render_abstract_track_reviewing_state_icon(abstract, track) %}
    {% set mapping_title = {'not_started': _("No reviewer has reviewed this abstract yet"),
                            'positive': _("All reviewers proposed to accept this abstract"),
                            'conflicting': _("All reviewers proposed to accept this abstract "
                                             "but as different contribution types"),
                            'negative': _("No reviewer proposed to accept this abstract"),
                            'mixed': _("Not all reviewers proposed to accept this abstract")} %}
    {% set mapping_icon = {'positive': 'icon-checkmark',
                           'conflicting': 'icon-checkmark',
                           'negative': 'icon-cross',
                           'mixed': 'icon-tilde',
                           'not_started': 'icon-circle-small'} %}
    {% set state = abstract.get_track_reviewing_state(track) %}
    <div class="group-state-icon {{ state.css_class }} flexrow f-a-center f-j-center"
         title="{{ mapping_title[state.name] }}">
        <span class="{{ mapping_icon[state.name] }}"></span>
    </div>
{% endmacro %}

{% macro render_review_summary_box(abstract, user, visible_tracks) %}
    {% set event = abstract.event %}
    {% set can_judge = abstract.can_judge(user, check_state=true) %}
    <div class="i-timeline-item-label action icon-file-check"></div>
    <div class="i-timeline-item-box content-indicator-left">
        <div class="i-box-content track-reviews-wrapper">
            {% if can_judge or visible_tracks.issuperset(abstract.reviewed_for_tracks) %}
                <div class="global-review-summary flexrow f-a-center">
                    {{ _render_abstract_reviewing_state(abstract) }}
                    <div class="abstract-comment-badges flexrow f-a-center">
                        {% if abstract.score is not none %}
                            {{ render_score(abstract.score, event.cfa.rating_range, classes='i-tag') }}
                        {% endif %}
                    </div>
                </div>
            {% else %}
                <div class="global-review-summary flexrow f-a-center">
                    <div class="summary-title">
                        {%- trans %}Review summary{% endtrans -%}
                    </div>
                </div>
            {% endif %}
            {% set ratings = abstract.track_question_scores %}
            {% for track in visible_tracks %}
                {% set has_reviews = track.id in ratings %}
                <div class="group-review-summary">
                    <div class="group-review-summary__summary flexrow f-a-center">
                        {{ _render_abstract_track_reviewing_state_icon(abstract, track) }}
                        <div class="group-review-summary__track f-self-stretch truncate-text">
                            {{- track.title_with_group -}}
                        </div>
                        {% set score = abstract.get_track_score(track) %}
                        {% if has_reviews %}
                            <a href="#" class="group-review-summary__link"
                               data-toggle="#review-list-{{ track.id }}"
                               data-accordion=".track-reviews-wrapper"
                               data-show-text="{% trans %}Show reviews{% endtrans %}"
                               data-hide-text="{% trans %}Hide reviews{% endtrans %}">
                                {%- trans %}Show reviews{% endtrans -%}
                            </a>
                            {% if score is not none %}
                                <a href="#" class="group-review-summary__link"
                                   data-toggle="#question-list-{{ track.id }}"
                                   data-accordion=".track-reviews-wrapper"
                                   data-show-text="{% trans %}Show questions{% endtrans %}"
                                   data-hide-text="{% trans %}Hide questions{% endtrans %}">
                                    {%- trans %}Show questions{% endtrans -%}
                                </a>
                            {% endif %}
                        {% endif %}
                        {% if score is not none %}
                            {{ render_score(score, event.cfa.rating_range) }}
                        {% endif %}
                    </div>
                    {% if has_reviews %}
                        <div class="group-review-summary__details">
                            <div id="question-list-{{ track.id }}" class="details-content weak-hidden">
                                <div class="details-content-inner review-questions">
                                    {% for question, score in ratings[track.id].items() %}
                                        <div class="details-content-row flexrow f-a-center">
                                            <div>
                                                <span class="question-index">{{ loop.index }}</span>
                                            </div>
                                            <div class="question-text f-self-stretch">{{ question.title }}</div>
                                            {% if score is not none %}
                                                {{ render_score(score, abstract.event.cfa.rating_range) }}
                                            {% endif %}
                                        </div>
                                    {% endfor %}
                                </div>
                            </div>
                            <div id="review-list-{{ track.id }}" class="details-content weak-hidden">
                                <div class="details-content-inner">
                                    {% for review in abstract.get_reviews(group=track) %}
                                        <div class="details-content-row flexrow f-a-center">
                                            <div class="f-self-stretch">
                                                {%- trans name=review.user.name, action=review.proposed_action.title -%}
                                                    <strong>{{ name }}</strong> proposes to {{ action }}
                                                {%- endtrans %}
                                            </div>
                                            <a href="#proposal-review-{{ review.id }}"
                                               class="group-review-summary__link js-highlight-review">
                                                {%- trans %}See review{% endtrans -%}
                                            </a>
                                            {% if review.score is not none %}
                                                {{ render_score(review.score, abstract.event.cfa.rating_range) }}
                                            {% endif %}
                                        </div>
                                    {% endfor %}
                                </div>
                            </div>
                        </div>
                    {% endif %}
                </div>
            {% endfor %}
        </div>
    </div>
{% endmacro %}

{% macro render_reviewed_for_tracks_box(abstract, form) %}
    <div id="abstract-reviewed-for-tracks-box" class="i-timeline-item">
        <div class="i-timeline-item-label action icon-list"></div>
        <div class="i-timeline-item-box content-indicator-left">
            <div class="i-box-content">
                <div class="flexrow">
                    <div class="f-self-stretch">
                        <div class="info-line">
                            <label>
                                {% trans count=abstract.reviewed_for_tracks|length -%}
                                    Reviewing for track:
                                {%- pluralize -%}
                                    Reviewing for tracks:
                                {%- endtrans -%}
                            </label>
                            {{ render_tracks(abstract.reviewed_for_tracks) }}
                        </div>
                    </div>
                    {% if abstract.edit_track_mode.name != 'none' %}
                        <div>
                            <a href="#" class="hide-if-locked"
                               data-href="{{ url_for('.edit_review_tracks', abstract) }}"
                               data-form-container="#edit-reviewed-for-track-list"
                               data-update="#reviewing-page"
                               data-replace-update
                               data-hide-trigger
                               data-ajax-form>
                                {%- trans %}Edit track list{% endtrans -%}
                            </a>
                        </div>
                    {% endif %}
                </div>
            </div>
            <div class="i-box-footer hidden-if-empty" id="edit-reviewed-for-track-list"></div>
        </div>
    </div>
{% endmacro %}
