<?php
header('Content-Type: application/json');

include "./../lib/send_reply_json.php";
include "./../setup/connect_db.php";
include "./../lib/util_app.php";

$maestroID = $_POST["MaestroID"];
$appID = $_POST["AppID"];


$rankingHour = get_ranking_hour($maestroID, $appID);
$rankingDay = get_ranking_day($maestroID, $appID);
$rankingMonth = get_ranking_month($maestroID, $appID);

set_data("rankingHour", $rankingHour);
set_data("rankingDay", $rankingDay);
set_data("rankingMonth", $rankingMonth);
send_result_success();
exit;


function get_ranking_hour($maestroID, $appID) {
	global $db_conn;

	$query = "
		FROM best_record BR, player P
		WHERE BR.PlayerID = P.PlayerID AND DATE(BR.RecordDateTime) = DATE(NOW()) AND HOUR(BR.RecordDateTime) = HOUR(NOW()) AND BR.MaestroID = ? AND BR.AppID = ?
		GROUP BY BR.PlayerID
	";
	if(is_highest_record_prefer_app($appID)) {
		$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MAX(BR.BestRecord) AS BestRecord".$query;
		$query = $query."ORDER BY max(BR.BestRecord) DESC;";
	} else {
		$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MIN(BR.BestRecord) AS BestRecord".$query;
		$query = $query."ORDER BY min(BR.BestRecord) ASC;";
	}
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('ii', $maestroID, $appID);
	$stmt->execute();
	$stmt->bind_result($playerID, $name, $bestRecord);

	$bestRecordArray = array();
	while($stmt->fetch()) {
		$best_record['PlayerID'] = $playerID;
		$best_record['Name'] = $name;
		$best_record['BestRecord'] = $bestRecord;
		array_push($bestRecordArray, $best_record);
	}
	return $bestRecordArray;
}

function get_ranking_day($maestroID, $appID) {
	global $db_conn;

	$query = "
		FROM best_record BR, player P
		WHERE BR.PlayerID = P.PlayerID AND DATE(BR.RecordDateTime) = DATE(NOW()) AND BR.MaestroID = ? AND BR.AppID = ?
		GROUP BY BR.PlayerID
	";
	if(is_highest_record_prefer_app($appID)) {
		$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MAX(BR.BestRecord) AS BestRecord".$query;
		$query = $query."ORDER BY max(BR.BestRecord) DESC;";
	} else {
		$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MIN(BR.BestRecord) AS BestRecord".$query;
		$query = $query."ORDER BY min(BR.BestRecord) ASC;";
	}
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('ii', $maestroID, $appID);
	$stmt->execute();
	$stmt->bind_result($playerID, $name, $bestRecord);

	$bestRecordArray = array();
	while($stmt->fetch()) {
		$best_record['PlayerID'] = $playerID;
		$best_record['Name'] = $name;
		$best_record['BestRecord'] = $bestRecord;
		array_push($bestRecordArray, $best_record);
	}
	return $bestRecordArray;
}

function get_ranking_month($maestroID, $appID) {
	global $db_conn;

	$query = "
		FROM best_record BR, player P
		WHERE BR.PlayerID = P.PlayerID AND MONTH(BR.RecordDateTime) = MONTH(NOW()) AND BR.MaestroID = ? AND BR.AppID = ?
		GROUP BY BR.PlayerID
	";
	if(is_highest_record_prefer_app($appID)) {
		$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MAX(BR.BestRecord) AS BestRecord".$query;
		$query = $query."ORDER BY max(BR.BestRecord) DESC;";
	} else {
		$query = "SELECT BR.PlayerID As PlayerID, P.Name AS Name, MIN(BR.BestRecord) AS BestRecord".$query;
		$query = $query."ORDER BY min(BR.BestRecord) ASC;";
	}
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('ii', $maestroID, $appID);
	$stmt->execute();
	$stmt->bind_result($playerID, $name, $bestRecord);

	$bestRecordArray = array();
	while($stmt->fetch()) {
		$best_record['PlayerID'] = $playerID;
		$best_record['Name'] = $name;
		$best_record['BestRecord'] = $bestRecord;
		array_push($bestRecordArray, $best_record);
	}
	return $bestRecordArray;
}

?>