<?php

include_once './includes/tournaments/TournamentEntry.inc.php';
include_once './includes/tournaments/TournamentEntryLister.inc.php';
include_once './includes/tournaments/TournamentFormatter.inc.php';

include_once './includes/utils/InputSanitizer.inc.php';
include_once './includes/utils/DateParser.inc.php';
include_once './includes/utils/DateFormatter.inc.php';

/**
 * This class represent a tournament.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class Tournament {

    private $identifier;

    private $name;

    private $location;

    private $date;

    private $type;

    private $comments;

    public static function Get($identifier) {
        $sql = "SELECT * FROM tournaments WHERE identifier = $identifier";

        $result = MySQLDatabase::getInstance()->runRequest($sql);
        if (is_array($result) && count($result) > 0) {
            $result = $result[0];
        } else {
            return null;
        }

        return Tournament::GetSQL($result);
    }

    public static function GetSQL(array $sqlResponse) {

        $newTournament = new Tournament($sqlResponse['identifier']);
        
        $newTournament->name = $sqlResponse['name'];
        $newTournament->location = $sqlResponse['location'];
        $newTournament->date = DateParser::parseSQL($sqlResponse['date']);
        $newTournament->type = $sqlResponse['type'];
        $newTournament->comments = $sqlResponse['comments'];
        $newTournament->needsSaving = false;

        return $newTournament;
    }

    public static function Create($name, $location, $date, $type, $comments) {

        $name = InputSanitizer::Sanitize($name);
        $location = InputSanitizer::Sanitize($location);
        $date = InputSanitizer::Number($date);
        $type = InputSanitizer::Sanitize($type);
        $comments = InputSanitizer::Sanitize($comments);

        $dateSql = DateFormatter::formatSQL($date);

        $sql = "INSERT INTO `tournaments` (
            `identifier`, `name`, `location`, `date`, `type`, `comments`)
            VALUES (
            NULL, '$name', '$location', '$dateSql', '$type', '$comments');";

        $result = MySQLDatabase::getInstance()->runOperation($sql);
        $tournament = TournamentLister::getTournament($name, $location, $date, $type, $comments);

        return $tournament[0];

    }

    public static function Remove($identifier) {
        $tournament = Tournament::Get($identifier);
        
        $sql = "DELETE FROM `tournamentsEntries` WHERE tournamentId = $tournamentId";
        $result = MySQLDatabase::getInstance()->runOperation($sql);

        $sql = "DELETE FROM `tournaments` WHERE `identifier` = $identifier";
        $result = MySQLDatabase::getInstance()->runOperation($sql);

        return $result;

    }

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

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

    private function save() {

        $dateSql = DateFormatter::formatSQL($this->date);

        $sql = "UPDATE `tournaments` SET
            `name` = '$this->name',
            `location` = '$this->location',
            `date` = '$dateSql',
            `type` = '$this->type',
            `comments` = '$this->comments'

            WHERE `identifier` = $this->identifier;";

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

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

    public function getName() {
        return $this->name;
    }

    public function getLocation() {
        return $this->location;
    }

    public function getDate() {
        return $this->date;
    }

    public function getType() {
        return $this->type;
    }

    public function getComments() {
        return $this->comments;
    }

    /**
     * Sets the informations about the Tournament
     *
     * @param string $name
     * @param string $location
     * @param int $date
     * @param string $type
     * @param string $comments
     */
    public function setInformations($name, $location, $date, $type, $comments) {

        $name = InputSanitizer::Sanitize($name);
        $location = InputSanitizer::Sanitize($location);
        $date = InputSanitizer::Number($date);
        $type = InputSanitizer::Sanitize($type);
        $comments = InputSanitizer::Sanitize($comments);

        $this->name = $name;
        $this->location = $location;
        $this->date = $date;
        $this->type = $type;
        $this->comments = $comments;

        $this->needsSaving = true;
    }

}
?>
