{% extends "cranefly/layout.html" %}
{% import "cranefly/macros.html" as macros with context %}

{% block title %}{{ macros.page_title(title=_('Threads you are watching')) }}{% endblock %}

{% block container %}
<div class="page-header header-primary">
  <div class="container">
    {{ messages_list(messages) }}
    <h1>{% trans %}Threads you are watching{% endtrans %}</h1>

    <ul class="nav nav-tabs header-tabs">
      <li class="{% if not new %}active{% endif %}"><a href="{% url 'watched_threads' %}">{% trans %}All Threads{% endtrans %}</a></li>
      <li class="{% if new %}active{% endif %}"><a href="{% url 'watched_threads_new' %}">{% trans %}Unread Threads{% endtrans %}</a></li>
    </ul>
  </div>
</div>

<div class="container container-primary">
  {% if message %}
  <div class="messages-list">
    {{ macros.draw_message(message) }}
  </div>
  {% endif %}

  {% if threads %}
  <div class="forum-threads-list watched-threads">
    {{ pager() }}
    <table class="table">
      <thead>
        <tr>
          <th>{% trans %}Thread{% endtrans %}</th>
          <th colspan="2" class="span5">{% trans %}Activity{% endtrans %}</th>
        </tr>
      </thead>
      <tbody>
        {% for thread in threads %}
        <tr id="watch-{{ loop.index }}">
          <td>
            <a href="{{ thread_url(thread, 'new') }}" class="thread-icon{% if not thread.is_read %} thread-new{% endif %} tooltip-top" title="{% if not thread.is_read -%}
            {% trans %}Click to see first unread post{% endtrans %}
            {%- else -%}
            {% trans %}Click to see last post{% endtrans %}
            {%- endif %}"><i class="icon-comment"></i></a>
            <a href="{{ thread_url(thread) }}" class="thread-name">{{ thread.name }}</a>
            <span class="thread-details">
              {% trans user=thread_starter(thread), forum=thread_forum(thread), start=thread.start|reltimesince|low %}by {{ user }} in {{ forum }} {{ start }}{% endtrans %}
            </span>
          </td>
          <td>
            <div class="thread-last-reply">
              {{ replies(thread.replies) }} - {% trans user=thread_reply(thread), last=thread.last|reltimesince|low %}last by {{ user }} {{ last }}{% endtrans %}
            </div>
          </td>
          <td class="watched-thread-flags">
            <form action="{{ thread_url(thread, 'unwatch') }}" method="post">
              <input type="hidden" name="{{ csrf_id }}" value="{{ csrf_token }}">
              <input type="hidden" name="retreat" value="{{ delete_retreat(loop) }}">
              <button type="submit" class="btn btn-danger tooltip-top" title="{% trans %}Unwatch{% endtrans %}"><i class="icon-remove"></i></button>
            </form>

            <form action="{% if thread.send_email %}{{ thread_url(thread, 'unwatch_email') }}{% else %}{{ thread_url(thread, 'watch_email') }}{% endif %}" method="post">
              <input type="hidden" name="{{ csrf_id }}" value="{{ csrf_token }}">
              <input type="hidden" name="retreat" value="{{ request_path }}#watch-{{ loop.index }}">
              <button type="submit" class="btn btn-{% if thread.send_email %}success{% else %}inverse{% endif %} tooltip-top" title="{% if thread.send_email %}{% trans %}Don't notify with e-mail{% endtrans %}{% else %}{% trans %}Notify with e-mail{% endtrans %}{% endif %}"><i class="icon-envelope"></i></button>
            </form>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
    {{ pager() }}
  </div>
  {% else %}
  <p class="lead">{% if new -%}
    {% trans %}There are no unread threads that you are watching.{% endtrans %}
    {%- else -%}
    {% trans %}You are not watching any threads.{% endtrans %}
    {%- endif %}</p>
  {% endif %}
</div>
{% endblock %}


{% macro thread_url(thread, route='') -%}{% filter url(thread=thread.pk, slug=thread.slug) %}{% filter trim %}
{% if thread.forum_id == private_threads.pk -%}
private_thread
{%- elif thread.forum_id == reports.pk -%}
report
{%- else -%}
thread
{%- endif -%}{%- if route %}_{{ route }}{% endif %}{% endfilter %}{% endfilter %}
{%- endmacro%}

