<?php

require_once "../config.php";

class Tournament
{
    private $id;
    private $name;
    private $size;
    private $place;
    private $price;
    private $description;
    private $date_start, $date_end;
    private $date_inscription;
    private $date_solutions;
    private $date_syntheses;
    private $final;
    private $year;

    private function __construct() {}

	public static function fromId($id)
	{
		global $DB;
		$req = $DB->prepare("SELECT * FROM `tournaments` WHERE `id` = ?;");
		$req->execute([htmlspecialchars($id)]);
		$data = $req->fetch();

		if ($data === false)
			throw new InvalidArgumentException("Le tournoi spécifié n'existe pas.");

		$tournament = new Tournament();
		$tournament->fill($data);
		return $tournament;
	}

	public static function fromName($name)
	{
		global $DB, $YEAR;
		$req = $DB->prepare("SELECT * FROM `tournaments` WHERE `name` = ? AND `year` = $YEAR;");
		$req->execute([htmlspecialchars($name)]);
		$data = $req->fetch();

		if ($data === false)
			throw new InvalidArgumentException("Le tournoi spécifié n'existe pas.");

		$tournament = new Tournament();
		$tournament->fill($data);
		return $tournament;
	}

    private function fill($data)
	{
		$this->id = $data["id"];
		$this->name = $data["name"];
		$this->size = $data["size"];
		$this->place = $data["place"];
		$this->price = $data["price"];
		$this->description = $data["description"];
		$this->date_start = $data["date_start"];
		$this->date_end = $data["date_end"];
		$this->date_inscription = $data["date_inscription"];
		$this->date_solutions = $data["date_solutions"];
		$this->date_syntheses = $data["date_syntheses"];
		$this->final = $data["final"] == true;
		$this->year = $data["year"];
	}

	public function getId()
	{
		return $this->id;
	}

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

	public function setName($name)
	{
		global $DB;
		$this->name = $name;
		$DB->prepare("UPDATE `tournaments` SET `name` = ? WHERE `id` = ?;")->execute([$name, $this->id]);
	}

	public function getSize()
	{
		return $this->size;
	}

	public function setSize($size)
	{
		global $DB;
		$this->size = $size;
		$DB->prepare("UPDATE `tournaments` SET `size` = ? WHERE `id` = ?;")->execute([$size, $this->id]);
	}

	public function getPlace()
	{
		return $this->place;
	}

	public function setPlace($place)
	{
		global $DB;
		$this->place = $place;
		$DB->prepare("UPDATE `tournaments` SET `place` = ? WHERE `id` = ?;")->execute([$place, $this->id]);
	}

	public function getPrice()
	{
		return $this->price;
	}

	public function setPrice($price)
	{
		global $DB;
		$this->price = $price;
		$DB->prepare("UPDATE `tournaments` SET `price` = ? WHERE `id` = ?;")->execute([$price, $this->id]);
	}

	public function getDescription()
	{
		return $this->description;
	}

	public function setDescription($desc)
	{
		global $DB;
		$this->description = $desc;
		$DB->prepare("UPDATE `tournaments` SET `description` = ? WHERE `id` = ?;")->execute([$desc, $this->id]);
	}

	public function getStartDate()
	{
		return $this->date_start;
	}

	public function setStartDate($date)
	{
		global $DB;
		$this->date_start = $date;
		$DB->prepare("UPDATE `tournaments` SET `date_start` = ? WHERE `id` = ?;")->execute([$date, $this->id]);
	}

	public function getEndDate()
	{
		return $this->date_end;
	}

	public function setEndDate($date)
	{
		global $DB;
		$this->date_end = $date;
		$DB->prepare("UPDATE `tournaments` SET `date_end` = ? WHERE `id` = ?;")->execute([$date, $this->id]);
	}

	public function getInscriptionDate()
	{
		return $this->date_inscription;
	}

	public function setInscriptionDate($date)
	{
		global $DB;
		$this->date_inscription = $date;
		$DB->prepare("UPDATE `tournaments` SET `date_inscription` = ? WHERE `id` = ?;")->execute([$date, $this->id]);
	}

	public function getSolutionsDate()
	{
		return $this->date_solutions;
	}

	public function setSolutionsDate($date)
	{
		global $DB;
		$this->date_solutions = $date;
		$DB->prepare("UPDATE `tournaments` SET `date_solutions` = ? WHERE `id` = ?;")->execute([$date, $this->id]);
	}

	public function getSynthesesDate()
	{
		return $this->date_syntheses;
	}

	public function setSynthesesDate($date)
	{
		global $DB;
		$this->date_syntheses = $date;
		$DB->prepare("UPDATE `tournaments` SET `date_syntheses` = ? WHERE `id` = ?;")->execute([$date, $this->id]);
	}

	public function isFinal()
	{
		return $this->final;
	}

	public function setFinal($final)
	{
		global $DB;
		$this->final = $final;
		$DB->prepare("UPDATE `tournaments` SET `final` = ? WHERE `id` = ?;")->execute([$final, $this->id]);
	}

	public function getYear()
	{
		return $this->year;
	}
}