<?php #lib/90_response.php
 
class Response
{
	public $code = 500;
	private $body = ["messages" => [], "content" => []];
	private $etag = null;
	private $html = null;
	private $hasBody = true;
	private static $instance = null;
	
  /***** Static functions *****/
  
  public static function &Get() {
  	if (is_null(self::$instance)) {
  		self::$instance = new self();
  	}
  	return self::$instance;
  }

  /***** Con-/Destructor *****/
  
  private function __construct() {}
  public function __destruct() {
  	http_response_code($this->code);
  	if (is_null($this->html)) {
  		header("Content-type: application/json");
  	} else {
  		header("Content-type: text/html");
  	}
  	if (!is_null($this->etag)) {
  		header("ETag: ".$this->etag);
  	}
  	if ($this->hasBody) {
  		if ($this->IsOk() && Request::IsPrint()) {
				echo trim(Printer::Print(Request::PrintID, $this->body["content"]));
	  	} else {
		  	echo trim(json_encode($this->body));
	} } }
  
  /***** Public functions *****/
  
  public function IsOk() {return 200==$this->code;}
  
  public function &Message($message) {$this->body["messages"][] = $message; return $this;}
  public function &Code($code) {$this->code = $code; return $this;}
  public function &Json($name, $json) {$this->body[$name] = $json; return $this;}
  public function &Content($json) {$this->body["content"] = $json; return $this;}
  public function &Etag($etag) {$this->etag = $etag; return $this;}
  public function &Html($html) {$this->html = $html; return $this;}
  public function &DisableBody() {$this->hasBody = false; return $this;}
  
  public function &Good() {$this->Code(200); return $this;}
  public function &Inserted($name) {$this->Code(201)->Message($name." hinzugefügt"); return $this;}
  public function &Deleted($name) {$this->Code(200)->Message($name." entfernt"); return $this;}
  public function &Updated($name) {$this->Code(200)->Message($name." aktualisiert"); return $this;}
  
  public function &SubInserted() {$this->Code(201)->Message("Verknüpfung hinzugefügt"); return $this;}
  public function &SubDeleted() {$this->Code(200)->Message("Verknüpfung entfernt"); return $this;}
  public function &SubUpdated() {$this->Code(200)->Message("Verknüpfung aktualisiert"); return $this;}
  
  public function &FileUploaded() {$this->Code(201)->Message("Datei hochgeladen"); return $this;}
  public function &FileErased() {$this->Code(200)->Message("Datei gelöscht"); return $this;}
  
  public function &Forbidden() {$this->Code(403); return $this;}
  public function &NotFound() {$this->Code(404)->Message("Resource not found"); return $this;}
  public function &MissingID() {$this->Code(400)->Message("Required ID was not specified"); return $this;}
  public function &IllegalInput() {$this->Code(400)->Message("You are forbidden to change at least something in the provided dataset."); return $this;}
  public function &IllegalQuery() {$this->Code(400)->Message("You are forbidden to request at least something in the queried dataset."); return $this;}
  public function &MissingContent() {$this->Code(400)->Message("Required input 'content' is missing"); return $this;}
  public function &NotImplemented() {$this->Code(405)->Message("This method is not (yet) supported by this resource!"); return $this;}
  public function &SubRouteNotExisting($name) {$this->Code(404)->Message("Subroute `".$name."` not existant for this resource"); return $this;}
  public function &DbError() {$this->Code(500)->Message("Mysql error: ".Db::Get()->error); return $this;}
}
