{% 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 }}">
{% 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 {% if item.author == post.author %}op{% endif %}" href="/u/{{ item.author }}">u/{{ item.author }}</a>
			{% if item.flair.0 != "" %}
				<small class="author_flair">{{ item.flair.0 }}</small>
			{% endif %}
			&bull; <span class="datetime">{{ item.time }}</span>
		</summary>
		<p class="comment_body">{{ item.body }}</p>
{%- endmacro %}

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

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

		<!-- POST CONTENT -->
		<div class="post highlighted panel">
			<div class="post_left">
				<p class="post_score">{{ post.score }}</p>
				{% if post.flags.nsfw %}<div class="nsfw">NSFW</div>{% endif %}
			</div>
			<div class="post_right">
				<p class="post_header">
					<b><a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a></b>
					&bull;
					<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
					{% if post.author_flair.0 != "" %}
						<small class="author_flair">{{ post.author_flair.0 }}</small>
					{% endif %}
					<span class="datetime">{{ post.time }}</span>
				</p>
				<a href="{{ post.permalink }}" class="post_title">
					{{ post.title }}
					{% if post.flair.0 != "" %}
						<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
					{% endif %}
				</a>
				{% if post.post_type == "image" %}
				<img class="post_media" src="{{ post.media }}"/>
				{% else if post.post_type == "video" %}
				<video class="post_media" src="{{ post.media }}" controls autoplay loop>
				{% else if post.post_type == "link" %}
				<a id="post_url" href="{{ post.media }}">{{ post.media }}</a>
				{% endif %}
				<div class="post_body">{{ post.body }}</div>
				<div id="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>
		</div>

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

		<!-- COMMENTS -->
		{% for c in comments -%}
		<div class="thread">
			<!-- EACH COMMENT -->
			{% call comment(c) %}
			<div class="replies">{% for reply1 in c.replies %}{% call comment(reply1) %}
				<!-- FIRST-LEVEL REPLIES -->
				<div class="replies">{% for reply2 in reply1.replies %}{% call comment(reply2) %}
					<!-- SECOND-LEVEL REPLIES -->
					<div 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 %}
					</div>{% call close() %}
				{% endfor %}
				</div>{% call close() %}
			{% endfor %}
			</div>{% call close() %}
		</div>
		{%- endfor %}

	</div>
{% endblock %}