<?php 

class TextExport
{
	public static $mime = 'text/plain';
	public static $extension = 'txt';
	public static $description = 'Fichier de texte brut';

	public static function sample($dataset){
		$stream = '';
		foreach($dataset as $macro => $infos)
			$stream .= ($infos['type']=='list') ? '{{#'.$macro.'}}{{/'.$macro.'}} : '.$infos['desc'].PHP_EOL : '{{'.$macro.'}} : '.$infos['desc'].PHP_EOL;
		return $stream;
	}

	public static function from_template($source, $data, $return, $isHtml=false){
		$fileParts = explode('.', $source);
		$ext = count($fileParts)>1 ? '.'.end($fileParts) : '';
		if(count(explode(SLASH, $ext)) > 1) $ext = '';

		$destination = File::dir().'tmp'.SLASH.'template.'.time().'-'.rand(0,100).$ext;
		$source = File::dir().$source;
		copy($source,$destination);

		$stream = file_get_contents($destination);
		if(mb_detect_encoding($stream, 'UTF-8', true) == false) $stream = utf8_encode($stream);
		
		foreach($data as $tag=>$value){
			if(is_array($value)){
				$stream =  preg_replace_callback(
					'#\{\{\#'.$tag.'\}\}(.*)\{{\/'.$tag.'\}\}#si',
					function ($match) use($value) {
						$html = '';
						foreach($value as $line){
							$bloc = $match[1];
							foreach($line as $k=>$v){
								$bloc = str_replace('{{'.$k.'}}',$v,$bloc);
								$bloc = self::if_condition($bloc, $line);
								$bloc = self::else_condition($bloc, $line);
							}
							$html .=$bloc;
						}
						return $html;
					},
					$stream
				);
				continue;
			}

			//Pas d'images possibles dans un fichier texte
			if(substr($value,0,2)=='::' && !$isHtml) continue;

			//Remplacement d'image pour les fichiers HTML/PDF
			if(substr($value,0,2)=='::') $value = substr($value,2);
			$stream = str_replace('{{'.$tag.'}}', $value, $stream);
		}

		$stream = self::if_condition($stream, $data);
		$stream = self::else_condition($stream, $data);

		file_put_contents($destination, $stream);
		if($return!='stream') return $destination;

		$stream = file_get_contents($destination);
		unlink($destination);
		return ($isHtml ? $stream : utf8_decode($stream));
	}

	public static function if_condition($stream, $data){
		// $ifRegex = '/\{\{\#([^\/\:\#}]*)\}\}(.*?)\{\{\/\1\}\}/is';
		// $stream = recursive_preg_replace($ifRegex, $stream, $data);
		
		//conditions
		$ifRegex = '/\{\{\#([^\/\:\#}]*)\}\}(.*?)\{\{\/\1\}\}/is';
		return preg_replace_callback($ifRegex,function($matches) use ($data) {
			$key = $matches[1];
			$stream = $matches[2];
			return !isset($data[$key]) || (!is_array($data[$key]) && empty($data[$key])) || (is_array($data[$key]) && count($data[$key])==0) ?'':$stream;
		},$stream);
	}

	public static function else_condition($stream, $data){
		// $elseRegex = '/\{\{\^([^\/\:\#}]*)\}\}(.*?)\{\{\/\1\}\}/is';
		// $stream = recursive_preg_replace($elseRegex, $stream, $data);
		
		//conditions
		$elseRegex = '/\{\{\^([^\/\:\#}]*)\}\}(.*?)\{\{\/\1\}\}/is';
		return preg_replace_callback($elseRegex,function($matches) use ($data) {
			$key = $matches[1];
			$stream = $matches[2];
			return (isset($data[$key]) && !is_array($data[$key]) && !empty($data[$key])) || (is_array($data[$key]) && count($data[$key])>0) ?'':$stream;
		},$stream);
	}
}

