{% extends "dictionary/base.html" %}
{% load filters functions i18n %}

{% block title %}{% trans "user" %}: {{ profile.username }}{% if tab.name != "latest" %} - {{ tab.label }}{% endif %}{% if page_obj.number > 1 %} - {% trans "page" %} {{ page_obj.number }}{% endif %}{% endblock %}

{% block bodyclass %} has-entries{% endblock %}
{% block bodyattr %}{% if user.is_authenticated and user.pinned_entry_id %} data-pin="{{ user.pinned_entry_id }}"{% endif %}{% endblock %}

{% block content %}
    {% load humanize %}
    <section class="user-info">
        <h1 class="h4">
            <a class="profile-username" href="{% url 'topic-search' %}?q={{ profile.username }}">{{ profile.username }}</a>
            {% with gen=profile.generation %}
                {% if gen %}
                    <small class="ml-2" style="font-size: .5em; vertical-align: middle">{% blocktrans with user_type=profile.is_novice|yesno:_("novice,author") gen_ordinal=gen|ordinal %}{{ gen_ordinal }} generation {{ user_type }}{% endblocktrans %}</small>
                {% endif %}
            {% endwith %}
        </h1>

        {% if user.is_authenticated and user != profile %}
            <ul class="user-links" data-username="{{ profile.username }}">
                {% check_follow_user user profile as is_following %}
                <li class="follow-user-trigger">
                    <a role="button" tabindex="0">{{ is_following|yesno:_("unfollow,follow") }}</a>
                </li>

                <li class="block-user">
                    <a role="button" tabindex="0">{% trans "block" %}</a>
                </li>

                <li class="send-message">
                    <a role="button" tabindex="0">{% trans "message" %}</a>
                </li>
            </ul>
        {% endif %}

        {% if profile.is_suspended and user == profile %}
            {% url "general-report" as report_url %}
            <div class="rounded bg-light mb-3 p-3" role="alert">
                {% blocktrans trimmed with date=profile.suspended_until %}
                    Your account was suspended until the date {{ date }}. For a detailed information
                    check the private message we sent to you. If you think something is wrong, you can use this
                    <a href="{{ report_url }}">report form</a> to contact us.
                {% endblocktrans %}
            </div>
        {% endif %}

        <ul class="user-stats mb-2">
            <li title="{% trans "total number of entries" %}">{{ profile.entry_count }}</li>
            <li title="{% trans "number of entries last month" %}">{{ profile.entry_count_month }}</li>
            <li title="{% trans "number of entries last week" %}">{{ profile.entry_count_week }}</li>
            <li title="{% trans "number of entries last 24 hours" %}">{{ profile.entry_count_day }}</li>
            <li class="nobullet" title="{% trans "latest entry when?" %}">{{ profile.last_entry_date|naturaltime|default:_("never") }}</li>

            {% if novice_queue %}
                <li class="float-right nobullet">{% blocktrans with queue_ordinal=novice_queue|ordinal %}you are the {{ queue_ordinal }} person in the novice list.{% endblocktrans %}</li>
            {% endif %}

            <li class="profile-badges nobullet d-block mt-2">
                {% if profile.is_novice %}
                    <a class="badge-item" href="{% url 'topic-unicode-url' unicode_string=_("novice") %}">{% trans "novice" %}</a>
                {% endif %}

                {% if profile.is_suspended %}
                    {% blocktrans asvar curse_time with time=profile.suspended_until|naturaltime %}throughout {{ time }}{% endblocktrans %}
                    <a class="badge-item" href="{% url 'topic-unicode-url' unicode_string=_("cursed") %}" title="{{ curse_time }}">{% trans "cursed" %}</a>
                {% endif %}

                {% if profile.entry_count >= 1000 %}
                    <a class="badge-item" href="{% url "topic-unicode-url" unicode_string=_("zealous") %}" title="{% trans "has at least 1000 entries" %}">{% trans "zealous" %}</a>
                {% endif %}

                {% has_perm profile "dictionary.can_suggest_categories" as can_suggest_categories %}

                {% if can_suggest_categories %}
                    <a class="badge-item" href="{% url "topic-unicode-url" unicode_string=_("assorter") %}" title="{% trans "can suggest categories to topics" %}">{% trans "assorter" %}</a>
                {% endif %}

                {% for badge in profile.badges.all %}
                    <a title="{{ badge.description }}" class="badge-item" {% if badge.url %} href="{{ badge.url }}" target="_blank" {% else %}href="{% url 'topic-unicode-url' unicode_string=badge.name %}"{% endif %}>{{ badge.name }}</a>
                {% endfor %}

                {% if profile.karma_flair %}
                    <span>{{ profile.karma_flair }}</span>
                {% endif %}
            </li>
        </ul>


        {% if profile.pinned_entry_id %}
            {% with profile.pinned_entry as entry %}
                {% include "dictionary/includes/profile_entry_pinned.html" %}
            {% endwith %}
        {% else %}
            {% with profile.entry_nice as entry %}
                {% if entry %}
                    {% include "dictionary/includes/profile_entry_pinned.html" %}
                {% endif %}
            {% endwith %}
        {% endif %}

    </section>

    {% with object_count=page_obj.paginator.count %}

        <nav class="user-stats-nav" aria-label="{% trans "Profile navigation menu" %}">
            <ul class="m-0 p-0">
                <li {% if tab.name == "latest" %}class="active"{% endif %}><a href="{{ profile.get_absolute_url }}">{% trans "entries" %}</a></li>
                <li {% if tab.name == "favorites" %}class="active"{% endif %}><a href="{% url 'user-profile-stats' profile.slug "favorites" %}">{% trans "favorites" %}</a></li>

                <li class="profile-stat-expand{% if tab.name != "latest" and tab.name != "favorites" %} active{% endif %}">
                    <a class="expand" role="button" tabindex="0" id="more-tabs-dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{% trans "statistics" %}</a>
                    <div class="dropdown-menu" aria-labelledby="more-tabs-dropdown" data-orientation="bottom-start">
                        <a class="dropdown-item" href="{% url 'user-profile-stats' profile.slug "popular" %}">{% trans "most favorited" %}</a>
                        <a class="dropdown-item" href="{% url 'user-profile-stats' profile.slug "recentlyvoted" %}">{% trans "recently voted" %}</a>
                        <a class="dropdown-item" href="{% url 'user-profile-stats' profile.slug "liked" %}">{% trans "most liked" %}</a>
                        <a class="dropdown-item" href="{% url 'user-profile-stats' profile.slug "weeklygoods" %}">{% trans "attracting entries of this week" %}</a>
                        <a class="dropdown-item" href="{% url 'user-profile-stats' profile.slug "beloved" %}">{% trans "beloved entries" %}</a>
                        <a class="dropdown-item" href="{% url 'user-profile-stats' profile.slug "authors" %}">{% trans "favorite authors" %}</a>
                        <a class="dropdown-item" href="{% url 'user-profile-stats' profile.slug "wishes" %}">{% trans "wishes" %}</a>
                        <a class="dropdown-item" href="{% url 'user-profile-stats' profile.slug "channels" %}">{% trans "contributed channels" %}</a>
                    </div>
                </li>
            </ul>
        </nav>

        <header class="d-flex justify-content-between align-items-center mt-3">
            <h2 class="h5 font-weight-bold">
                <a href="{% url 'user-profile-stats' profile.slug tab.name %}">{{ tab.label }}{% if object_count %} ({{ object_count }}){% endif %}</a>
                {% if page_obj.number > 1 %}
                    <span class="ml-2">- {% trans "page" %} {{ page_obj.number }}</span>
                {% endif %}
            </h2>

            {% if tab.type == "entry" %}
                <a role="button" tabindex="0" href="{% url 'topic_list' slug='userstats' %}?user={{ profile.slug }}&tab={{ tab.name }}" data-lf-slug="userstats" data-tab="{{ tab.name }}" data-lf-extra='{"user":"{{ profile.slug }}"}'>
                    <svg fill="currentColor" viewBox="0 0 16 16" width="24" height="24">
                        <use href="#lshift"></use>
                        <title>{% trans "navigate to the topic list" %}</title>
                    </svg>
                </a>
            {% endif %}
        </header>

        {% if object_count > 0 %}

            {% if tab.type == "entry" %}
                <section class="user_entries">
                    <ul class="profile entries">
                        {% for entry in object_list %}
                            {% include "dictionary/includes/entry.html" with show_title="yes" %}
                        {% endfor %}
                    </ul>
                </section>
            {% endif %}

            {% if tab.type == "author" %}
                <ul class="list-group">
                    {% for author in object_list %}
                        <li class="list-group-item d-flex justify-content-between" data-username="{{ author.username }}">
                            <a href="{{ author.get_absolute_url }}">{{ author.username }}</a>
                            {% if user.is_authenticated %}
                                {% check_follow_user user author as is_following_stat %}
                                {% if author != user %}
                                    <span class="follow-user-trigger"><a role="button" tabindex="0">{{ is_following_stat|yesno:_("unfollow,follow") }}</a></span>
                                {% endif %}
                            {% endif %}
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}

            {% if tab.type == "topic" %}
                <ul class="list-group">
                    {% for topic in object_list %}
                        <li class="list-group-item d-flex justify-content-between">
                            <a href="{{ topic.get_absolute_url }}">{{ topic.title }}</a>
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}

            {% if tab.type == "category" %}
                <ul class="list-group">
                    {% for category in object_list %}
                        <li class="list-group-item d-flex justify-content-between">
                            <a href="{% url 'topic_list' slug='userstats' %}?user={{ profile.slug }}&tab=channels&channel={{ category.slug }}" data-lf-slug="userstats" data-tab="channels" data-lf-extra='{"user": "{{ profile.slug }}", "channel": "{{ category.slug }}"}' title="{{ category.description }}">#{{ category.name }}</a>
                            <span>{{ category.count }}</span>
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}

        {% else %}
            <small>{% trans "nothing here" %}</small>
        {% endif %}

    {% endwith %}

    <div class="my-4">
        {% include "dictionary/includes/paginaton.html" with paginator=page_obj stretch="yes" index="yes" %}
    </div>
    {% if user.is_authenticated %}
        {% include "dictionary/includes/send_message_modal.html" %}
        {% include "dictionary/includes/block_user_modal.html" %}
    {% endif %}
{% endblock %}


{% block rightframe %}
    {% if user.is_authenticated %}
        <label for="memento"><h2 class="h5"><strong>{% trans "memento" %}</strong></h2></label>
        <form method="post" class="memento">
            {% load widget_tweaks %}
            {% blocktrans asvar text_placeholder with username=profile.username %}your thoughts about the user '{{ username }}'{% endblocktrans %}
            {% render_field form.body placeholder=text_placeholder name="memento" class="form-control" rows="10" id="memento" %}
            {% csrf_token %}
            <button class="btn btn-django mt-3 stretch-mobile">{% trans "save" %}</button>
        </form>
    {% endif %}
{% endblock %}
