{% extends 'partials/base.html' %}

{% block name %}deck{% endblock %}

{% block content %}
<main>
	<article>
		<header>
		<span>Download: <a href="{{ card['item_location'] }}">{{ card['title'] }}</a></span>
		</header>    
		<main id="pdf-doc">
		</main>
		<footer>
			<form method="post">
				<input type="hidden" name="card_id" value="{{ card['id'] }}">
				<!-- <span id="buttonContainer" style="display: flex; justify-content: space-around;"> -->
				<span id="buttonContainer">
					<button type="submit" name="rate" value="Yes" accesskey="1">Yes</button> <!--ti keyi so alt+shit+key...-->
					<button type="submit" name="rate" value="Maybe" accesskey="2">Maybe</button>
					<button type="submit" name="rate" value="No" accesskey="3">No</button>
					<button type="submit" name="rate" value="Delete" accesskey="d">Delete</button>
					<button type="submit" name="share" value="Share" accesskey="s">Share</button>
				</span>      
			</form>
		</footer>
	</article>
</main>

<script type="text/javascript" src="{{ url_for('static', filename='js/pdf.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/pdf.worker.js') }}"></script>
<script type="text/javascript">
	document.addEventListener('DOMContentLoaded', function (){

			var currPage = 1; //Pages are 1-based not 0-based
			var numPages = 0;
			var thePDF = null;

			//This is where you start
			var doc = pdfjsLib.getDocument('{{ card["item_location"] }}');
			doc.promise.then(function(pdf) {
					//Set PDFJS global object (so we can easily access in our page functions
					thePDF = pdf;

					//How many pages it has
					numPages = pdf.numPages;

					//Start with first page
					pdf.getPage( 1 ).then( handlePages );
				});

			function handlePages(page)
			{
					//This gives us the page's dimensions at full scale
					var scale = 1.5;
					var viewport = page.getViewport({ scale: scale, });

					//We'll create a canvas for each page to draw it on
					var canvas = document.createElement( "canvas" );
					canvas.style.display = "block";
					var context = canvas.getContext('2d');

					//Add it to the web page
					document.getElementById('pdf-doc').appendChild(canvas);

					// Support HiDPI-screens.
						var outputScale = window.devicePixelRatio || 1;

					canvas.width = Math.floor(viewport.width * outputScale);
					canvas.height = Math.floor(viewport.height * outputScale);
					canvas.style.width = "100%";
					canvas.style.height =  Math.floor(canvas.style.width * viewport.width / viewport.height);


					var transform = outputScale !== 1
						? [outputScale, 0, 0, outputScale, 0, 0]
						: null;
					var renderContext = {
							canvasContext: context,
							viewport: viewport,
							transform: transform
						};

					//Draw it on the canvas
					page.render(renderContext);


					//Move to next page
					currPage++;
					if ( thePDF !== null && currPage <= Math.min(numPages, 10) )
				{
				thePDF.getPage( currPage ).then( handlePages );
				}
				}
				});
</script>

{% endblock %}