{% macro replies(thread_replies) -%}
{% trans count=thread_replies, replies=('<strong>' ~ (thread_replies|intcomma) ~ '</strong>')|safe -%}
{{ replies }} reply
{%- pluralize -%}
{{ replies }} replies
{%- endtrans %}
{%- endmacro %}

{% macro thread_starter(thread) -%}
{% if thread.start_poster_id %}<a href="{% url 'user' user=thread.start_poster_id, username=thread.start_poster_slug %}" class="user-link">{{ thread.start_poster_name }}</a>{% else %}{{ thread.start_poster_name }}{% endif %}
{%- endmacro %}

{% macro thread_forum(thread) -%}{% filter trim %}
{% if thread.forum_id == private_threads.pk %}
<a href="{% url 'private_threads' %}" class="forum-link">{% trans %}Private Threads{% endtrans %}</a>
{% elif thread.forum_id == reports.pk %}
TODO!
{% else %}
<a href="{% url 'forum' forum=thread.forum_id, slug=thread.forum.slug %}" class="forum-link">{{ thread.forum.name }}</a>
{% endif%}
{% endfilter %}{%- endmacro %}

{% macro thread_reply(thread) -%}
{% if thread.last_poster_id %}<a href="{% url 'user' user=thread.last_poster_id, username=thread.last_poster_slug %}" class="user-link">{{ thread.last_poster_name }}</a>{% else %}{{ thread.last_poster_name }}{% endif %}
{%- endmacro %}

{% macro pager() -%}
{% if pagination['total'] > 1 %}
<div class="pagination">
  <ul>
    <li class="count">{{ macros.pager_label(pagination) }}</li>
    {% if new %}
    {%- if pagination['prev'] > 1 %}<li><a href="{% url 'watched_threads_new' %}" class="tooltip-top" title="{% trans %}Latest Threads{% endtrans %}"><i class="icon-chevron-left"></i> {% trans %}Latest{% endtrans %}</a></li>{% endif -%}
    {%- if pagination['prev'] > 0 %}<li><a href="{%- if pagination['prev'] > 1 %}{% url 'watched_threads_new' page=pagination['prev'] %}{% else %}{% url 'watched_threads_new' %}{% endif %}" class="tooltip-top" title="{% trans %}Newer Threads{% endtrans %}"><i class="icon-chevron-left"></i></a></li>{% endif -%}
    {%- if pagination['next'] > 0 %}<li><a href="{% url 'watched_threads_new' page=pagination['next'] %}" class="tooltip-top" title="{% trans %}Older Threads{% endtrans %}"><i class="icon-chevron-right"></i></a></li>{% endif -%}
    {% else %}
    {%- if pagination['prev'] > 1 %}<li><a href="{% url 'watched_threads' %}" class="tooltip-top" title="{% trans %}Latest Threads{% endtrans %}"><i class="icon-chevron-left"></i> {% trans %}Latest{% endtrans %}</a></li>{% endif -%}
    {%- if pagination['prev'] > 0 %}<li><a href="{%- if pagination['prev'] > 1 %}{% url 'watched_threads' page=pagination['prev'] %}{% else %}{% url 'watched_threads' %}{% endif %}" class="tooltip-top" title="{% trans %}Newer Threads{% endtrans %}"><i class="icon-chevron-left"></i></a></li>{% endif -%}
    {%- if pagination['next'] > 0 %}<li><a href="{% url 'watched_threads' page=pagination['next'] %}" class="tooltip-top" title="{% trans %}Older Threads{% endtrans %}"><i class="icon-chevron-right"></i></a></li>{% endif -%}
    {% endif %}
  </ul>
</div>
{% endif %}
{%- endmacro %}

{% macro delete_retreat(loop) -%}
{%- if pagination['page'] == 1 -%}
  {{ request_path }}{% if not (loop.first and loop.last) %}#watch-{{ loop.index }}{% endif %}
{%- else -%}
  {%- if loop.first and loop.last -%}
    {%- if new -%}
      {%- if pagination['prev'] > 1 %}{% url 'watched_threads_new' page=pagination['prev'] %}{% else %}{% url 'watched_threads_new' %}{% endif %}
    {%- else -%}
      {%- if pagination['prev'] > 1 %}{% url 'watched_threads' page=pagination['prev'] %}{% else %}{% url 'watched_threads' %}{% endif %}
    {%- endif -%}#watch-{{ settings.threads_per_page }}
  {%- else -%}
    {{ request_path }}#watch-{{ loop.index }}
  {%- endif -%}
{%- endif  -%}
{%- endmacro %}