<?php
class WritingCollection extends DBMethodContainer
{
	private function getLanguage($languageCode)
	{
		if(intval($languageCode) === 0)
			return "korean";

		return "english";
	}

	public function getSystemList($languageCode)
	{
		$language = $this->getLanguage($languageCode);

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

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

		return $writingList;
	}

	public function getActiveSystemList($maestroID, $languageCode)
	{
		$language = $this->getLanguage($languageCode);

		$query = "
			SELECT WritingID
			FROM writing
			WHERE Language=? AND ActivateStatus=1";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("s", $language);
		$stmt->execute();
		$stmt->bind_result($writingID);

		$writingList = array();
		while($stmt->fetch()) {
			$writingData = array();
		    $writingData["writingID"] = $writingID;
			array_push($writingList, $writingData);
		}
		$stmt->close();

		return $writingList;
	}

	public function getWritingHighestRecord($maestroID, $playerID, $writingID)
	{
		$query = "
			SELECT WritingID, HighestRecord
			FROM typing_exam_highest_record
			WHERE MaestroID=? AND PlayerID=? AND WritingID=?";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("iii", $maestroID, $playerID, $writingID);
		$stmt->execute();
		$stmt->bind_result($writingID, $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;
	}

}
?>