{% from 'events/abstracts/reviewing/timeline.html' import render_abstract_review_action_text,
                                                          render_ratings_details_titled_rule, render_ratings_details_extra %}
{% from 'events/abstracts/reviewing/_common.html' import render_track %}
{% from 'events/reviews/_common.html' import render_edited_hint, render_group, render_score, render_visibility_hint,
                                             render_user_avatar %}
{% from 'events/reviews/_omnibox.html' import render_omnibox %}


{% macro render_timeline(proposal, comment_form, review_form=none, revision_form=none) %}
    {% set event_locked = proposal.event.is_locked %}
    {% set can_comment = not event_locked and proposal.can_comment(session.user, check_state=true) %}
    {% set can_review = not event_locked and proposal.can_review(session.user, check_state=true) %}
    {% for revision in proposal.get_revisions() %}
        {% set timeline = revision.get_timeline(session.user) %}
        {% if not loop.first %}
            <div class="i-timeline">
                <div class="i-timeline to-separator-wrapper">
                    <div class="i-timeline-connect-down to-separator"></div>
                </div>
            </div>
        {% endif %}
        {{ caller('revision-info', revision) }}
        {% set timeline_class = 'weak-hidden' if revision != proposal.get_last_revision() %}
        <div class="i-timeline {{ timeline_class }}" id="revision-timeline-{{ revision.id }}">
            {% if timeline or (can_comment or can_review) %}
                <div class="i-timeline with-line">
                    <div class="i-timeline-connect-up"></div>
                    {% for item in timeline %}
                        {% if item.timeline_item_type == 'comment' %}
                            {{ render_comment(item, proposal) }}
                        {% elif item.timeline_item_type == 'review' %}
                            {{ render_review(item, form=form) }}
                        {% elif item.timeline_item_type == 'judgment' %}
                            {{ caller('revision-judgment', revision) }}
                        {% endif %}
                    {% endfor %}
                    {% if loop.last and (can_comment or can_review) %}
                        {% set caller_ = caller %}
                        {% call(block) render_omnibox(proposal, comment_form, review_form) %}
                            {{ caller_(block) }}
                        {% endcall %}
                    {% endif %}
                </div>
            {% endif %}
            {% if loop.last %}
                <div class="i-timeline to-separator-wrapper">
                    {# XXX: We need to nest the connector into another .i-timeline to keep the same nesting #}
                    <div class="i-timeline-connect-down to-separator"></div>
                </div>
                <div class="i-timeline-separator"></div>
                {{ caller('submit-revision') }}
                {% if proposal.is_in_final_state %}
                    {{ caller('revision-judgment', revision) }}
                {% endif %}
            {% endif %}
        </div>
    {% endfor %}
{% endmacro %}


{% macro render_comment(comment, proposal) %}
    <div id="review-comment-{{ comment.id }}" class="i-timeline-item">
        {{ render_user_avatar(comment.user) }}
        <div class="flexrow i-timeline-item-content">
            <div class="i-timeline-item-box header-indicator-left">
                <div class="i-box-header flexrow">
                    <div class="f-self-stretch">
                        {% trans name=comment.user.name -%}
                            <strong>{{ name }}</strong> left a comment
                        {%- endtrans %}
                        {{ render_visibility_hint(comment.visibility) }}
                        <time datetime="{{ comment.created_dt.isoformat() }}">
                            {{- comment.created_dt|format_human_date -}}
                        </time>
                        {% if comment.modified_dt %}
                            {{ render_edited_hint(comment.modified_dt, comment.modified_by) }}
                        {% endif %}
                    </div>
                    {% if comment.can_edit(session.user) %}
                        <div class="review-comment-actions hide-if-locked">
                            <a class="i-link icon-edit js-edit-comment"
                               title="{% trans %}Edit comment{% endtrans %}"
                               data-href="{{ proposal.get_save_comment_url(comment) }}"
                               data-form-container="#review-comment-{{ comment.id }} .js-form-container"
                               data-update="#reviewing-page"
                               data-replace-update
                               data-confirm-close-unsaved
                               data-ajax-form></a>
                            <a class="i-link icon-cross js-delete-comment"
                               title="{% trans %}Remove comment{% endtrans %}"
                               data-method="DELETE"
                               data-href="{{ proposal.get_delete_comment_url(comment) }}"
                               data-title="{% trans %}Remove comment{% endtrans %}"
                               data-confirm="{% trans %}Are you sure you want to remove this comment?{% endtrans %}"
                               data-ajax>
                            </a>
                        </div>
                    {% endif %}
                </div>
                <div class="i-box-content js-form-container">
                    <div class="markdown-text">
                        {{ comment.text }}
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endmacro %}


