<?php
	require_once('../common.php');

	$is_sandbox = false;
	$title = "My Videos";
	if ($_GET['sandbox']) {
		$is_sandbox = true;
		$title = "Sandbox";
	}
?>
<html>
	<head>
	<title>Playwire Video API Test - <?php echo $title; ?></title>
		<style>
			ul.videos {
				list-style: none;
			}
			li.video {
				padding: 5px;
				padding-bottom: 20px;
			}
			img.thumbnail {
				max-width: 200px;
				border-width: 0;
			}
		</style>
	</head>
<body>
<?php
	if (empty($api_token)) {
		showAPITokenForm();
	} else {
		require_once('../class.playwire.php');

		$playwire = new Playwire($api_token);
?>
<h2><?php echo $title ?> for user with token <?php echo $api_token ?></h2>
<a href="index.php?clear_token=1">Clear API Token</a><br/><br/>
<?php
	function paginationLink($is_sandbox, $count, $page)
	{
		$result = '';
		$result .= "<a href=\"index.php?count=".$count."&page=".$page;
		if ($is_sandbox)
			$result .= '&sandbox=1';
		$result .= "\">".$page."</a>";
		return $result;
	}

	echo '<a href="upload.php">Upload a Video</a><br/><br/>';

	if ($is_sandbox) {
		$total_videos = $playwire->getVideoSandboxCount();
		echo '<a href="index.php">Go To My Videos</a>';
	} else {
		$total_videos = $playwire->getVideoCount();
		echo '<a href="index.php?sandbox=1">Go To Video Sandbox</a>';
	}

	echo "<h3>Total videos is " . $total_videos . "</h3>";

	$page = 1;
	if ($_GET['page']) {
		$page = $_GET['page'];
	}
	$count = 10;
	if ($_GET['count']) {
		$count = $_GET['count'];
	}
	$sort_by = '';
	if ($_GET['sort']) {
		$sort_by = $_GET['sort'];
	}

	echo "<h3>Videos per page: ";
	foreach(array(10, 20, 30, 40, 50) as $per_page) {
		if ($count == $per_page)
			echo $per_page . " ";
		else
			echo "<a href=\"index.php?count=" . $per_page . "\">" . $per_page . "</a> ";
	}
	echo "</h3>";

	echo "<h3>Sort by: ";
	foreach(array('title', 'title_desc', 'total_views', 'total_views_desc') as $sort_by_local) {
		if ($sort_by_local == $sort_by)
			echo $sort_by_local . " ";
		else
			echo "<a href=\"index.php?sort=" . $sort_by_local . "\">" . $sort_by_local . "</a> ";
	}
	echo "</h3>";

	$params = array();
	if ($sort_by != '') {
		$params['get'] = array();
		$params['get']['sort'] = $sort_by;
	}

	echo '<ul class="videos">';
	if ($is_sandbox)
		$videos = $playwire->getVideoSandboxIndex($count, $page, $params);
	else
		$videos = $playwire->getVideoIndex($count, $page, $params);
	foreach($videos as $video) {
		echo "<li class=\"video\"><a href=\"";
		if ($is_sandbox)
			echo "video_profile.php";
		else
			echo "video.php";
		echo "?id=" . $video->id . "\">";
		echo "<img class=\"thumbnail\" src=\"" . $video->thumb_url . "\"/><br/>\n";
		echo $video->name . "</a><br/>\n";
		echo date_format($video->created_at, "F j, Y g:i A") . "<br/>\n";
		echo "Views: " . $video->total_views . "<br/>\n";
		echo "Category: " . $video->category_name . "<br/>\n";
		echo "<a href=\"delete.php?id=" . $video->id . "\">Delete Video</a><br/>\n";
		echo "</li>";
	}
?>
</ul>
Page: 
<?php
	$iterations = ceil($total_videos/$count);

	for ($i = 1; $i <= $iterations; $i++) {
		if ($page == $i)
			echo ($i) . " ";
		else
			echo paginationLink($is_sandbox, $count, $i) . " ";
	}

	} // for top level if
?>
</body>
</html>
