<?php
class TypingExamCollection extends DBMethodContainer
{
	// begin - for TDD
	public function deleteTypingExamRecordAll($maestroID, $playerID) {
		$query = "
			DELETE
			FROM typing_exam_record
			WHERE MaestroID = ? AND PlayerID = ?";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param('ii', $maestroID, $playerID);
		$stmt->execute();
		$stmt->fetch();
		$stmt->close();
	}

	public function deleteTypingExamHighestRecordAll($maestroID, $playerID) {
		$query = "
			DELETE
			FROM typing_exam_highest_record
			WHERE MaestroID = ? AND PlayerID = ?";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param('ii', $maestroID, $playerID);
		$stmt->execute();
		$stmt->fetch();
		$stmt->close();
	}

	public function addPrevHourRecord($maestroID, $playerID, $writingID, $record)
	{
		$query = "
			INSERT INTO typing_exam_record (MaestroID, PlayerID, WritingID, Record, RecordDateTime)
			VALUES (?, ?, ?, ?, DATE_SUB(NOW(), INTERVAL 1 HOUR))";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("iiid", $maestroID, $playerID, $writingID, $record);
		$stmt->execute();
		$stmt->close();
	}
	// end - for TDD

	public function addRecord($maestroID, $playerID, $writingID, $record)
	{
		$query = "
			INSERT INTO typing_exam_record (MaestroID, PlayerID, WritingID, Record, RecordDateTime)
			VALUES (?, ?, ?, ?, NOW())";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("iiid", $maestroID, $playerID, $writingID, $record);
		$stmt->execute();
		$stmt->close();
	}

	public function updateRecord($typingExamRecordID, $record)
	{
		$query = "
			UPDATE typing_exam_record
			SET Record = ?, RecordDateTime = NOW()
			WHERE TypingExamRecordID = ?";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("di", $record, $typingExamRecordID);
		$stmt->execute();
		$stmt->close();
	}

	public function deleteRecord($typingExamRecordID)
	{
		$query = "
			DELETE
			FROM typing_exam_record
			WHERE TypingExamRecordID = ?
		";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param('i', $typingExamRecordID);
		$stmt->execute();
		$stmt->close();
	}

	public function addHighestRecord($maestroID, $playerID, $writingID, $record)
	{
		$query = "
			INSERT INTO typing_exam_highest_record (MaestroID, PlayerID, WritingID, HighestRecord, RecordDateTime)
			VALUES (?, ?, ?, ?, NOW())";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("iiid", $maestroID, $playerID, $writingID, $record);
		$stmt->execute();
		$stmt->close();
	}

	public function getThisHourRecord($maestroID, $playerID, $writingID)
	{
		$query = "
			SELECT TypingExamRecordID, Record
			FROM typing_exam_record
			WHERE MaestroID = ? AND PlayerID = ? AND WritingID = ? AND DATE(RecordDateTime) = DATE(NOW()) AND HOUR(RecordDateTime) = HOUR(NOW())";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("iii", $maestroID, $playerID, $writingID);
		$stmt->execute();
		$stmt->bind_result($typingExamRecordID, $record);

		$stmt->fetch();
		$result = array();
		$result["typingExamRecordID"] = $typingExamRecordID;
		$result["record"] = $record;
		$stmt->close();

		return $result;
	}

	public function getHighestRecord($maestroID, $playerID, $writingID)
	{
		$query = "
			SELECT TypingExamHighestRecordID, 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($typingExamHighestRecordID, $writingID, $highestRecord);

		$stmt->fetch();
		$recordData = array();
		$recordData["typingExamHighestRecordID"] = $typingExamHighestRecordID;
		$recordData["highestRecord"] = $highestRecord;
		$stmt->close();

		return $recordData;
	}

	public function getHighestRecordArrayForAllWriting($maestroID, $playerID)
	{
		$query = "
			SELECT TypingExamHighestRecordID, WritingID, HighestRecord
			FROM typing_exam_highest_record
			WHERE MaestroID = ? AND PlayerID = ?";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("iii", $maestroID, $playerID);
		$stmt->execute();
		$stmt->bind_result($typingExamHighestRecordID, $writingID, $highestRecord);

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

		return $resultArray;
	}

