{% extends "base.html" %}
{% import "utils.html" as utils %}

{% block title %}{{ post.title }} - r/{{ post.community }}{% endblock %}

{% block search %}
	{% call utils::search(["/r/", post.community.as_str()].concat(), "") %}
{% endblock %}

{% block root %}/r/{{ post.community }}{% endblock %}{% block location %}r/{{ post.community }}{% endblock %}
{% block head %}
	{% call super() %}
	<meta name="author" content="u/{{ post.author.name }}">
{% endblock %}

<!-- OPEN COMMENT MACRO -->
{% macro comment(item) -%}
<div id="{{ item.id }}" class="comment">
	<div class="comment_left">
		<p class="comment_score">{{ item.score }}</p>
		<div class="line"></div>
	</div>
	<details class="comment_right" open>
		<summary class="comment_data">
			<a class="comment_author {{ item.author.distinguished }} {% if item.author.name == post.author.name %}op{% endif %}" href="/u/{{ item.author.name }}">u/{{ item.author.name }}</a>
			{% if item.author.flair.flair_parts.len() > 0 %}
				<small class="author_flair">{% call utils::render_flair(item.author.flair.flair_parts) %}</small>
			{% endif %}
			<span class="created" title="{{ post.created }}">{{ item.rel_time }}</span>
		</summary>
		<div class="comment_body">{{ item.body }}</div>
{%- endmacro %}

<!-- CLOSE COMMENT MACRO -->
{% macro close() %}
</details></div>
{% endmacro %}

{% block content %}
	<div id="column_one">

		<!-- POST CONTENT -->
		<div class="post highlighted">
			
			<p class="post_header">
				<a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a>
				<span class="dot">&bull;</span>
				<a class="post_author" href="/u/{{ post.author.name }}">u/{{ post.author.name }}</a>
				{% if post.author.flair.flair_parts.len() > 0 %}
					<small class="author_flair">{% call utils::render_flair(post.author.flair.flair_parts) %}</small>
				{% endif %}
				<span class="dot">&bull;</span>
				<span class="created" title="{{ post.created }}">{{ post.rel_time }}</span>
			</p>
			<p class="post_title">
				<a href="{{ post.permalink }}">{{ post.title }}</a>
				{% if post.flair.flair_parts.len() > 0 %}
					<small class="post_flair" style="color:{{ post.flair.foreground_color }}; background:{{ post.flair.background_color }};">{% call utils::render_flair(post.flair.flair_parts) %}</small>
				{% endif %}
				{% if post.flags.nsfw %} <small class="nsfw">NSFW</small>{% endif %}
			</p>

			<!-- POST MEDIA -->
			{% if post.post_type == "image" %}
			<img class="post_media" alt="Post image" width="{{ post.media.width }}px" height="{{ post.media.height}}px" src="{{ post.media.url }}"/>
			{% else if post.post_type == "video" || post.post_type == "gif" %}
			<video class="post_media" src="{{ post.media.url }}" controls autoplay loop></video>
			{% else if post.post_type == "link" %}
			<a id="post_url" href="{{ post.media.url }}">{{ post.media.url }}</a>
			{% endif %}

			<!-- POST BODY -->
			<div class="post_body">{{ post.body }}</div>
			<div class="post_score">{{ post.score }}<span class="label"> Upvotes</span></div>
			<div class="post_footer">
				
				<ul id="post_links">
					<li><a href="/{{ post.id }}">permalink</a></li>
					<li><a href="https://reddit.com/{{ post.id }}">reddit</a></li>
				</ul>
				<p>{{ post.upvote_ratio }}% Upvoted</p>
			</div>

		</div>

		<!-- SORT FORM -->
		<form id="sort">
			<select name="sort" title="Sort comments by"> 
				{% call utils::options(sort, ["confidence", "top", "new", "controversial", "old"], "confidence") %}
			</select><input id="sort_submit" type="submit" value="&rarr;">
		</form>

		<!-- COMMENTS -->
		{% for c in comments -%}
		<div class="thread">
			<!-- EACH COMMENT -->
			{% call comment(c) %}
			<blockquote class="replies">{% for reply1 in c.replies %}{% call comment(reply1) %}
				<!-- FIRST-LEVEL REPLIES -->
				<blockquote class="replies">{% for reply2 in reply1.replies %}{% call comment(reply2) %}
					<!-- SECOND-LEVEL REPLIES -->
					<blockquote class="replies">{% for reply3 in reply2.replies %}{% call comment(reply3) %}
						<!-- THIRD-LEVEL REPLIES -->
						{% if reply3.replies.len() > 0 %}
							<!-- LINK TO CONTINUE REPLIES -->
							<a class="deeper_replies" href="{{ post.permalink }}{{ reply3.id }}">&rarr; More replies</a>
						{% endif %}
						{% call close() %}
					{% endfor %}
					</blockquote>{% call close() %}
				{% endfor %}
				</blockquote>{% call close() %}
			{% endfor %}
			</blockquote>{% call close() %}
		</div>
		{%- endfor %}

	</div>
{% endblock %}