{% extends "layout.html" %}
{% block title %}{{ torrent.display_name }} :: {{ config.SITE_NAME }}{% endblock %}
{% block metatags %}
{% set uploader_name = torrent.user.username if (torrent.user and not torrent.anonymous) else 'Anonymous' %}
<meta property="og:description" content="{{ category_name(torrent.sub_category.id_as_string) }} | {{ torrent.filesize | filesizeformat(True) }} | Uploaded by {{ uploader_name }} on {{ torrent.created_time.strftime('%Y-%m-%d') }}">
{% endblock %}
{% block body %}
{% from "_formhelpers.html" import render_field %}
<div class="panel panel-{% if torrent.deleted %}deleted{% elif torrent.remake %}danger{% elif torrent.trusted %}success{% else %}default{% endif %}">
	<div class="panel-heading"{% if torrent.hidden %} style="background-color: darkgray;"{% endif %}>
		<h3 class="panel-title">
			{% if can_edit %}
			<a href="{{ url_for('torrents.edit', torrent_id=torrent.id) }}" title="Edit torrent"><i class="fa fa-fw fa-pencil"></i></a>
			{% endif %}
			{{ torrent.display_name }}
		</h3>
	</div>
	<div class="panel-body">
		<div class="row">
			<div class="col-md-1">Category:</div>
			<div class="col-md-5">
				<a href="{{ url_for('main.home', c=torrent.main_category.id_as_string) }}">{{ torrent.main_category.name }}</a> - <a href="{{ url_for('main.home', c=torrent.sub_category.id_as_string) }}">{{ torrent.sub_category.name }}</a>
			</div>

			<div class="col-md-1">Date:</div>
			<div class="col-md-5" data-timestamp="{{ torrent.created_utc_timestamp|int }}">{{ torrent.created_time.strftime('%Y-%m-%d %H:%M UTC') }}</div>
		</div>

		<div class="row">
			<div class="col-md-1">Submitter:</div>
			<div class="col-md-5">
				{% set user_url = torrent.user and url_for('users.view_user', user_name=torrent.user.username) %}
				{%- if not torrent.anonymous and torrent.user -%}
				<a class="text-{{ torrent.user.userlevel_color }}" href="{{ user_url }}" data-toggle="tooltip" title="{{ torrent.user.userlevel_str }}">{{ torrent.user.username }}</a>
				{%- else -%}
				Anonymous {% if torrent.user and (g.user == torrent.user or g.user.is_moderator) %}(<a class="text-{{ torrent.user.userlevel_color }}" href="{{ user_url }}" data-toggle="tooltip" title="{{ torrent.user.userlevel_str }}">{{ torrent.user.username }}</a>){% endif %}
				{%- endif -%}
				{% if g.user and g.user.is_superadmin and torrent.uploader_ip %}
				({{ torrent.uploader_ip_string }})
				{% endif %}
			</div>

			<div class="col-md-1">Seeders:</div>
			<div class="col-md-5"><span style="color: green;">{% if config.ENABLE_SHOW_STATS %}{{ torrent.stats.seed_count }}{% else %}Coming soon{% endif %}</span></div>

		</div>

		<div class="row">
			<div class="col-md-1">Information:</div>
			<div class="col-md-5">
				{% if torrent.information %}
				{{ torrent.information_as_link | safe }}
				{% else %}
				No information.
				{% endif%}
			</div>

			<div class="col-md-1">Leechers:</div>
			<div class="col-md-5"><span style="color: red;">{% if config.ENABLE_SHOW_STATS %}{{ torrent.stats.leech_count }}{% else %}Coming soon{% endif %}</span></div>
		</div>

		<div class="row">
			<div class="col-md-1">File size:</div>
			<div class="col-md-5">{{ torrent.filesize | filesizeformat(True) }}</div>

			<div class="col-md-1">Completed:</div>
			<div class="col-md-5">{% if config.ENABLE_SHOW_STATS %}{{ torrent.stats.download_count }}{% else %}Coming soon{% endif %}</div>
		</div>
		<div class="row">
			<div class="col-md-offset-6 col-md-1">Info hash:</div>
			<div class="col-md-5"><kbd>{{ torrent.info_hash_as_hex }}</kbd></div>
		</div>
	</div><!--/.panel-body -->

	<div class="panel-footer clearfix">
		{% if torrent.has_torrent %}<a href="{{ url_for('torrents.download', torrent_id=torrent.id )}}"><i class="fa fa-download fa-fw"></i>Download Torrent</a> or {% endif %}<a href="{{ torrent.magnet_uri }}" class="card-footer-item"><i class="fa fa-magnet fa-fw"></i>Magnet</a>
		{% if g.user and g.user.age > config['RATELIMIT_ACCOUNT_AGE'] %}
		<button type="button" class="btn btn-xs btn-danger pull-right" data-toggle="modal" data-target="#reportModal">
			Report
		</button>
		{% endif %}
	</div>