	public function updateHighestRecord($typingExamHighestRecordID, $record)
	{
		$query = "
			UPDATE typing_exam_highest_record
			SET HighestRecord = ?, RecordDateTime = NOW()
			WHERE TypingExamHighestRecordID = ?";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param("di", $record, $typingExamHighestRecordID);
		$stmt->execute();
		$stmt->close();
	}

	function getHistoryRecord($maestroID, $playerID, $writingID, $date) {
		$query = "
			SELECT DATE(RecordDateTime), MAX(Record)
			FROM typing_exam_record
			WHERE MaestroID = ? AND PlayerID = ? AND DATE(RecordDateTime) <= ? AND WritingID = ?
			GROUP BY DATE(RecordDateTime)
			ORDER BY RecordDateTime DESC
			LIMIT 7";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param('iisi', $maestroID, $playerID, $date, $writingID);
		$stmt->execute();
		$stmt->bind_result($date, $highestRecord);

		$historyRecordArray = array();
		while($stmt->fetch()) {
			$historyRecord['Date'] = $date;
			$historyRecord['HighScore'] = $highestRecord;
			$historyRecord['AppName'] = "TypingExam";
			array_push($historyRecordArray, $historyRecord);
		}
		$stmt->close();

		return $historyRecordArray;
	}

	function getRankingRecordHour($maestroID, $writingID, $date, $time) {
		$query = "
			SELECT TER.playerID As PlayerID, U.Name AS Name, MAX(TER.Record) AS HighScore
			FROM typing_exam_record TER, player U
			WHERE TER.MaestroID = ? AND TER.playerID = U.playerID AND YEAR(TER.RecordDateTime) = YEAR(?) AND MONTH(TER.RecordDateTime) = MONTH(?) AND DAYOFMONTH(TER.RecordDateTime) = DAYOFMONTH(?) AND HOUR(TER.RecordDateTime) = HOUR(?) AND TER.WritingID = ? AND TER.Record >= 0
			GROUP BY TER.playerID
			ORDER BY MAX(TER.Record) DESC;
		";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param('issssi', $maestroID, $date, $date, $date, $time, $writingID);
		$stmt->execute();
		$stmt->bind_result($playerID, $name, $highScore);

		$rankingRecordArray = array();
		while($stmt->fetch()) {
		    $rankingRecord['PlayerID'] = $playerID;
		    $rankingRecord['Name'] = $name;
		    $rankingRecord['HighScore'] = $highScore;
		    array_push($rankingRecordArray, $rankingRecord);
		}
		$stmt->close();

		return $rankingRecordArray;
	}

	function getRankingRecordDay($maestroID, $writingID, $date) {
		$query = "
			SELECT TER.playerID As PlayerID, U.Name AS Name, MAX(TER.Record) AS HighScore
			FROM typing_exam_record TER, player U
			WHERE TER.MaestroID = ? AND TER.playerID = U.playerID AND YEAR(TER.RecordDateTime) = YEAR(?) AND MONTH(TER.RecordDateTime) = MONTH(?) AND DAYOFMONTH(TER.RecordDateTime) = DAYOFMONTH(?) AND TER.WritingID = ? AND TER.Record >= 0
			GROUP BY TER.playerID
			ORDER BY MAX(TER.Record) DESC;
		";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param('isssi', $maestroID, $date, $date, $date, $writingID);
		$stmt->execute();
		$stmt->bind_result($playerID, $name, $highScore);

		$rankingRecordArray = array();
		while($stmt->fetch()) {
		    $rankingRecord['PlayerID'] = $playerID;
		    $rankingRecord['Name'] = $name;
		    $rankingRecord['HighScore'] = $highScore;
		    array_push($rankingRecordArray, $rankingRecord);
		}
		$stmt->close();

		return $rankingRecordArray;
	}

	function getRankingRecordMonth($maestroID, $writingID, $date) {
		$query = "
			SELECT TER.playerID As PlayerID, U.Name AS Name, MAX(TER.Record) AS HighScore
			FROM typing_exam_record TER, player U
			WHERE TER.MaestroID = ? AND TER.playerID = U.playerID AND YEAR(TER.RecordDateTime) = YEAR(?) AND MONTH(TER.RecordDateTime) = MONTH(?) AND TER.WritingID = ? AND TER.Record >= 0
			GROUP BY TER.playerID
			ORDER BY MAX(TER.Record) DESC;
		";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param('issi', $maestroID, $date, $date, $writingID);
		$stmt->execute();
		$stmt->bind_result($playerID, $name, $highScore);

		$rankingRecordArray = array();
		while($stmt->fetch()) {
		    $rankingRecord['PlayerID'] = $playerID;
		    $rankingRecord['Name'] = $name;
		    $rankingRecord['HighScore'] = $highScore;
		    array_push($rankingRecordArray, $rankingRecord);
		}
		$stmt->close();

		return $rankingRecordArray;
	}

