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

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

$maestroName = $_POST["maestro_name"];
$name = $_POST["name"];
$enterCode = $_POST["enter_code"];

if($enterCode === "" || strlen($enterCode) < 2) {
	set_error_message("입장번호는 2개 이상의 숫자로 입력해야 합니다.");
	send_result_fail();
	exit;
}

if(strlen($enterCode) > 8) {
	set_error_message("입장번호는 8개 이하의 숫자로 입력해야 합니다.");
	send_result_fail();
	exit;
}

$maestroInfo = get_maestro_id($maestroName);
if($maestroInfo === NULL) {
	set_error_message("입력한 마에스트로 계정이 없습니다");
	send_result_fail();
	exit;
}

$maestroID = $maestroInfo["maestroID"];
$maestroAccountType = $maestroInfo["accountType"];
$activateStatus = (int)$maestroInfo["activateStatus"];
if($activateStatus == 0) {
	set_error_message("아직 유료 가입하지 않은 마에스트로 계정입니다.");
	send_result_fail();
	exit;
} else if($activateStatus == 100) {
	set_error_message("죄송합니다. 등록이 취소된 마에스트로 계정입니다.");
	send_result_fail();
	exit;
}

$date = new DateTime($maestroInfo["availableActivateDateTime"]);
$now = new DateTime();
if($maestroInfo["activateStatus"] === 1 && $date < $now) {
	set_error_message("마에스트로 계정의 유효기간이 지났습니다.\n(만료일 : ".$date->format("Y-m-d").")\n선생님께 문의하세요.");
	send_result_fail();
	exit;
}

$playerInfo = get_login_data($maestroID, $name, $enterCode);
if($playerInfo === NULL) {
	set_error_message("입력한 이름 / 입장번호와 일치하는 계정이 없습니다.");
	send_result_fail();
	exit;
}

$playerID = $playerInfo["playerID"];
$playerAccountType = $playerInfo["accountType"];


set_data("MaestroID", $maestroID);
set_data("MaestroAccountType", $maestroAccountType);
set_data("PlayerID", $playerID);
set_data("PlayerName", $name);
set_data("PlayerAccountType", $playerAccountType);

send_result_success();
exit;


function get_maestro_id($maestroName) {
	global $db_conn;

	$query = "
		SELECT MaestroID, AccountType, ActivateStatus, AvailableActivateDateTime
		FROM maestro
		WHERE Name = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('s', $maestroName);
	$stmt->execute();
	$stmt->bind_result($maestroID, $accountType, $activateStatus, $availableActivateDateTime);
	$returnValue = $stmt->fetch();
	$stmt->close();

	if($returnValue === NULL || $returnValue === FALSE)
		return NULL;

	$result["maestroID"] = $maestroID;
	$result["accountType"] = $accountType;
	$result["activateStatus"] = $activateStatus;
	$result["availableActivateDateTime"] = $availableActivateDateTime;
	return $result;
}

function get_login_data($maestroID, $name, $enterCode) {
	global $db_conn;

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

	if($returnValue === NULL || $returnValue === FALSE)
		return NULL;

	$result["playerID"] = $playerID;
	$result["accountType"] = $accountType;
	return $result;
}

?>