<?php
require_once('global.php');
require_once(ABSOLUTE_PATH.'/class/ChampionHelper.php');
require_once(ABSOLUTE_PATH.'/class/Paginator.php');
require_once(ABSOLUTE_PATH.'/class/ToplistManager.php');

$template_body_class.= 'background-global';

$db = db::getInstance();
$ch = ChampionHelper::getInstance();
$tm = ToplistManager::getInstance();

// Content Selection
$contentMap = [
	'skillscore' => 'LSS Leaderboard',
	'most-games' => 'Champion Enthusiasts',
	'champion-mastery' => 'Champion Masteries'
];
$currentContent = ((isset($_GET['content']))&&(isset($contentMap[$_GET['content']]))) ? $_GET['content'] : 'skillscore';
$contentSelect = '			<nav class="nav-content nav flex-wrap align-items-stretch">';
foreach($contentMap as $key => $value){
	$active = ($currentContent==$key) ? ' active' : '';
	$contentSelect.= '				<a class="nav-link'.$active.'" href="top/'.$key.'">'.$value.'</a>';
}
$contentSelect.= '		</nav>';

$filterChampion = ((isset($_GET['filterChampion']))&&($_GET['filterChampion']!='')) ? htmlspecialchars($_GET['filterChampion']) : null;
$filterPlatform = ((isset($_GET['filterPlatform']))&&($_GET['filterPlatform']!='')) ? htmlspecialchars($_GET['filterPlatform']) : null;

if(!$ch->isValidChampionId($filterChampion)){
	$filterChampion = null;
}

switch($currentContent){
	case 'highest-lolskillscore':
	default:
		$type = ToplistManager::TYPE_SCORE;
#		$messageBoxContent = '						<p>This toplist features summoners with the highest LoLSkillScores for a champion for the current season. LoLSkillScore is a measurement invented by LoLSkill to determine the skill of a certain summoner with a certain champion. <a href="faq">View FAQ</a></p>';
		break;
	case 'most-games':
		$type = ToplistManager::TYPE_GAMES;
#		$messageBoxContent = '						<p>Champion Enthusiasts are summoners who are dedicated to a single champion. This toplist features Champion Enthusiasts who have played the most ranked games with a single champion in the current season.</p>';
		break;
	case 'champion-mastery':
		$type = ToplistManager::TYPE_CHAMPIONMASTERY;
#		$messageBoxContent = '						<p>This toplist shows off the highest champion mastery scores!</p>';
		break;
}

$filter = [];
if($filterChampion!=null){
	$filter['champion'] = $filterChampion;
}
if($filterPlatform!=null){
	$filter['platform'] = $sh->getPlatform($filterPlatform);
}

$page = ((isset($_GET['p']))&&($_GET['p']!='')) ? intval($_GET['p']) : 1;
$limit = 30;
$offset = ($page * $limit) - $limit;
$total = $tm->getTotal($type, $filter);

$paginator = new Paginator();
$paginator->setTotalPages(ceil($total / $limit));
$paginator->setCurrentPage($page);
$paginator->setPaginationVariance(5);
$paginator->setLink('top/'.$currentContent.'/page-%p?filterChampion='.$filterChampion.'&amp;filterPlatform='.$filterPlatform);
$paginator->setShowFirst(true);
$paginator->setShowPrevious(true);
$paginator->setShowNext(true);
$pagination = $paginator->paginate();

$meta_title = 'Toplists &ndash; LoLSkill';
$meta_description = 'Check out toplists of the world\'s best League of Legends players and their best champions!';
$template_og['url'] = DYNAMIC_PATH.'/top/'.$currentContent.'/page-'.$page.'?filterChampion='.$filterChampion.'&amp;filterPlatform='.$filterPlatform;
$template_og['title'] = 'Toplist &ndash; LoLSkill';
$template_og['description'] = 'Check out toplists of the world\'s best League of Legends players and their best champions!';

$template_content = $am->getLeaderboard();
$template_content.= '<section id="toplist">
	<div class="container">';
$template_content.= $contentSelect;
$template_content.= '	</div>
	<div class="container filter">
		<form id="form-filter-toplist" class="form-inline" action="top/'.$currentContent.'" method="get">
			<div class="container">
				<div class="row w-100">
					<div class="col-12 col-sm-6 col-md-4 mb-1 mb-sm-0">
						<select id="filterChampion" name="filterChampion" class="form-control mb-3 mr-sm-3 mb-sm-0">
							<option value="">All Champions</option>';
foreach($ch->getChampions() as $champion){
	$template_content.= '							<option value="'.$champion.'"'.(($filterChampion==$champion) ? ' selected' : '').'>'.$ch->getDisplayName($champion).'</option>';
}
$template_content.= '						</select>
					</div>
					<div class="col-12 col-sm-6 col-md-4 mb-1 mb-sm-3 mb-md-0">
						<select id="filterPlatform" name="filterPlatform" class="form-control mb-3 mr-sm-3 mb-sm-0">
							<option value="">All Regions</option>
							<option value="EUW" '.(($filterPlatform=='EUW') ? 'selected' : '').'>EU West</option>
							<option value="EUNE" '.(($filterPlatform=='EUNE') ? 'selected' : '').'>EU Nordic & East</option>
							<option value="NA" '.(($filterPlatform=='NA') ? 'selected' : '').'>North America</option>
							<option value="BR" '.(($filterPlatform=='BR') ? 'selected' : '').'>Brazil</option>
							<option value="KR" '.(($filterPlatform=='KR') ? 'selected' : '').'>Korea</option>
							<option value="TR" '.(($filterPlatform=='TR') ? 'selected' : '').'>Turkey</option>
							<option value="RU" '.(($filterPlatform=='RU') ? 'selected' : '').'>Russia</option>
							<option value="LAN" '.(($filterPlatform=='LAN') ? 'selected' : '').'>Latin America North</option>
							<option value="LAS" '.(($filterPlatform=='LAS') ? 'selected' : '').'>Latin America South</option>
							<option value="OCE" '.(($filterPlatform=='OCE') ? 'selected' : '').'>Oceania</option>
							<option value="JP" '.(($filterPlatform=='JP') ? 'selected' : '').'>Japan</option>
						</select>
					</div>
					<div class="col-12 col-md-4">
						<button type="submit" class="btn btn-outline-primary">Apply</button>
					</div>
				</div>
			</div>
		</form>
	</div>
	<div class="container">';

$data = $tm->getData($type, $offset, $limit, $filter);
$template_content.= $pagination;
$template_content.= $tm->getPrintableTable($data, ($page-1) * $limit +1);
$template_content.= $pagination;

$template_content.= '			</div>
		</section>';
$template_content.= $am->getLeaderboard();

include_once(ABSOLUTE_PATH.'/template.php');
?>