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

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

$maestroID = $_POST["maestro_id"];
$playerName = $_POST["player_name"];
$enterCode = $_POST["enter_code"];
/*
if(!is_numeric($enterCode)) {
    send_error_code("생년월일이 숫자가 아닙니다. : ".$enterCode);
    exit;
} else if(strlen($enterCode) != 6) {
    send_error_code("6자리 숫자값을 정확히 입력하세요 : YYMMDD (예 : 120131) : ".$enterCode);
    exit;
}
*/

$result = has_player_name($maestroID, $playerName, $enterCode);
if($result !== null) {
	set_error_message("이미 등록된 학생과 입장 코드 정보입니다.");
	send_result_fail();
	exit;
}

$maestroAccountType = get_maestro_account_type($maestroID);
$playerCount = get_player_count($maestroID);
if($playerCount >= get_max_player_count($maestroAccountType)) {
	set_error_message("학생을 더 이상 추가할 수 없습니다. (더 많은 학생수로 업그레이드 하세요)");
	send_result_fail();
	exit;
}

add_player($maestroID, $playerName, $enterCode);

$result = has_player_name($maestroID, $playerName, $enterCode);
if($result === null) {
	set_error_message("학생 등록에 실패했습니다.");
	send_result_fail();
	exit;
}

// increase_player_count($maestroID, 1);
$count = count_registered_player($maestroID);
update_count_registered_player($maestroID, $count);

set_data("count", $count);
send_result_success();
exit;


function has_player_name($maestroID, $playerName, $enterCode) {
	global $db_conn;

	$query = "
		SELECT PlayerID
		FROM player
		WHERE MaestroID = ? AND Name = ? AND EnterCode = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("iss", $maestroID, $playerName, $enterCode);
	$stmt->execute();
	$stmt->bind_result($playerID);
	// while($stmt->fetch())
	$stmt->fetch();
	$stmt->close();

	return $playerID;
}

function get_maestro_account_type($maestroID) {
	global $db_conn;

	$query = "
		SELECT AccountType
		FROM maestro
		WHERE MaestroID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestroID);
	$stmt->execute();
	$stmt->bind_result($accountType);
	// while($stmt->fetch())
	$stmt->fetch();
	$stmt->close();

	return $accountType;
}

function get_player_count($maestroID) {
	global $db_conn;

	$query = "
		SELECT PlayerCount
		FROM maestro
		WHERE MaestroID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestroID);
	$stmt->execute();
	$stmt->bind_result($playerCount);
	// while($stmt->fetch())
	$stmt->fetch();
	$stmt->close();

	return $playerCount;
}

function add_player($maestroID, $playerName, $enterCode) {
	global $db_conn;

	$query = "
		INSERT INTO player (MaestroID, Name, EnterCode)
		VALUES (?, ?, ?)
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("iss", $maestroID, $playerName, $enterCode);
	$stmt->execute();
}

function increase_player_count($maestroID, $count) {
	global $db_conn;

	$query = "
		UPDATE maestro
		SET PlayerCount = PlayerCount + ?
		WHERE MaestroID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $count, $maestroID);
	$stmt->execute();
}

function count_registered_player($maestroID) {
	global $db_conn;

	$query = "
		SELECT COUNT(PlayerID)
		FROM player
		WHERE MaestroID = ? AND AccountType = 0
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestroID);
	$stmt->execute();
	$stmt->bind_result($countPlayer);
	// while($stmt->fetch())
	$stmt->fetch();
	$stmt->close();

	return $countPlayer;
}

function update_count_registered_player($maestroID, $countPlayer) {
	global $db_conn;

	$query = "
		UPDATE maestro
		SET PlayerCount = ?
		WHERE MaestroID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("ii", $countPlayer, $maestroID);
	$stmt->execute();
}

?>