<?php
class Version{
	
	protected $ID;
	protected $name;
	protected $sendDate;
	protected $publicFiles;//String "jar xlsx javadoc" in bdd , stored as a string array
	protected $tags;//String "beta bugged" in bdd , stored as a string array
	protected $project;
	protected $language;
	
	public function __construct($ID){
		$this->ID = $ID;
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM versions WHERE ID=?');
		$req->execute(array($ID));
		if($rep = $req->fetch()){
			$this->valid = TRUE;
			$this->name = $rem['name'];
			$this->sendDate = $rep['sendDate'];
			$this->publicFiles = explode(" ",$rep['publicFiles']);
			$this->tags = explode(" ",$rep['tags']);
			$this->project = new Project($rep['projectID']);
			$this->language = Langage::getFromID($rep['languageID']);
		}else{
			$this->valid = FALSE;
		}
	}
	
	public function exists(){
		return $this->valid;
	}
	
	public function __toString(){
		$out = 'Version\n';
		$out .= '\tID:' . $this->ID . '\n';
		$out .= '\tName:{' . $this->name . '}\n';
		$out .= '\tSend date:' . $this->sendDate . '\n';
		$out .= '\tPublic files:"' . implode(' ',$this->publicFiles) . '"}\n';
		$out .= '\tTags:"' . implode(' ',$this->tags) . '"}\n';
		$out .= '\tProject:' . substr(str_replace('\n\t',';',$this->project->__toString()),0,-1) . '\n';
		$out .= '\tLanguage:' . $this->language->getName() . '\n';
		return out;
	}
	
	
	//Getters
	public function getID(){
		return $this->ID;
	}
	
	public function getName(){
		return $this->name;
	}
	
	public function getSendDate(){
		return $this->sendDate;
	}
	
	public function getPublicFiles(){
		//returns a string array
		return $this->publicFiles;
	}
	
	public function getTags(){
		//returns a string array
		return $this->tags;
	}
	
	public function getProject(){
		return $this->project;
	}
	
	public function getLanguage(){
		return $this->language;
	}
	
	//Useful Getters
	//TODO make all of the following 'is' functions using haveTag
	public function haveTag($tag){
		return in_array($tag,$this->tags);
	}
	
	public function isJarPublic(){
		return in_array("jar",$this->publicFiles);
	}
	
	public function isJavaPublic(){
		return in_array("java",$this->publicFiles);
	}
	
	public function isJavadocPublic(){
		return in_array("javadoc",$this->publicFiles);
	}
	
	public function isXlsmPublic(){
		return in_array("xlsm",$this->publicFiles);
	}
	
	public function isVbPublic(){
		return in_array("vb",$this->publicFiles);
	}
	
	public function isAlpha(){
		return in_array("alpha",$this->tags);
	}
	
	public function isBeta(){
		return in_array("beta",$this->tags);
	}
	
	public function isRelease(){
		return in_array("release",$this->tags);
	}
	
	public function isTest(){
		return in_array("test",$this->tags);
	}
	
	public function isBugged(){
		return in_array("bugged",$this->tags);
	}
	
	//Setters
	public function storeTags(){
		$rep = $GLOBALS['bdd']->prepare('UPDATE versions SET tags=? WHERE ID=?');
		$rep->execute(array(implode(' ',$this->tags),$this->ID));
	}
	
	public function addTag($tag,$store = TRUE){
		$this->deleteTag($tag,FALSE);
		$this->tags[] = $tag;
		if($store)$this->storeTags();
	}
	
	public function deleteTag($tag,$store = TRUE){
		$this->tags = array_diff($this->tags,array($tag));
		if($store)$this->storeTags();
	}
	
	public function setARelease(){
		$this->deleteTag('alpha',FALSE);
		$this->deleteTag('beta',FALSE);
		$this->addTag('release',FALSE);
		$this->storeTags();
	}
	
