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

$maestro_id = $_POST["maestro_id"];

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

$mouseAppList = get_app_list(100);
$typingAppList = get_typing_app_list();
$mouseActiveAppList = get_active_mouse_app_list($maestro_id);
$typingActiveAppList = get_active_typing_app_list($maestro_id);
$typingPracticeAppCount = get_typing_practice_app_count($maestro_id);
$typingTestAppCount = get_typing_test_app_count($maestro_id);

if($replyJSON.length === 0) {
	set_error_code("no_app_list");
	set_error_message("앱 목록을 가져오지 못했습니다.");
	send_result_fail();
	exit;
}

set_data("MouseAppList", $mouseAppList);
set_data("TypingAppList", $typingAppList);
set_data("MouseActiveAppList", $mouseActiveAppList);
set_data("TypingActiveAppList", $typingActiveAppList);
set_data("TypingPracticeAppCount", $typingPracticeAppCount);
set_data("TypingTestAppCount", $typingTestAppCount);
send_result_success();
exit;



function get_app_list($appType) {
	global $db_conn;

	$query = "
		SELECT AppID, AppName, KoreanName
		FROM app
		WHERE AppType = ? AND Status = 1
		ORDER BY AppID ASC";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param('i', $appType);
	$stmt->execute();
	$stmt->bind_result($app_id, $app_name, $korean_name);

	$return_array = array();
	while($stmt->fetch()) {
	    $row_array['AppID'] = $app_id;
	    $row_array['AppName'] = $app_name;
	    $row_array['KoreanName'] = $korean_name;
		array_push($return_array, $row_array);
	}
	$stmt->close();

	return $return_array;
}

function get_typing_app_list() {
	global $db_conn;

	$query = "
		SELECT AppID, AppName, KoreanName
		FROM app
		WHERE AppType > 50 AND AppType < 100 AND Status = 1
		ORDER BY AppID ASC";
	$stmt = $db_conn->prepare($query);
	// $stmt->bind_param('i', $appType);
	$stmt->execute();
	$stmt->bind_result($app_id, $app_name, $korean_name);

	$return_array = array();
	while($stmt->fetch()) {
	    $row_array['AppID'] = $app_id;
	    $row_array['AppName'] = $app_name;
	    $row_array['KoreanName'] = $korean_name;
		array_push($return_array, $row_array);
	}
	$stmt->close();

	return $return_array;
}

function get_active_mouse_app_list($maestro_id) {
	global $db_conn;

	$query = "
		SELECT A.AppID, A.AppName, A.KoreanName, A.AppType
		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", $maestro_id);
	$stmt->execute();
	$stmt->bind_result($app_id, $app_name, $korean_name, $app_type);

	$return_array = array();
	while($stmt->fetch()) {
	    $row_array["AppID"] = $app_id;
	    $row_array["AppName"] = $app_name;
	    $row_array["KoreanName"] = $korean_name;
	    $row_array["AppType"] = $app_type;
		array_push($return_array, $row_array);
	}
	$stmt->close();

	return $return_array;
}

function get_active_typing_app_list($maestro_id) {
	global $db_conn;

	$query = "
		SELECT A.AppID, A.AppName, A.KoreanName, A.AppType
		FROM app AS A
		INNER JOIN active_app AS AA ON A.AppID = AA.AppID AND A.AppType > 50 AND A.AppType < 100 AND AA.MaestroID=?";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestro_id);
	$stmt->execute();
	$stmt->bind_result($app_id, $app_name, $korean_name, $app_type);

	$return_array = array();
	while($stmt->fetch()) {
	    $row_array["AppID"] = $app_id;
	    $row_array["AppName"] = $app_name;
	    $row_array["KoreanName"] = $korean_name;
	    $row_array["AppType"] = $app_type;
		array_push($return_array, $row_array);
	}
	$stmt->close();

	return $return_array;
}

function get_typing_practice_app_count($maestro_id) {
	global $db_conn;

	$query = "
		SELECT COUNT(*)
		FROM app AS A
		INNER JOIN active_app AS AA ON A.AppID = AA.AppID AND A.AppType <= 2 AND AA.MaestroID=?";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestro_id);
	$stmt->execute();
	$stmt->bind_result($typing_practice_app_count);
	$stmt->fetch();
	$stmt->close();

	return $typing_practice_app_count;
}

function get_typing_test_app_count($maestro_id) {
	global $db_conn;

	$query = "
		SELECT COUNT(*)
		FROM app AS A
		INNER JOIN active_app AS AA ON A.AppID = AA.AppID AND A.AppType > 10 AND A.AppType <= 12 AND AA.MaestroID=?";
	$stmt = $db_conn->prepare($query);
	$stmt->bind_param("i", $maestro_id);
	$stmt->execute();
	$stmt->bind_result($typing_practice_app_count);
	$stmt->fetch();
	$stmt->close();

	return $typing_practice_app_count;
}

?>