{% from "macros/user.html" import m_user_photo, m_user_link %}
{% from "macros/form.html" import m_field %}

{% macro m_comments(entity) %}
  {%- if comments.has_comments(entity) %}
    <ol class="comments">
    {%- for comment in comments.for_entity(entity) %}
      <li id="comment-{{ comment.id }}">{{ m_comment(comment) }}</li>
    {%- endfor %}
    </ol>
  {%- endif %}
{% endmacro %}

{# single comment #}
{% macro m_comment_edit_link(comment) %}
  {%- if g.user == comment.owner or g.is_manager %}
    <a href="{{ url_for('comments.edit', entity_id=comment.entity.id, object_id=comment.id) }}">
      <i class="fa fa-edit"></i>
      {{ _('Edit') }}
    </a>
  {%- endif %}
{% endmacro %}

{% macro m_comment_delete_link(comment) %}
  {%- if g.user == comment.owner or g.is_manager %}
    <a class="js-widget text-danger"
       href="{{ url_for('comments.delete', entity_id=comment.entity.id, object_id=comment.id) }}"
       data-init-with="deleteConfirm"
       data-init-params="{{ {
                          'title': _('Delete comment ?'),
                          'message': _('Do you really want to delete this comment?'),
                          'label': _("Delete"),
                          'cancelLabel': _('Cancel'),
                          }|tojson|forceescape }}">
      <i class="fa fa-trash"></i>
      {{ _('Delete') }}
    </a>
  {%- endif %}
{% endmacro %}

{% macro m_comment(comment) %}
  <div class="header">
  {%- call m_user_link(comment.creator) %}
    {{ m_user_photo(comment.creator, size=45) }}
  {%- endcall %}

  {%- call m_user_link(comment.creator, css='author') %}
    {{ comment.creator.name }}
  {%- endcall %}
  <span class="date"><small>{{ comment.created_at | age }}</small></span>
  </div>

  <p class="comment-body"> {{ comment.body | linkify | paragraphs }} </p>
  <div>
    {{ m_comment_edit_link(comment) }}
    <span class="pull-right">
      {{ m_comment_delete_link(comment) }}
      {%- set count = comment.history |length %}
      {%- if comment.history %}
        <small>
          {{ _n('edited', 'edited %(count)d times', count, count=count) }}
        </small>
      {%- endif %}
    </span>
  </div>
{% endmacro %}

{% macro m_comment_form(entity) %}
  {%- set ctx = comments.get_form_context(entity) %}
  <form action="{{ ctx.url }}" method="POST" enctype="multipart/form-data" role="form">
    {%- for field in ctx.form %}
      {{ m_field(field) }}
    {%- endfor %}

    <div class="form-group form-actions">
      {%- for button in ctx.buttons %}
        {{ button.render() }}
      {%- endfor %}
    </div>
  </form>
{% endmacro %}