	public function setAnAlpha(){
		$this->deleteTag('beta',FALSE);
		$this->deleteTag('release',FALSE);
		$this->addTag('alpha',FALSE);
		$this->storeTags();
	}
	
	public function setABeta(){
		$this->deleteTag('alpha',FALSE);
		$this->deleteTag('release',FALSE);
		$this->addTag('beta',FALSE);
		$this->storeTags();
	}
	
	public function setATest(){
		$this->addTag('test');
	}
	
	public function setNotATest(){
		$this->delTag('test');
	}
	
	public function setBugged(){
		$this->addTag('bugged');
	}
	
	public function setNotBugged(){
		$this->delTag('bugged');
	}
	
	public function storePublicFiles(){
		$rep = $GLOBALS['bdd']->prepare('UPDATE versions SET publicFiles=? WHERE ID=?');
		$rep->execute(array(implode(' ',$this->publicFiles),$this->ID));
	}
	
	public function addPublicFile($file,$store = TRUE){
		$this->deletePublicFile($file,FALSE);
		$this->publicFiles[] = $file;
		if($store)$this->storePublicFiles();
	}
	
	public function deletePublicFile($file,$store = TRUE){
		$this->publicFiles = array_diff($this->publicFiles,array($file));
		if($store)$this->storePublicFiles();
	}
		
	public function setJarPublic(){
		$this->addPublicFile('jar');
	}
	
	public function setJarNotPublic(){
		$this->deletePublicFile('jar');
	}
	
	public function setJavaPublic(){
		$this->addPublicFile('java');
	}
	
	public function setJavaNotPublic(){
		$this->deletePublicFile('java');
	}
	
	public function setJavadocPublic(){
		$this->addPublicFile('javadoc');
	}
	
	public function setJavadocNotPublic(){
		$this->deletePublicFile('javadoc');
	}
	
	public function setXlsmPublic(){
		$this->addPublicFile('xlsm');
	}
	
	public function setXlsmNotPublic(){
		$this->deletePublicFile('xlsm');
	}
	
	public function setVbPublic(){
		$this->addPublicFile('vb');
	}
	
	public function setVbNotPublic(){
		$this->deletePublicFile('vb');
	}
	
	
	//Version Getter
	private static function versionGetterOutput($req){
		$out = array();
		while($rep = $req->fetch())
			$out[] = new Version($rep['ID']);
			switch(count($out)){
				case 0:
					return NULL;
				case 1:
					return $out[0];
				default:
					return $out;
			}
	}
	
	public static function getFromLanguage($language){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM versions WHERE languageID=?');
		$req->execute(array($language->getID()));
		return versionGetterOutput($req);
	}
	
	public static function getFromProject($project){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM versions WHERE projectID=?');
		$req->execute(array($project->getID()));
		return versionGetterOutput($req);
	}
	
	public static function getFromName($name){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM versions WHERE name=?');
		$req->execute(array($name));
		return versionGetterOutput($req);
	}
	
	public static function getFromTag($tag){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM versions');
		$req->execute();
		$out = array();
		while ($rep = $req->fetch()) {
			$projet = new Version($rep['ID']);
			if ($projet->haveTag($tag))
				$out[] = $projet;
		}
		switch(count($out)){
			case 0:
				return NULL;
			case 1:
				return $out[0];
			default:
				return $out;
		}
		
		return projectGetterOutput($req);
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM versions WHERE languageID=?');
		$req->execute(array($language->getID()));
		return versionGetterOutput($req);
	}
	
	public static function getFromSendDate($date){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM versions WHERE sendDate=?');
		$req->execute(array($date));
		return versionsGetterOutput($req);
	}
	
	public static function getSendedLaterThan($date){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM versions WHERE sendDate>?');
		$req->execute(array($date));
		return versionsGetterOutput($req);
	}
	
	public static function getSendedEarlierThan($date){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM versions WHERE sendDate<?');
		$req->execute(array($date));
		return versionsGetterOutput($req);
	}
	
	
	
	
}