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

$maestro_id = $_POST["MaestroID"];
$player_id = $_POST["PlayerID"];
$app_id = $_POST["AppID"];
$record = $_POST["Record"];
// echo $record." / ";

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

// best record is for the saved data by every hour
$prev_best_record_id = -1;
$prev_best_record = 0;

$prev_best_record_id = get_best_record($maestro_id, $app_id, $player_id);
echo "id : ".$prev_best_record_id." ".$prev_best_record;

if($prev_best_record_id === null || $prev_best_record_id < 0) {
	echo "insert";
	insert_best_record($maestro_id, $app_id, $player_id, $record);
} else {
	echo $record." ".$prev_best_record;
	if(is_highest_record_prefer_app($app_id)) {
		if($record > $prev_best_record) {
			echo "update";
			update_best_record($prev_best_record_id, $record);
		}
	} else {
		if($record < $prev_best_record) {
			echo "update";
			update_best_record($prev_best_record_id, $record);
		}
	}
}

// highest record is the highest record
$app_highest_record = get_app_highest_record($maestro_id, $app_id, $player_id);
echo "\napp_highest_record : $app_highest_record";

// echo "\nrecord : $record";
if($app_highest_record == null) {
	insert_app_highest_record($maestro_id, $app_id, $player_id, $record);
	// echo "\ninsert_app_highest_record : $record";
} else {
	if(is_highest_record_prefer_app($app_id)) {
		if($record > $app_highest_record) {
			remove_app_highest_record($maestro_id, $app_id, $player_id);
			// echo "\nremove_app_highest_record";
			insert_app_highest_record($maestro_id, $app_id, $player_id, $record);
			// echo "\ninsert_app_highest_record : $record";
		}
	} else {
		if($record < $app_highest_record) {
			remove_app_highest_record($maestro_id, $app_id, $player_id);
			// echo "\nremove_app_highest_record";
			insert_app_highest_record($maestro_id, $app_id, $player_id, $record);
			// echo "\ninsert_app_highest_record : $record";
		}
	}
}

$db_conn->close();


function get_best_record($maestro_id, $app_id, $player_id) {
	global $db_conn;
	global $prev_best_record_id, $prev_best_record;

	$query = "
		SELECT BestRecordID, BestRecord
		FROM best_record
		WHERE MaestroID = ? AND AppID = ? AND PlayerID = ? AND DATE(RecordDateTime) = DATE(NOW())
		AND HOUR(RecordDateTime) = HOUR(NOW())
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("iii", $maestro_id, $app_id, $player_id);
	$stmt->execute();
	$stmt->bind_result($prev_best_record_id, $prev_best_record);
	$stmt->fetch();
	$stmt->close();

	$GLOBALS["prev_best_record_id"] = $prev_best_record_id;
	$GLOBALS["prev_best_record"] = $prev_best_record;

	return $prev_best_record_id;
}


function insert_best_record($maestro_id, $app_id, $player_id, $record) {
	global $db_conn;

	$query = "
		INSERT INTO best_record (MaestroID, PlayerID, AppID, BestRecord, RecordDateTime)
		VALUES (?, ?, ?, ?, NOW());
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('iiid', $maestro_id, $player_id, $app_id, $record);
	$stmt->execute();
}

function update_best_record($prev_best_record_id, $record) {
	global $db_conn;

	$query = "
		UPDATE best_record
		SET BestRecord = ?, RecordDateTime = NOW()
		WHERE BestRecordID = ? AND DATE(RecordDateTime) = DATE(NOW())
		AND HOUR(RecordDateTime) = HOUR(NOW())
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('di', $record, $prev_best_record_id);
	$stmt->execute();
}

?>