<?php

/**
 * This class represents an entry in a tournament.
 *
 * An entry is the number of points and the ranks a user has in a tournament
 * in a given category.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class TournamentEntry {

    private $tournamentId;
    private $userId;
    private $categoryId;

    private $rank;
    private $points;
    private $participants;

    private $identifier;

    private $needsSaving;

    public static function Get($identifier) {

        $newEntry = new TournamentEntry($identifier);

        $sql = "SELECT *
                FROM tournamentsEntries
                WHERE entryId = $identifier";

        $result = MySQLDatabase::getInstance()->runRequest($sql);

        $result = $result[0];

        $newEntry->points = $result['points'];
        $newEntry->rank = $result['rank'];
        $newEntry->participants = $result['participants'];

        $newEntry->tournamentId = $result['tournamentId'];
        $newEntry->userId = $result['userId'];
        $newEntry->categoryId = $result['categoryId'];

        return $newEntry;
    }

    public static function Create(Tournament $tournament, Member $user, Category $category, $points, $rank, $participants) {
        $tournamentId = $tournament->getIdentifier();
        $categoryId = $category->getIdentifier();
        $userId = $user->getIdentifier();

        $points = InputSanitizer::Number($points);
        $rank = InputSanitizer::Sanitize($rank);
        $participants = InputSanitizer::Sanitize($participants);

        $sql = "INSERT INTO `tournamentsEntries` (
            `entryId`, `tournamentId`, `categoryId`, `userId`, `points`, `rank`, `participants`)
            VALUES (
            NULL, '$tournamentId', '$categoryId', '$userId', '$points', '$rank', '$participants');";

        $result = MySQLDatabase::getInstance()->runOperation($sql);

        $sql = "SELECT entryId
                FROM tournamentsEntries
                WHERE tournamentId = $tournamentId AND categoryId = $categoryId AND userId = $userId
                LIMIT 1;";
        $result = MySQLDatabase::getInstance()->runRequest($sql);

        $identifier = $result[0]['entryId'];

        $newEntry = TournamentEntry::Get($identifier);
        return $newEntry;
    }

    public static function Remove($identifier) {
        $sql = "DELETE FROM `tournamentsEntries` WHERE `entryId` = $identifier;";

        $result = MySQLDatabase::getInstance()->runOperation($sql);

        return $result;
    }

    private function __construct($identifier) {
        $this->identifier = $identifier;
        $this->needSaving = false;
    }

    public function  __destruct() {
        if ($this->needsSaving) {
            $this->save();
        }
    }

    private function save() {
        $sql = "UPDATE `tournamentsEntries` SET
            `tournamentId` = $this->tournamentId,
            `categoryId` = $this->categoryId,
            `userId` = $this->userId,
            `points` = $this->points,
            `rank` = $this->rank,
            `participants` = $this->participants

            WHERE entryId = $this->identifier;";

        $result = MySQLDatabase::getInstance()->runOperation($sql);
    }

    public function getTournament() {
        return Tournament::Get($this->tournamentId);
    }

    public function getMember() {
        return Member::Get($this->userId);
    }

    public function getCategory() {
        return Category::Get($this->categoryId);
    }

    public function getPoints() {
        return $this->points;
    }

    public function getRank() {
        return $this->rank;
    }

    public function getParticipants() {
        return $this->participants;
    }

    public function getIdentifier() {
        return $this->identifier;
    }

    public function setContent(Member $user, Category $category, $points, $rank, $participants) {
        $this->userId = $user->getIdentifier();
        $this->categoryId = $category->getIdentifier();
        $this->points = $points;
        $this->rank = $rank;
        $this->participants = $participants;

        $this->needsSaving = true;
    }
}
?>