</div><!--/.panel -->

<div class="panel panel-default">
	<div markdown-text class="panel-body" id="torrent-description">
		{%- if torrent.description -%}
		{# Escape newlines into html entities because CF strips blank newlines #}
		{{- torrent.description | escape | replace('\r\n', '\n') | replace('\n', '&#10;'|safe) -}}
		{%- else -%}
		#### No description.
		{%- endif -%}
	</div>
</div>

{% cache 86400, "filelist", torrent.info_hash_as_hex %}
{% if files and files.__len__() <= config.MAX_FILES_VIEW %}
<div class="panel panel-default">
	<div class="panel-heading">
		<h3 class="panel-title">File list</h3>
	</div>

	<div class="torrent-file-list panel-body">
		<ul>
		{% for key, value in files.items() recursive -%}
		{% if value is iterable %}
			{% set pre_expanded = not loop.depth0 and value.items()|length <= 20 %}
			<li>
				<a href="" class="folder"><i class="fa fa-folder{% if pre_expanded %}-open{% endif %}"></i>{{ key }}</a>
				<ul{% if pre_expanded %} data-show="yes"{% endif %}>{{ '\n' + loop(value.items()) }}
				</ul>
			</li>
		{% else %}
			<li><i class="fa fa-file"></i>{{ key }} <span class="file-size">({{ value | filesizeformat(True) }})</span></li>
		{% endif %}
		{% endfor %}
		</ul>
	</div>
</div><!--/.panel -->
{% elif files %}
<div class="panel panel-default">
	<div class="panel-heading panel-heading-collapse">
		<h3 class="panel-title">
			Too many files to display.
		</h3>
	</div>
</div>
{% else %}
<div class="panel panel-default">
	<div class="panel-heading panel-heading-collapse">
		<h3 class="panel-title">
			File list is not available for this torrent.
		</h3>
	</div>
</div>
{% endif %}
{% endcache %}

<div id="comments" class="panel panel-default">
	<div class="panel-heading">
		<a data-toggle="collapse" href="#collapse-comments" role="button" aria-expanded="{% if g.user and g.user.preferences.hide_comments %}false{% else %}true{% endif %}" aria-controls="collapse-comments">
		<h3 class="panel-title">
			Comments - {{ torrent.comment_count }}
		</h3>
		</a>
	</div>
	<div class="collapse {% if g.user and g.user.preferences.hide_comments %}{% else %}in{% endif %}" id="collapse-comments">
	{% for comment in comments %}
	<div class="panel panel-default comment-panel" id="com-{{ loop.index }}">
		<div class="panel-body">
			<div class="col-md-2">
				<p>
					<a class="text-{{ comment.user.userlevel_color }}" href="{{ url_for('users.view_user', user_name=comment.user.username) }}" data-toggle="tooltip" title="{{ comment.user.userlevel_str }}">{{ comment.user.username }}</a>
					{% if comment.user.id == torrent.uploader_id and not torrent.anonymous %}
					(uploader)
					{% endif %}
				</p>
				<img class="avatar" src="{{ comment.user.gravatar_url() }}" alt="{{ comment.user.userlevel_str }}">
			</div>
			<div class="col-md-10 comment">
				<div class="row comment-details">
					<a href="#com-{{ loop.index }}"><small data-timestamp-swap data-timestamp="{{ comment.created_utc_timestamp|int }}">{{ comment.created_time.strftime('%Y-%m-%d %H:%M UTC') }}</small></a>
					{% if comment.edited_time %}
					<small data-timestamp-swap data-timestamp-title data-timestamp="{{ comment.edited_utc_timestamp }}" title="{{ comment.edited_time }}">(edited)</small>
					{% endif %}
					<div class="comment-actions">
						{% if g.user.id == comment.user_id and not comment.editing_limit_exceeded and (not torrent.comment_locked or comment_form) %}
						<button class="btn btn-xs edit-comment" title="Edit"{% if config.EDITING_TIME_LIMIT %} data-until="{{ comment.editable_until|int }}"{% endif %}>Edit</button>
						{% endif %}
						{% if g.user.is_superadmin or (g.user.id == comment.user_id and not torrent.comment_locked and not comment.editing_limit_exceeded) %}
						<form class="delete-comment-form" action="{{ url_for('torrents.delete_comment', torrent_id=torrent.id, comment_id=comment.id) }}" method="POST">
							<button name="submit" type="submit" class="btn btn-danger btn-xs" title="Delete">Delete</button>
						</form>
						{% endif %}
					</div>
				</div>
				<div class="row">
					{# Escape newlines into html entities because CF strips blank newlines #}
					<div markdown-text class="comment-content" id="torrent-comment{{ comment.id }}">{{- comment.text | escape | replace('\r\n', '\n') | replace('\n', '&#10;'|safe) -}}</div>
					{% if g.user.id == comment.user_id and comment_form %}
					<form class="edit-comment-box" action="{{ url_for('torrents.edit_comment', torrent_id=torrent.id, comment_id=comment.id) }}" method="POST">
						{{ comment_form.csrf_token }}
						<div class="form-group">
							<textarea class="form-control" name="comment" autofocus>{{- comment.text | escape | replace('\r\n', '\n') | replace('\n', '&#10;'|safe) -}}</textarea>
						</div>
						{% if config.USE_RECAPTCHA and g.user.age < config['ACCOUNT_RECAPTCHA_AGE'] %}
						<div class="row">
							<div class="col-md-4">
								{% if comment_form.recaptcha.errors %}
								<div class="alert alert-danger">
									<p><strong>CAPTCHA error:</strong></p>
									<ul>
										{% for error in comment_form.recaptcha.errors %}
										<li>{{ error }}</li>
										{% endfor %}
									</ul>
								</div>
								{% endif %}
								{{ comment_form.recaptcha }}
							</div>
						</div>
						{% endif %}
						<input type="submit" value="Submit" class="btn btn-success btn-sm">
						<button class="btn btn-sm edit-comment" title="Cancel">Cancel</button>
						<span class="edit-error text-danger"></span>
						<div class="edit-waiting" style="display:none"></div>
					</form>
					{% endif %}
				</div>
			</div>
		</div>
	</div>

	{% endfor %}
	{% if torrent.comment_locked %}
	<div class="alert alert-warning">
		<p>
			<i class="fa fa-lock" aria-hidden="true"></i>
			Comments have been locked.
		</p>
	</div>
	{% endif %}
	{% if comment_form %}
	<form class="comment-box" method="POST">
		{{ comment_form.csrf_token }}
		<div class="row">
			<div class="col-md-12">
				{{ render_field(comment_form.comment, class_='form-control') }}
			</div>
		</div>
		{% if config.USE_RECAPTCHA and g.user.age < config['ACCOUNT_RECAPTCHA_AGE'] %}
		<div class="row">
			<div class="col-md-4">
				{% if comment_form.recaptcha.errors %}
				<div class="alert alert-danger">
					<p><strong>CAPTCHA error:</strong></p>
					<ul>
						{% for error in comment_form.recaptcha.errors %}
						<li>{{ error }}</li>
						{% endfor %}
					</ul>
				</div>
				{% endif %}
				{{ comment_form.recaptcha }}
			</div>
		</div>
		{% endif %}
		<div class="row">
			<div class="col-md-12">
				<input type="submit" value="Submit" class="btn btn-success btn-sm">
			</div>
		</div>
	</form>
	{% endif %}
	</div>
</div>

{% if g.user and g.user.age > config['RATELIMIT_ACCOUNT_AGE'] %}
<div class="modal fade" id="reportModal" tabindex="-1" role="dialog" aria-labelledby="reportModalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
						aria-hidden="true">&times;</span></button>
				<h4 class="modal-title">Report torrent #{{ torrent.id }}</h4>
			</div>
			<div class="modal-body">
				<div class="alert alert-warning" role="alert">
					Before submitting a report, please check that the torrent
					actually breaks <a href="{{ url_for('site.rules') }}">the
					rules</a>. Useless reports like "download is slow" or
					"thanks" can get you banned from the site.
				</div>
				<form method="POST" action="{{ url_for('torrents.report', torrent_id=torrent.id) }}">
					{{ report_form.csrf_token }}
					{{ render_field(report_form.reason, class_='form-control', maxlength=255) }}
					<div style="float: right;">
						<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
						<button type="submit" id="reportSubmit" class="btn btn-danger">Report</button>
					</div>
				</form>
			</div>
			<div class="modal-footer" style="border-top: none;">
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
	// Focus the report text field once the modal is opened.
	$('#reportModal').on('shown.bs.modal', function () {
		$('#reason').focus();
		$('#reportSubmit').attr('disabled', true);
	});
	$('#reason').on('input', function(e) {
		if($('#reason').val().length > 0) {
			$('#reportSubmit').removeAttr('disabled');
		} else {
			$('#reportSubmit').attr('disabled', true);
		}
	});
</script>
{% endif %}

{% endblock %}
