<?php
/*****************************************************************************
 * PhotoPage.php
 * Contains the photo page template.
 *
 * Kabuki CMS (C) 2013-2016, Aaron van Geffen
 *****************************************************************************/

class PhotoPage extends SubTemplate
{
	protected $photo;
	private $exif;
	private $previous_photo_url = '';
	private $next_photo_url = '';
	private $is_asset_owner = false;

	public function __construct(Image $photo)
	{
		$this->photo = $photo;
	}

	public function setPreviousPhotoUrl($url)
	{
		$this->previous_photo_url = $url;
	}

	public function setNextPhotoUrl($url)
	{
		$this->next_photo_url = $url;
	}

	public function setIsAssetOwner($flag)
	{
		$this->is_asset_owner = $flag;
	}

	protected function html_content()
	{
		$this->photoNav();
		$this->photo();

		echo '
				<div id="sub_photo">
					<h2 class="entry-title">', $this->photo->getTitle(), '</h2>';

		$this->taggedPeople();
		$this->linkNewTags();

		echo '
				</div>';

		$this->photoMeta();

		if($this->is_asset_owner)
			$this->addUserActions();

		echo '
				<script type="text/javascript" src="', BASEURL, '/js/photonav.js"></script>';
	}

	protected function photo()
	{
		echo '
				<div id="photo_frame">
					<a href="', $this->photo->getUrl(), '">';

		if ($this->photo->isPortrait())
			echo '
						<img src="', $this->photo->getThumbnailUrl(null, 960), '" alt="">';
		else
			echo '
						<img src="', $this->photo->getThumbnailUrl(1280, null), '" alt="">';

		echo '
					</a>
				</div>';
	}

	private function photoNav()
	{
		if ($this->previous_photo_url)
			echo '
				<a href="', $this->previous_photo_url, '" id="previous_photo"><em>Previous photo</em></a>';
		else
			echo '
				<span id="previous_photo"><em>Previous photo</em></span>';

		if ($this->next_photo_url)
			echo '
				<a href="', $this->next_photo_url, '" id="next_photo"><em>Next photo</em></a>';
		else
			echo '
				<span id="next_photo"><em>Next photo</em></span>';
	}

	private function photoMeta()
	{
		echo '
				<div id="photo_exif_box">
					<h3>EXIF</h3>
					<dl class="photo_meta">';

		if (!empty($this->exif->created_timestamp))
			echo '
						<dt>Date Taken</dt>
						<dd>', date("j M Y, H:i:s", $this->exif->created_timestamp), '</dd>';

		echo '
						<dt>Uploaded by</dt>
						<dd>', $this->photo->getAuthor()->getfullName(), '</dd>';

		if (!empty($this->exif->camera))
			echo '
						<dt>Camera Model</dt>
						<dd>', $this->exif->camera, '</dd>';

		if (!empty($this->exif->shutter_speed))
			echo '
						<dt>Shutter Speed</dt>
						<dd>', $this->exif->shutterSpeedFraction(), '</dd>';

		if (!empty($this->exif->aperture))
			echo '
						<dt>Aperture</dt>
						<dd>f/', number_format($this->exif->aperture, 1), '</dd>';

		if (!empty($this->exif->focal_length))
			echo '
						<dt>Focal Length</dt>
						<dd>', $this->exif->focal_length, ' mm</dd>';

		if (!empty($this->exif->iso))
			echo '
						<dt>ISO Speed</dt>
						<dd>', $this->exif->iso, '</dd>';

		echo '
					</dl>
				</div>';
	}

	private function taggedPeople()
	{
		echo '
					<h3>Tags</h3>
					<ul id="tag_list">';

		foreach ($this->photo->getTags() as $tag)
		{
			echo '
						<li id="tag-', $tag->id_tag, '">
							<a rel="tag" title="View all posts tagged ', $tag->tag, '" href="', $tag->getUrl(), '" class="entry-tag">', $tag->tag, '</a>';

			if ($tag->kind === 'Person')
				echo '
							<a class="delete-tag" title="Unlink this tag from this photo" href="#" data-id="', $tag->id_tag, '">❌</a>';

			echo '
						</li>';
		}

		echo '
					</ul>';
	}

	private function linkNewTags()
	{
		echo '
				<div>
					<h3>Link tags</h3>
					<p style="position: relative"><input type="text" id="new_tag" placeholder="Type to link a new tag"></p>
				</div>
				<script type="text/javascript" src="', BASEURL, '/js/ajax.js"></script>
				<script type="text/javascript" src="', BASEURL, '/js/autosuggest.js"></script>
				<script type="text/javascript">
					setTimeout(function() {
						var removeTag = function(event) {
							event.preventDefault();
							var that = this;
							var request = new HttpRequest("post", "', $this->photo->getPageUrl(), '",
								"id_tag=" + this.dataset["id"] + "&delete", function(response) {
									if (!response.success) {
										return;
									}

									var tagNode = document.getElementById("tag-" + that.dataset["id"]);
									tagNode.parentNode.removeChild(tagNode);
								});
						};

						var tagRemovalTargets = document.getElementsByClassName("delete-tag");
						for (var i = 0; i < tagRemovalTargets.length; i++) {
							tagRemovalTargets[i].addEventListener("click", removeTag);
						}

						var tag_autosuggest = new TagAutoSuggest({
							inputElement: "new_tag",
							listElement: "tag_list",
							baseUrl: "', BASEURL, '",
							appendCallback: function(item) {
								var request = new HttpRequest("post", "', $this->photo->getPageUrl(), '",
									"id_tag=" + item.id_tag, function(response) {
										var newLink = document.createElement("a");
										newLink.href = item.url;

										var newLabel = document.createTextNode(item.label);
										newLink.appendChild(newLabel);

										var removeLink = document.createElement("a");
										removeLink.className = "delete-tag";
										removeLink.dataset["id"] = item.id_tag;
										removeLink.href = "#";
										removeLink.addEventListener("click", removeTag);

										var crossmark = document.createTextNode("❌");
										removeLink.appendChild(crossmark);

										var newNode = document.createElement("li");
										newNode.id = "tag-" + item.id_tag;
										newNode.appendChild(newLink);
										newNode.appendChild(removeLink);

										var list = document.getElementById("tag_list");
										list.appendChild(newNode);
									}, this);
							}
						});
					}, 100);
				</script>';
	}

	public function setExif(EXIF $exif)
	{
		$this->exif = $exif;
	}

	public function addUserActions()
	{
		echo '
				<div id=user_actions_box>
					<h3>Actions</h3>
					<a class="btn btn-red" href="', BASEURL, '/', $this->photo->getSlug(), '?confirm_delete">Delete</a>
				</div>';
	}
}