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

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


$maestro_experience_id = get_maestro_experience_id();
if($maestro_experience_id === null) {
	set_error_message("등록된 마에스트로 관리자 계정이 없습니다.");
	send_result_fail();
	exit;
}

$maestro_test_player_id = get_maestro_test_player_id($maestro_experience_id);
if($maestro_test_player_id === null) {
	set_error_message("등록된 마에스트로 테스트 계정이 없습니다. (".$maestro_experience_id.")");
	send_result_fail();
	exit;
}

$maestro_test_player_name = get_maestro_test_player_name($maestro_test_player_id);

reset_maestro_sample_player_data($maestro_experience_id);

$count = count_registered_player($maestro_experience_id);
update_count_registered_player($maestro_experience_id, $count);

reset_maestro_active_app_data($maestro_experience_id);

set_data("maestroID", $maestro_experience_id);
set_data("playerID", $maestro_test_player_id);
set_data("playerName", $maestro_test_player_name);
send_result_success();
exit;


function get_maestro_experience_id() {
	global $db_conn;

	$query = "
		SELECT MaestroID
		FROM maestro
		WHERE AccountType = 101
	";
	$stmt = $db_conn->prepare($query);
	// $stmt->bind_param("s", $maestro_name);
	$stmt->execute();
	$stmt->bind_result($maestro_experience_id);
	// while($stmt->fetch()) {
	// 	;
	// }
	$stmt->fetch();
	$stmt->close();

	return $maestro_experience_id;
}

function get_maestro_test_player_id($maestro_id) {
	global $db_conn;

	$query = "
		SELECT MaestroTestID
		FROM maestro
		WHERE MaestroID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestro_id);
	$stmt->execute();
	$stmt->bind_result($maestro_test_player_id);
	// while($stmt->fetch()) {
	// 	;
	// }
	$stmt->fetch();
	$stmt->close();

	return $maestro_test_player_id;
}

function get_maestro_test_player_name($player_id) {
	global $db_conn;

	$query = "
		SELECT Name
		FROM player
		WHERE PlayerID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $player_id);
	$stmt->execute();
	$stmt->bind_result($maestro_test_player_name);
	// while($stmt->fetch()) {
	// 	;
	// }
	$stmt->fetch();
	$stmt->close();

	return $maestro_test_player_name;
}

function reset_maestro_active_app_data($maestro_id) {
	remove_maestro_active_app_data($maestro_id);
	add_maestro_active_app_data($maestro_id);
}

function remove_maestro_active_app_data($maestro_id) {
	global $db_conn;

	$query = "
		DELETE
		FROM active_app
		WHERE MaestroID = ?
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestro_id);
	$stmt->execute();
	$stmt->close();
}

function add_maestro_active_app_data($maestro_id) {
	global $db_conn;

	$query = "
		INSERT INTO active_app (MaestroID, AppID)
		SELECT ?, app.AppID
		FROM app
		WHERE app.Status = 1;
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestro_id);
	$stmt->execute();
	$stmt->close();
}


function reset_maestro_sample_player_data($maestro_id) {
	remove_maestro_sample_player_data($maestro_id);
	add_maestro_sample_player_data($maestro_id);
}

function remove_maestro_sample_player_data($maestro_id) {
	global $db_conn;

	$query = "
		DELETE
		FROM player
		WHERE MaestroID = ? AND AccountType = 0
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestro_id);
	$stmt->execute();
	$stmt->close();
}

function add_maestro_sample_player_data($maestro_id) {
	$samplePlayerList = make_sample_player_list($maestro_id);
	for($i = 0; $i < count($samplePlayerList); $i++) {
		insert_maestro_sample_player_data($maestro_id, $samplePlayerList[$i]);
	}
}


function insert_maestro_sample_player_data($maestro_id, $player) {
	global $db_conn;

	$query = "
		INSERT INTO player (MaestroID, Name, EnterCode, AccountType)
		VALUES (?, ?, ?, ?)
	";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("issi",
		$maestro_id,
		$player["Name"],
		$player["EnterCode"],
		$player["AccountType"]
	);
	$stmt->execute();
	$stmt->close();
}

function make_sample_player_list() {
	$samplePlayerList = array();

	$player["Name"] = "050215";
	$player["EnterCode"] = "050215";
	$player["AccountType"] = 0;
	array_push($samplePlayerList, $player);

	$player["Name"] = "6-3_이철수";
	$player["EnterCode"] = "1111";
	$player["AccountType"] = 0;
	array_push($samplePlayerList, $player);

	$player["Name"] = "3-1김영희";
	$player["EnterCode"] = "0301";
	$player["AccountType"] = 0;
	array_push($samplePlayerList, $player);

	$player["Name"] = "홍길동";
	$player["EnterCode"] = "181231";
	$player["AccountType"] = 0;
	array_push($samplePlayerList, $player);

	return $samplePlayerList;
}

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

?>