	function getRankingMinusRecordHour($maestroID, $writingID, $date, $time) {
		$query = "
			SELECT TER.playerID As PlayerID, U.Name AS Name, MAX(TER.Record) AS HighScore
			FROM typing_exam_record TER, player U
			WHERE TER.MaestroID = ? AND TER.playerID = U.playerID AND YEAR(TER.RecordDateTime) = YEAR(?) AND MONTH(TER.RecordDateTime) = MONTH(?) AND DAYOFMONTH(TER.RecordDateTime) = DAYOFMONTH(?) AND HOUR(TER.RecordDateTime) = HOUR(?) AND TER.WritingID = ? AND TER.Record < 0
			GROUP BY TER.playerID
			ORDER BY MAX(TER.Record) ASC;
		";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param('issssi', $maestroID, $date, $date, $date, $time, $writingID);
		$stmt->execute();
		$stmt->bind_result($playerID, $name, $highScore);

		$rankingRecordArray = array();
		while($stmt->fetch()) {
		    $rankingRecord['PlayerID'] = $playerID;
		    $rankingRecord['Name'] = $name;
		    $rankingRecord['HighScore'] = $highScore;
		    array_push($rankingRecordArray, $rankingRecord);
		}
		$stmt->close();

		return $rankingRecordArray;
	}

	function getRankingMinusRecordDay($maestroID, $writingID, $date) {
		$query = "
			SELECT TER.playerID As PlayerID, U.Name AS Name, MAX(TER.Record) AS HighScore
			FROM typing_exam_record TER, player U
			WHERE TER.MaestroID = ? AND TER.playerID = U.playerID AND YEAR(TER.RecordDateTime) = YEAR(?) AND MONTH(TER.RecordDateTime) = MONTH(?) AND DAYOFMONTH(TER.RecordDateTime) = DAYOFMONTH(?) AND TER.WritingID = ? AND TER.Record < 0
			GROUP BY TER.playerID
			ORDER BY MAX(TER.Record) ASC;
		";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param('isssi', $maestroID, $date, $date, $date, $writingID);
		$stmt->execute();
		$stmt->bind_result($playerID, $name, $highScore);

		$rankingRecordArray = array();
		while($stmt->fetch()) {
		    $rankingRecord['PlayerID'] = $playerID;
		    $rankingRecord['Name'] = $name;
		    $rankingRecord['HighScore'] = $highScore;
		    array_push($rankingRecordArray, $rankingRecord);
		}
		$stmt->close();

		return $rankingRecordArray;
	}

	function getRankingMinusRecordMonth($maestroID, $writingID, $date) {
		$query = "
			SELECT TER.playerID As PlayerID, U.Name AS Name, MAX(TER.Record) AS HighScore
			FROM typing_exam_record TER, player U
			WHERE TER.MaestroID = ? AND TER.playerID = U.playerID AND YEAR(TER.RecordDateTime) = YEAR(?) AND MONTH(TER.RecordDateTime) = MONTH(?) AND TER.WritingID = ? AND TER.Record < 0
			GROUP BY TER.playerID
			ORDER BY MAX(TER.Record) ASC;
		";
		$stmt = $this->mysqli->prepare($query);
		$stmt->bind_param('issi', $maestroID, $date, $date, $writingID);
		$stmt->execute();
		$stmt->bind_result($playerID, $name, $highScore);

		$rankingRecordArray = array();
		while($stmt->fetch()) {
		    $rankingRecord['PlayerID'] = $playerID;
		    $rankingRecord['Name'] = $name;
		    $rankingRecord['HighScore'] = $highScore;
		    array_push($rankingRecordArray, $rankingRecord);
		}
		$stmt->close();

		return $rankingRecordArray;
	}
}
?>