<?php
class MenuCollection extends DBMethodContainer
{
	private $writingCollection = null;


	public function getAppGroupName($appGroupCode)
	{
		switch(intval($appGroupCode)) {
			case 0:
			return "typing practice";

			case 1:
			return "typing test";

			case 2:
			return "typing exam";

			case 3:
			return "typing play";

			case 4:
			return "mouse play";
		}
	}

	public function getLanguageName($language)
	{
		switch(intval($language)) {
			case 0:
			return "Korean";

			case 1:
			return "English";
		}
	}

	public function getTypingAppType($appGroupCode, $language)
	{
		switch(intval($appGroupCode)) {
			case 0:	// typing practice"
			if(intval($language) === 0)	// Korean
				return 1;
			else	// English
				return 2;

			case 1:	// typing test
			if(intval($language) === 0)	// Korean
				return 11;
			else	// English
				return 12;

			case 3:	// typing play
			if(intval($language) === 0)	// Korean
				return 51;
			else	// English
				return 52;

			default:
				return -1;
		}
	}



	// app list
	public function getAppList($maestroID, $playerID, $appGroup, $language)
	{
		switch($appGroup) {
			case 0:	// typing practice
			case 1:	// typing test
			case 3:	// typing play
			return $this->getTypingAppList($maestroID, $playerID, $appGroup, $language);

			case 2:
			if($writingCollection === null)
				$writingCollection = new WritingCollection($this->mysqli);
			$systemWritingList = $writingCollection->getSystemList($language);
			return $systemWritingList;

			case 4:	// mouse play
			return $this->getMouseAppList();
		}
	}

	public function getTypingAppList($maestroID, $playerID, $appGroup, $language)
	{
		$query = "
			SELECT AppID, AppName, KoreanName
			FROM app
			WHERE AppType=? AND Status=1";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("i", $this->getTypingAppType($appGroup, $language));
		$stmt->execute();
		$stmt->bind_result($appID, $appName, $koreanName);

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

		return $resultArray;
	}

	public function getMouseAppList()
	{
		$query = "
			SELECT AppID, AppName, KoreanName
			FROM app
			WHERE AppType=100 AND Status=1";
		$stmt = $this->mysqli->prepare($query);
		$stmt->execute();
		$stmt->bind_result($appID, $appName, $koreanName);

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

		return $resultArray;
	}



	// active app list
	public function getActiveAppList($maestroID, $playerID, $appGroup, $language)
	{
		switch($appGroup) {
			case 0:	// typing practice
			case 1:	// typing test
			case 3:	// typing play
			return $this->getTypingActiveAppList($maestroID, $playerID, $appGroup, $language);

			case 2:
			if($writingCollection === null)
				$writingCollection = new WritingCollection($this->mysqli);
			$activeSystemWritingList = $writingCollection->getActiveSystemList($maestroID, $language);
			return $activeSystemWritingList;

			case 4:	// mouse play
			return $this->getMouseActiveAppList($maestroID, $playerID, $appGroup);
		}
	}

	public function getTypingActiveAppList($maestroID, $playerID, $appGroup, $language)
	{
		$query = "
			SELECT A.AppID
			FROM app AS A
			INNER JOIN active_app AS AA ON A.AppID = AA.AppID AND A.AppType=? AND AA.MaestroID=?";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("ii", $this->getTypingAppType($appGroup, $language), $maestroID);
		$stmt->execute();
		$stmt->bind_result($appID);

		$resultArray = array();
		while($stmt->fetch()) {
			$rowArray = array();
		    $rowArray["appID"] = $appID;
			array_push($resultArray, $rowArray);
		}
		$stmt->close();

		return $resultArray;
	}

