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

// get parameters from client
$command = $_POST["command"];


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;
}


const MAESTRO_ID	= 3;	// 삼화초
const PLAYER_ID		= 35;	// 박지상

const WRITING_ID		= 1;
const WRITING_RECORD	= 123.456;


if ($command === "addTypingExamHighestRecord") {
	include "./db_method_container.php";
	include "./typing_exam_collection.php";

	$typingExam = new TypingExamCollection($dbConnector->getMysqli());
	$typingExam->addHighestRecord(MAESTRO_ID, PLAYER_ID, WRITING_ID, WRITING_RECORD);
	// $typingExamHighestRecordData = $typingExam->getHighestRecord(MAESTRO_ID, PLAYER_ID, WRITING_ID);
	// $jsonBuilder->setData("typingExamHighestRecordData", $typingExamHighestRecordData);
} elseif ($command === "updateTypingExamHighestRecord") {
	include "./db_method_container.php";
	include "./typing_exam_collection.php";

	$typingExam = new TypingExamCollection($dbConnector->getMysqli());
	$typingExamHighestRecordData = $typingExam->getHighestRecord(MAESTRO_ID, PLAYER_ID, WRITING_ID);
	// $jsonBuilder->setData("typingExamHighestRecordData", $typingExamHighestRecordData);
	$typingExam->updateHighestRecord($typingExamHighestRecordData["typingExamHighestRecordID"], WRITING_RECORD + 10);
	// $typingExamHighestRecordData = $typingExam->getHighestRecord(MAESTRO_ID, PLAYER_ID, WRITING_ID);
	// $jsonBuilder->setData("updateTypingExamHighestRecord", $typingExamHighestRecordData);
} elseif ($command === "getTypingExamHigestRecord") {
	include "./db_method_container.php";
	include "./typing_exam_collection.php";

	$typingExam = new TypingExamCollection($dbConnector->getMysqli());
	$typingExamHighestRecordData = $typingExam->getHighestRecord(MAESTRO_ID, PLAYER_ID, WRITING_ID);
	$jsonBuilder->setData("typingExamHighestRecordData", $typingExamHighestRecordData);
}

$jsonBuilder->sendResultSuccess();
?>