<?php
class Message{
	
	protected $ID;
	protected $sender;
	protected $sendDate;
	protected $text;
	protected $discussion;
	
	protected $valid;
	
	public function __construct($ID){
		$this->ID = $ID;
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM messages WHERE ID=?');
		$req->execute(array($ID));
		if($rep = $req->fetch()){
			$this->valid = TRUE;
			$this->sender = new Membre($rep['senderID']);
			$this->sendDate = $rep['sendDate'];
			$this->text = $rep['text'];
			$this->discussion = new Discussion($rep['discussionID']);
		}else{
			$this->valid = FALSE;
		}
	}
	
	public function exists(){
		return $this->valid;
	}
	
	public function __toString(){
		$out = 'Message\n';
		$out .= '\tID:' . $this->ID . '\n';
		$out .= '\tSender:{' . substr(str_replace('\n\t',';',$this->sender->__toString()),0,-1) . '}\n';
		$out .= '\tSend date:' . $this->sendDate . '\n';
		$out .= '\tText:' . $this->text . '}\n';
		$out .= '\tDiscussion:' . substr(str_replace('\n\t',';',$this->discussion->__toString()),0,-1) . '\n';
		return out;
	}
	
	
	//Getters
	public function getID(){
		return $this->ID;
	}
	
	public function getSender(){
		return $this->sender;
	}
	
	public function getSendDate(){
		return $this->sendDate;
	}
	
	public function getText(){
		return $this->text;
	}
	
	public function getDiscussion(){
		return $this->discussion;
	}
	
	
	//Useful getters
	public function canEdit($person){
		return $person->isAdminLevelGreaterOrEqualThan(6) || $person->getID() == $this->sender->getID();
	}

	
	//Setters
	public function editText($newText){
		$this->text = htmlSepcialChars($newText);
		$req = $GLOBALS ['bdd']->prepare ( 'INSERT INTO messages(discussion_id,texte,senderID,sendTime) VALUES (?,?,?,NOW())' );
		$req->execute (array ($this->discussion->getID(),$this->text,$this->sender->getID()));
	}
	
	//Message getter
	private static function messagesGetterOutput($req){
		$out = array();
		while($rep = $req->fetch())
			$out[] = new Discussion($rep['ID']);
			switch(count($out)){
				case 0:
					return NULL;
				case 1:
					return $out[0];
				default:
					return $out;
			}
	}
	
	public static function getFromSender($param) {
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM messages WHERE senderID=?');
		$req->execute(array($sender->getID()));
		return messagesGetterOutput($req);
	}
	
	
	public static function getFromSendDate($date){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM messages WHERE sendDate=?');
		$req->execute(array($date));
		return messagesGetterOutput($req);
	}
	
	public static function getSendedLaterThan($date){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM messages WHERE sendDate>?');
		$req->execute(array($date));
		return messagesGetterOutput($req);
	}
	
	public static function getSendedEarlierThan($date){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM messages WHERE sendDate<?');
		$req->execute(array($date));
		return messagesGetterOutput($req);
	}
	
	public static function getFromText($text) {
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM messages WHERE text=?');
		$req->execute(array($text));
		return messagesGetterOutput($req);
	}

	public static function getFromDiscussion($discut){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM messages WHERE discussionID=?');
		$req->execute(array($discut->getID()));
		return messagesGetterOutput($req);
	}
	
	
	//Message creator
	public static function sendMessage($sender,$discussion,$text){
		$req = $GLOBALS ['bdd']->prepare ( 'INSERT INTO messages(discussion_id,texte,senderID,sendTime) VALUES (?,?,?,NOW())' );
		$req->execute (array($discussion->getID(),htmlspecialchars ($text),$sender->getID()));
	}
	
}