{% macro render_review(review, form) %}
    {% set no_content = not review.comment and not review.ratings %}
    {% set ratings = review.ratings|rejectattr('question.is_deleted')|sort(attribute='question.position') %}
    {% set proposal = review.revision.proposal %}
    <div id="proposal-review-{{ review.id }}" class="i-timeline-item">
        {{ render_user_avatar(review.user) }}
        <div class="flexrow i-timeline-item-content">
            <div class="i-timeline-item-metadata">
                <div class="f-self-stretch">
                    {% trans name=review.user.name -%}
                        <strong>{{ name }}</strong> left a review
                    {%- endtrans %}
                    {{ render_visibility_hint(review.visibility) }}
                    <time datetime="{{ review.created_dt.isoformat() }}">
                        {{- review.created_dt|format_human_date -}}
                    </time>
                    {% if review.modified_dt %}
                        {{ render_edited_hint(review.modified_dt) }}
                    {% endif %}
                </div>
                {% if proposal.proposal_type == 'abstract' %}
                    {{ render_track(review.group.instance) }}
                {% else %}
                    {{ render_group(review.group.title) }}
                {% endif %}
            </div>
            <div class="i-timeline-item-box header-indicator-top {% if not review.comment %}header-only{% endif %}"
                {% if not review.comment %}data-no-comment{% endif %}>
                <div class="i-box-header flexrow">
                    <div class="review-badges">
                        {% if proposal.proposal_type == 'abstract' %}
                            {{ render_abstract_review_action_text(review) }}
                        {% endif %}
                        {% if ratings %}
                            {% if review.score is not none %}
                                {% set pretty_score = render_score(review.score, proposal.cfp.rating_range,
                                                                   classes='bold semantic-text') %}
                                · {% trans %}score {{ pretty_score }}{% endtrans %}
                            {% endif %}
                            (<a class="js-ratings-toggle toggle-link">{#--#}
                                <span class="js-show-ratings">{% trans %}show ratings{% endtrans %}</span>{#--#}
                                <span class="weak-hidden js-hide-ratings">{% trans %}hide ratings{% endtrans %}</span>{#--#}
                            </a>)
                        {% endif %}
                    </div>
                    {% if review.can_edit(session.user, check_state=true) %}
                        <div class="review-comment-actions hide-if-locked">
                            <a class="i-link icon-edit js-edit-review"
                               title="{% trans %}Edit review{% endtrans %}"
                               data-href="{{ proposal.get_save_review_url(review=review) }}"
                               data-form-container="#proposal-review-{{ review.id }} .js-form-container"
                               data-update="#reviewing-page"
                               data-replace-update
                               data-confirm-close-unsaved
                               data-ajax-form></a>
                        </div>
                    {% endif %}
                </div>
                <div class="i-box-content js-form-container">
                    {% if ratings %}
                        <div class="ratings-details weak-hidden">
                            <ul class="review-questions">
                                {% for rating in ratings %}
                                    <li class="flexrow">
                                        <div>
                                            <span class="question-index">{{ loop.index }}</span>
                                        </div>
                                        <div class="question-text f-self-stretch">
                                            {{- rating.question.title -}}
                                        </div>
                                        <div>
                                            {% if rating.question.field_type == 'rating' %}
                                                <span class="rating-scale flexrow f-a-center">
                                                    {{ _render_rating(proposal.cfp.rating_range, rating.value) }}
                                                    <span class="score">{{ rating.value if rating.value is not none else '-' }}</span>
                                                </span>
                                            {% elif rating.question.field_type == 'bool' %}
                                                {% if rating.value is not none %}
                                                    {{ _('Yes') if rating.value else _('No') }}
                                                {% endif %}
                                            {% else %}
                                                {{ rating.value }}
                                            {% endif %}
                                            </span>
                                        </div>
                                    </li>
                                {% endfor %}
                            </ul>
                            {% if proposal.proposal_type == 'abstract' %}
                                {{ render_ratings_details_titled_rule(review) }}
                            {% else %}
                                {% if review.comment %}
                                    <div class="titled-rule">
                                        {% trans %}Comment{% endtrans %}
                                    </div>
                                {% endif %}
                            {% endif %}
                        </div>
                    {% endif %}
                    {% if proposal.proposal_type == 'abstract' %}
                        {{ render_ratings_details_extra(review) }}
                    {% endif %}
                    {% if review.comment %}
                        <div class="markdown-text">
                            {{ review.comment }}
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endmacro %}


{% macro _render_rating(rating_range, value) %}
    {%- for bullet in range(rating_range[0], rating_range[1] + 1) -%}
        <span class="bullet {{ 'full' if value is not none and bullet <= value else '' }}"></span>
    {%- endfor -%}
{% endmacro %}
