{% extends "base.html" %}

{% block title %}
	{%- if thread.package -%}
		{%- if thread.review -%}
			{%- if thread.review.rating > 3 -%}
				{%- set rating = "👍" -%}
			{%- elif thread.review.rating < 3 -%}
				{%- set rating = "👎" -%}
			{%- else -%}
				{%- set rating = "-" -%}
			{%- endif -%}
		{%- endif -%}
		{{ rating }} {{ thread.title }} - {{ thread.package.title }}
	{%- else -%}
		{{ thread.title }}
	{%- endif -%}
{% endblock %}

{% block description -%}
	{{ thread.get_description() }}
{%- endblock %}

{% block headextra %}
	<meta name="og:image" content="{{ thread.author.get_profile_pic_url() }}"/>
{% endblock %}

{% block scriptextra %}
	{% if current_user.is_authenticated %}
		<script src="/static/js/quick_review_voting.js?v=2"></script>
	{% endif %}
	{% from "macros/forms.html" import easymde_scripts %}
	{{ easymde_scripts() }}
{% endblock %}

{% block content %}
	{% if current_user.is_authenticated %}
		{% if current_user in thread.watchers %}
			<form method="post" action="{{ thread.get_unsubscribe_url() }}" class="float-end">
				<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
				<input type="submit" class="btn btn-primary" value="{{ _('Unsubscribe') }}" />
			</form>
		{% else %}
			<form method="post" action="{{ thread.get_subscribe_url() }}" class="float-end">
				<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
				<input type="submit" class="btn btn-primary" value="{{ _('Subscribe') }}" />
			</form>
		{% endif %}
		{% if thread.check_perm(current_user, "DELETE_THREAD") %}
			<a href="{{ url_for('threads.delete_thread', id=thread.id) }}" class="float-end me-2 btn btn-danger">{{ _('Delete') }}</a>

			{#
				The standard way to convert to thread is from the Edit review page
				We only want to show the Convert to Thread button if they can also delete the thread
			#}
			{% if thread.review and thread.review.check_perm(current_user, "DELETE_REVIEW") %}
				<form method="post" action="{{ thread.review.get_delete_url() }}" class="float-end me-2">
					<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
					<input type="submit" class="btn btn-danger" value="{{ _('Convert to Thread') }}" />
				</form>
			{% endif %}
		{% endif %}
		{% if thread.check_perm(current_user, "LOCK_THREAD") %}
			{% if thread.locked %}
				<form method="post" action="{{ url_for('threads.set_lock', id=thread.id, lock=0) }}" class="float-end me-2">
					<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
					<input type="submit" class="btn btn-secondary" value="{{ _('Unlock') }}" />
				</form>
			{% else %}
				<form method="post" action="{{ url_for('threads.set_lock', id=thread.id, lock=1) }}" class="float-end me-2">
					<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
					<input type="submit" class="btn btn-secondary" value="{{ _('Lock') }}" />
				</form>
			{% endif %}
		{% endif %}
	{% endif %}

	{% if current_user == thread.author and thread.review %}
		<a class="btn btn-primary ms-1 float-end me-2"
				href="{{ thread.review.package.get_url('packages.review') }}">
			<i class="fas fa-pen"></i>
			{{ _("Edit Review") }}
		</a>
	{% endif %}

	<h1>
		{% if thread.review %}
			{% if thread.review.rating > 3 %}
				<i class="fas fa-thumbs-up me-2" style="color:#6f6;"></i>
			{% elif thread.review.rating < 3 %}
				<i class="fas fa-thumbs-down me-2" style="color:#f66;"></i>
			{% else %}
				<i class="fas fa-minus me-2" style="color:#999"></i>
			{% endif %}
		{% endif %}
		{% if thread.private %}&#x1f512; {% endif %}{{ thread.title }}
	</h1>

	{% if thread.package %}
		<p>
			{{ _("Package") }}: <a href="{{ thread.package.get_url('packages.view') }}">{{ thread.package.title }}</a>
		</p>
	{% endif %}

	{% if thread.private %}
		<aside class="row">
			<div class="col-md-9">
				<i>
					{{ _("This thread is only visible to its creator, package maintainers, and users of Approver rank or above.") }}
				</i>
			</div>
			<div class="col-md-3">
				<div class="d-flex flex-row justify-content-end flex-wrap align-items-center" style="gap: 0.5em;">
					<span class="text-muted me-2" title="{{ _('This thread is visible to the following users') }}">
						{{ _("Visible to:") }}
					</span>
					{% for viewer in thread.get_visible_to() %}
						<a href="{{ url_for('users.profile', username=viewer.username) }}" title="{{ viewer.display_name }}">
							<img style="max-height: 2em;" src="{{ viewer.get_profile_pic_url() }}" loading="lazy"
								alt="{{ viewer.display_name }}">
						</a>
					{% endfor %}
					<a href="{{ url_for('users.list_all') }}" title="{{ _('Plus approvers and editors') }}">
						+ <i class="fas fa-user-check"></i>
					</a>
				</div>
			</div>
		</aside>
	{% endif %}

	{% set report = thread.report.first() %}
	{% if report and report.check_perm(current_user, "SEE_REPORT") %}
		<a class="btn bg-primary btn-large" href="{{ url_for('report.view', rid=report.id) }}">View report page</a>
	{% endif %}

	{% if thread.review and current_user == thread.package.author %}
		{% set flag %}
			<i class="fas fa-flag mx-2"></i>
		{% endset %}
		<aside class="alert alert-secondary">
			<strong>{{ _("Is this review outdated?") }}</strong>
			{{ _("For example, does it complain about issues you've already fixed?") }}
			{{ _("You can report outdated reviews by clicking %(flag)s on the review.", flag=flag)  }}
		</aside>
	{% endif %}
	{% from "macros/threads.html" import render_thread %}
	{{ render_thread(thread, current_user, form) }}
{% endblock %}
