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

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

$maestroID = $_POST["maestro_id"];


$mouse_app_list = get_mouse_app_list();
if($mouse_app_list.length === 0) {
	set_error_message("등록된 마우스 앱 없음");
	send_result_fail();
	exit;
}

$activated_mouse_app_list = get_activated_mouse_app_list($maestroID);

set_data("appList", $mouse_app_list);
set_data("activatedAppList", $activated_mouse_app_list);
send_result_success();
exit;



function get_mouse_app_list() {
	global $db_conn;

	$query = "
		SELECT AppID, AppName, KoreanName FROM app
		WHERE AppType=100
		ORDER BY AppID ASC
	";
	$stmt = $db_conn->prepare($query);
	$stmt->execute();
	$stmt->bind_result($appID, $appName, $koreanName);

	$playerList = array();
	while($stmt->fetch()) {
	    $player["appID"] = $appID;
	    $player["appName"] = $appName;
	    $player["koreanName"] = $koreanName;
	    array_push($playerList, $player);
	}

	return $playerList;
}

function get_activated_mouse_app_list($maestroID) {
	global $db_conn;

	$query = "
		SELECT A.AppID
		FROM app AS A
		INNER JOIN active_app AS AA ON A.AppID = AA.AppID AND A.AppType = 100 AND AA.MaestroID=?"
	;
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestroID);
	$stmt->execute();
	$stmt->bind_result($appID);

	$playerList = array();
	while($stmt->fetch()) {
	    $player["appID"] = $appID;
	    array_push($playerList, $player);
	}

	return $playerList;
}

?>