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

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

$maestro_id = $_POST["MaestroID"];
$start_date = $_POST["StartDate"];
$end_date = $_POST["EndDate"];
$player_name_list = $_POST["PlayerNameList"];
$app_id = $_POST["AppID"];
$limit_count = $_POST["LimitCount"];
// echo "start_date : ".$start_date."\n";
// echo "end_date : ".$end_date."\n";
// echo "player_name_list : ".$player_name_list."\n";
// echo "app_id : ".$app_id."\n";
// echo "limit_count : ".$limit_count."\n";
// echo "\n";

$date_statement = makeDateStatement($start_date, $end_date);
$player_name_statement = makePlayerNameStatement($player_name_list);
$subject_statement = makeSubjectSentence($app_id);
$where_statement = make_where_statement($date_statement, $player_name_statement, $subject_statement);
// echo "maestro : ".$maestro_id."\n";
// echo "date : ".$date_statement."\n";
// echo "player_name : ".$player_name_statement."\n";
// echo "subject : ".$subject_statement."\n";
// echo "where_statement : ".$where_statement."\n";

$recordTotalCount = get_player_record_total_count($maestro_id, $where_statement);
// echo "recordTotalCount : ".$recordTotalCount."\n";
$replyJSON = get_player_record_list($maestro_id, $where_statement, $limit_count);
if($replyJSON.length === 0) {
	send_error_message($replyJSON, "오늘의 랭킹 기록 없음");
	$db_conn->close();
	exit;
}

set_data("RecordTotalCount", $recordTotalCount);
set_data("RecordList", $replyJSON);
send_result_success();
exit;




function makeDateStatement($start_date, $end_date) {
	return " '".$start_date."' <= TE.RecordDateTime AND TE.RecordDateTime <= DATE_ADD('".$end_date."', INTERVAL 1 DAY)";
	// return " '".$start_date."' <= TE.RecordDateTime AND TE.RecordDateTime <= '".$end_date."'";
	// return " '2017-01-01' < TE.RecordDateTime";
	// return " '2017-01-01' < TE.RecordDateTime AND TE.RecordDateTime < '".$end_date."'";
}

function makeSubjectSentence($app_id) {
	if($app_id == 0)
		return "";

	$subject_statement = "";
	if(is_subject_bundle_item($app_id)) {
		switch($app_id) {
			case 1004:	// korean exam
			$subject_statement = $subject_statement."WR.Language='korean'";
			break;

			case 1005:	// english exam
			$subject_statement = $subject_statement."WR.Language='english'";
			break;
		}
	} else {
		$subject_statement = $subject_statement."WR.WritingID=".$app_id;
	}

	return $subject_statement." AND TE.WritingID=WR.WritingID";
}

function makePlayerNameStatement($player_name_list) {
	$player_name_statement = "TE.PlayerID=P.PlayerID";
	if(strlen($player_name_list) > 0)
		$player_name_statement = $player_name_statement." AND P.Name LIKE '%".$player_name_list."%'";

	return $player_name_statement;
}


function is_subject_bundle_item($app_id) {
	if($app_id >= 1000)
		return true;

	return false;
}

function make_where_statement($date_statement, $player_name_statement, $subject_statement) {
	// add date condition
	$where_statement = $date_statement;
	if(strlen($where_statement) > 0 and strlen($player_name_statement) > 0)
		$where_statement = $where_statement." AND ";

	// add player name condition
	$where_statement = $where_statement.$player_name_statement;

	// add subject condition
	if(strlen($where_statement) > 0 and strlen($subject_statement) > 0)
		$where_statement = $where_statement." AND ";
	$where_statement = $where_statement.$subject_statement;

	return $where_statement;
}

function get_player_record_total_count($maestro_id, $where_statement) {
	global $db_conn;

	$query = "
		SELECT COUNT(*)
		FROM typing_exam_record TE, player P, writing WR
		WHERE TE.MaestroID = ? AND ".$where_statement."
	";

	// echo $query;
	// return;

	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestro_id);
	$stmt->execute();
	$stmt->bind_result($record_count);
	$stmt->fetch();
	$stmt->close();

	return $record_count;
}

function get_player_record_list($maestro_id, $where_statement, $limit_count) {
	global $db_conn;

	$query = "
		SELECT DATE(TE.RecordDateTime) AS Date, TIME(TE.RecordDateTime) AS Time,
			P.Name AS Name, WR.Name AS Subject, TE.Record AS Record
		FROM typing_exam_record TE, player P, writing WR
		WHERE TE.MaestroID = ? AND TE.WritingID=WR.WritingID AND TE.PlayerID=P.PlayerID AND ".$where_statement."
		ORDER BY TE.RecordDateTime DESC
	";

	if($limit_count > 0)
		$query = $query." LIMIT ".$limit_count;

	// echo $query;
	// return;

	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('i', $maestro_id);
	$stmt->execute();
	$stmt->bind_result($date, $time, $player_name, $subject, $record);

	$record_array = array();
	while($stmt->fetch()) {
	    $record_date['Date'] = $date;
	    $record_date['Time'] = $time;
	    $record_date['PlayerName'] = $player_name;
	    $record_date['Subject'] = $subject;
	    $record_date['Record'] = $record;
	    array_push($record_array, $record_date);
	}

	return $record_array;
}

?>