	public function getMouseActiveAppList($maestroID)
	{
		$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 = $this->mysqli->prepare($query);
		$stmt->bind_param("i", $maestroID);
		$stmt->execute();
		$stmt->bind_result($appID);

		$resultArray = array();
		while($stmt->fetch()) {
			$rowArray = array();
		    $rowArray["appID"] = $appID;
			array_push($resultArray, $rowArray);
		}
		$stmt->close();

		return $resultArray;
	}



	// highest record list
	public function getHighestRecordList($maestroID, $playerID, $appList)
	{
		switch($appGroup) {
			case 0:	// typing practice
			case 1:	// typing test
			case 3:	// typing play
			case 4:	// mouse play
			return $this->getTypingHighestRecordList($maestroID, $playerID, $appList);

			// case 2:
			// return $this->getWritingHighestRecordList($maestroID, $playerID, $appList);
		}
	}

	public function getWritingHighestRecordList($maestroID, $playerID, $appList)
	{
		if($writingCollection === null)
			$writingCollection = new WritingCollection($this->mysqli);

		$highestRecordArray = array();
		$count = count($appList);
		for($i = 0; $i < $count; $i++) {
			$writingID = $appList[$i]["writingID"];
			$highestRecord["writingID"] = $writingID;
			$highestRecord["highestRecord"] = $writingCollection->getWritingHighestRecord($maestroID, $playerID, $writingID);
			array_push($highestRecordArray, $highestRecord);
		}

		return $highestRecordArray;
	}

	public function getTypingHighestRecordList($maestroID, $playerID, $appList)
	{
		$highestRecordArray = array();
		$count = count($appList);
		for($i = 0; $i < $count; $i++) {
			$appID = $appList[$i]["appID"];
			$highestRecord["appID"] = $appID;
			$highestRecord["highestRecord"] = $this->getTypingHighestRecord($maestroID, $playerID, $appID);
			array_push($highestRecordArray, $highestRecord);

		}
		return $highestRecordArray;
	}

	public function getTypingHighestRecord($maestroID, $playerID, $appID)
	{
		$query = "
			SELECT HighestRecord
			FROM app_highest_record
			WHERE MaestroID=? AND PlayerID=? AND AppID=?";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("iii", $maestroID, $playerID, $appID);
		$stmt->execute();
		$stmt->bind_result($highestRecord);
		$returnValue = $stmt->fetch();
		$stmt->close();

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

		return $highestRecord;
	}




	/*
	public function getSystemWritingList()
	{
		$writer = "system";

		$query = "
			SELECT WritingID, Language, Name, Filename, LetterCount
			FROM writing
			WHERE Writer = ?";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("s", $writer);
		$stmt->execute();
		$stmt->bind_result($writingID, $language, $name, $filename, $letterCount);

		$resultArray = array();
		while($stmt->fetch()) {
			$rowArray = array();
		    $rowArray["writingID"] = $writingID;
		    $rowArray["language"] = $language;
		    $rowArray["name"] = $name;
		    $rowArray["filename"] = $filename;
		    $rowArray["letterCount"] = $letterCount;
			array_push($resultArray, $rowArray);
		}
		$stmt->close();

		return $resultArray;
	}

	public function getWritingInfo($writingID)
	{
		$query = "
			SELECT Name, Filename, Writer, WriterID
			FROM writing
			WHERE WritingID = ?";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("i", $writingID);
		$stmt->execute();
		$stmt->bind_result($name, $filename, $writer, $writerID);

		$stmt->fetch();
		$stmt->close();

		$rowArray = array();
		$rowArray["name"] = $name;
		$rowArray["filename"] = $filename;
		$rowArray["writer"] = $writer;
		$rowArray["writerID"] = $writerID;

		return $rowArray;
	}
	*/

	public function getAllowEditEntercode($maestroID) {
		$query = "SELECT AllowEditEnterCode FROM maestro WHERE MaestroID = ?";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("i", $maestroID);
		$stmt->execute();
		$stmt->bind_result($allowEditEntercode);

		$stmt->fetch();
		$stmt->close();

		return $allowEditEntercode;
	}
}
?>