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

$maestroID = $_POST["maestroID"];
$playerID = $_POST["playerID"];
$writingID = $_POST["writingID"];
$record = $_POST["record"];
$isPrevHourFlag = $_POST["isPrevHourFlag"];
// $isPrevHourFlag is for only TDD (addPrevHourTypingExamRecord)

include "./../lib/json_builder.php";
include "./../lib/connect_db.php";

$jsonBuilder = new JsonBuilder();
$dbConnector = new DBConnector();

$isConnected = $dbConnector->connectDB();
if (!$isConnected) {
	$jsonBuilder->setErrorMessage("DB connection : failed");
	$jsonBuilder->sendResultFail();
	exit;
}


include "./../db/db_method_container.php";
include "./../db/typing_exam_collection.php";

$typingExam = new TypingExamCollection($dbConnector->getMysqli());

if (isset($isPrevHourFlag)) {
	addPrevHourRecord($maestroID, $playerID, $writingID, $record);
	if($record >= 0) {
		updateHighestRecord($maestroID, $playerID, $writingID, $record);
	}
	$jsonBuilder->sendResultSuccess();
	exit;
}

if($record >= 0) {
	updateRecord($maestroID, $playerID, $writingID, $record);
	updateHighestRecord($maestroID, $playerID, $writingID, $record);
} else {
	updateMinusRecord($maestroID, $playerID, $writingID, $record);
}

$jsonBuilder->sendResultSuccess();




function addPrevHourRecord($maestroID, $playerID, $writingID, $record)
{
	global $typingExam, $jsonBuilder;

	$typingExam->addPrevHourRecord($maestroID, $playerID, $writingID, $record);
	// $jsonBuilder->setData("prevHourRecord", "added");
}

function updateRecord($maestroID, $playerID, $writingID, $record)
{
	global $typingExam, $jsonBuilder;

	$thisHourRecordData = $typingExam->getThisHourRecord($maestroID, $playerID, $writingID);
	// $jsonBuilder->setData("thisHourRecordData", $thisHourRecordData);
	if ($thisHourRecordData["record"] === null) {
		$typingExam->addRecord($maestroID, $playerID, $writingID, $record);
		// $jsonBuilder->setData("record", "added : ".$record);
	} else {
		if ($record > $thisHourRecordData["record"]) {
			$typingExam->updateRecord($thisHourRecordData["typingExamRecordID"], $record);
			// $jsonBuilder->setData("record", "updated : ".$record);
		}
	}
}

function updateHighestRecord($maestroID, $playerID, $writingID, $record)
{
	global $typingExam, $jsonBuilder;

	$highestRecordData = $typingExam->getHighestRecord($maestroID, $playerID, $writingID);
	// $jsonBuilder->setData("highestRecordData", $highestRecordData);
	if ($highestRecordData["highestRecord"] === null) {
		$typingExam->addHighestRecord($maestroID, $playerID, $writingID, $record);
		// $jsonBuilder->setData("highestRecord", "added : ".$record);
	} else {
		if ($record > $highestRecordData["highestRecord"]) {
			$typingExam->updateHighestRecord($highestRecordData["typingExamHighestRecordID"], $record);
			// $jsonBuilder->setData("highestRecord", "updated : ".$record);
		}
	}
}

function updateMinusRecord($maestroID, $playerID, $writingID, $minusRecord)
{
	global $typingExam, $jsonBuilder;

	$record = -1 * $minusRecord;
	$thisHourRecordData = $typingExam->getThisHourRecord($maestroID, $playerID, $writingID);
	// $jsonBuilder->setData("thisHourRecordData", $thisHourRecordData);
	if ($thisHourRecordData["record"] === null) {
		$typingExam->addRecord($maestroID, $playerID, $writingID, $minusRecord);
		// $jsonBuilder->setData("record", "added : ".$record);
	} else {
		if($thisHourRecordData["record"] < 0) {
			if ($minusRecord < $thisHourRecordData["record"]) {	// ex : -300 < -100
				$typingExam->updateRecord($thisHourRecordData["typingExamRecordID"], $minusRecord);
				// $jsonBuilder->setData("record", "updated : ".$record);
			}
		}

		// if $thisHourRecordData["record"] >= 0 then do nothing
	}
}
?>