<?php 

function View($name){
	view::reset();
	view::is($name);
}

class View{
	static $listIs = [];
	static function is($name,$data = []){
		if(!self::$isHtmlGenerating){
			self::$listIs = [];
		}
		array_unshift(self::$listIs, [
			"name" => $name,
			"data" => $data,
			"run"  => false,
		]);
		self::openSection("content");
	}
	static function reset(){
		$c = ob_get_clean();
		ob_start();
		self::$listIs = [];
	}
	static function import($name,$data = []){
		return self::is($name,$data);
	}
	static function set($section,$data = null,$append = false){
		if(!is_null($data)){
			if ($append) {
				if (is_array($data)) {
					if(!is_array(self::$sections[$section])) self::$sections[$section] = [];
					self::$sections[$section] += $data;
				}else{
					if(is_callable($data)){
						ob_start();
						$data();
						$data=ob_get_clean();
					}
					if(empty(self::$sections[$section])) self::$sections[$section] .= "";
					self::$sections[$section] .= $data;
				}			
			}else{
				self::$sections[$section] = $data;
			}
		}else{
			self::openSection($section);
		}
	}
	static $currentSection = "content";
	static $sections = [];
	static function openSection($name){
		if(self::$currentSection == $name) return;
		self::closeSection();
		if(empty(self::$sections[$name])) self::$sections[$name] = "";
		self::$currentSection = $name;
	}
	static function closeSection(){
		if(empty(self::$sections[self::$currentSection])) self::$sections[self::$currentSection] = "";
		if (self::$isHtmlGenerating) {
			self::$sections[self::$currentSection] = ob_get_clean();
		}else{
			self::$sections[self::$currentSection] .= ob_get_clean();
		}
		ob_start();
	}
	static function __callStatic($name,$args){
		if(empty($args)){
			self::set($name);
		}else{
			self::set($name,$args[0]);
		}
	}
	static $isHtmlGenerating = false;
	static function html(){
		self::$isHtmlGenerating = true;
		$noview = true;
		do {
			$is = false;
			for ($i=0; $i < count(self::$listIs); $i++) { 
				if(!self::$listIs[$i]["run"]){
					self::$listIs[$i]["run"] = true;
					$is = self::$listIs[$i];
				}
			}
			if ($is) {
				$name = $is["name"];
				if (isset(self::$listFiles[$name])) {
					self::closeSection();
					$noview = false;
					$filename = self::$listFiles[$name];
					$fn = function() use ($filename){
						include $filename;
					};
					$fn();
				}else{
					echo "<b style='display:block;color:red;background:#fff;padding:10px;margin:10px;'>Erreur View '$name' introuvable</b>";
				}
			}
		} while ($is);
		if ($noview) {
			echo self::get("content");
		}
		$code = ob_get_clean();
		self::$isHtmlGenerating = false;
		ob_start();
		return $code;
	}
	static $listFiles = [];
	static function create($name,$filename){
		self::$listFiles[$name] = $filename;
	}
	static function get($names,$default = null){
		if(is_string($names)) $names = explode(",",$names);
		foreach ($names as $name) {
			if(isset(self::$sections[$name])){
				if($name == "content"){
					if (!empty(self::$sections[$name])) {
						return self::$sections[$name];
					}
				}else{
					return self::$sections[$name];
				}
			}
		}

		if (is_callable($default)) {
			ob_start();
				$v = $default();
			$code=ob_get_clean();
			if(is_null($v)){
				$v = $code;
			}
			return $v;
		}
		return $default;
	}
}

function section($name,$default = null){
	$var = view::get($name,$default);
	if(is_array($var)) {
		print_r($var);
	}else{
		if (is_callable($var)) {
			$var();
		}else{
			echo $var;
		}
	}
}