<?php

/**
 * @see Zend_Loader_PluginLoader
 */
require_once  'File.php';

/**
 * Loader pro třídy s modely
 *
 * @package    MyWebBlog
 * @author     Marian Böhmer
 */
abstract class MyWeb_Log_ApplicationLogger
{
	protected static $pathLogFile;
	protected static $pathNameMethodFile;
	protected static $pathTextLogFile;
	protected static $log;
	protected static $stop;
	
	public static function setPathLogFile($path)
	{
		self::$pathLogFile=$path;
	}
	public static function setPathNameMethodFile($path)
	{
		self::$pathNameMethodFile=$path;
	}
	public static function setPathTextLogFile($path)
	{
		self::$pathTextLogFile=$path;
	}
	public static function stopLog()
	{
		self::$stop = true;
	}
	public static function startLog()
	{
		self::$stop = false;
	}
	
	public static function showLog()
	{
		return self::$log;
	}
	
	
	
	/**
	 * Moda přidá do textového souboru nový řádek
	 * @param $Class
	 * @param $idMethod
	 * @param $idTextLog
	 * @param $mode
	 */
    public static  function addLog($Class,$idMethod,$idTextLog,$mode="NORMAL"){
    	
    	if (!self::$stop){
        // otestuje zadli je zadán modul
        $logger = new Zend_Log();
        $writer = new Zend_Log_Writer_Stream(self::$pathLogFile);
        $logger->addWriter($writer);
        $textLog=self::textLog($idTextLog);
    	
        if (0 === strcmp($mode, 'ABSTRACT')){
        	//pokud metoda nameMethod nevrati ocekavany vysledek provede se WriteMethodList
    		$report=self::nameMethod($idMethod,$Class);
       		if((strpos($report,"=>")) == FALSE){
        		self::WriteMethodList($Class);
       		}
       	
        	$nameMethod=self::nameMethod($idMethod,$Class);
    		$reportText='CLASS:	'.get_parent_class($Class).'=>'.$Class."	METHOD:	".$nameMethod."	LOG:	".$textLog;
        	$logger->log($reportText, Zend_Log::INFO);
        	$reportHTML='<b style="background-color:black; color: white;">CLASS:	</b>'.get_parent_class($Class).'=>'.$Class.'<b style="background-color:black; color: white;">	METHOD:	</b>'.$nameMethod.'	<b style="background-color:black; color: white;">LOG:	</b>'.$textLog;
    	}else {
    		//pokud metoda nameMethod nevrati ocekavany vysledek provede se WriteMethodList
    		$report=self::nameMethod($idMethod,get_class($Class));
    		//echo "<p>$report ok".$report."</p>";
       		if((strpos($report,"=>")) == FALSE){
       			//echo "<p>".'FALSE'.$idMethod."</p>";
        		self::WriteMethodList(get_class($Class));
       		}
    		
    		$nameMethod=self::nameMethod($idMethod,get_class($Class));
        	$reportText='CLASS:	'.get_parent_class($Class).'=>'.get_class($Class)."	METHOD:	".$nameMethod."	LOG:	".$textLog;
        	$logger->log($reportText, Zend_Log::INFO);
        	$reportHTML='<b style="background-color:black; color: white;">CLASS:	</b>'.get_parent_class($Class).'=>'.get_class($Class).'<b style="background-color:black; color: white;">	METHOD:	</b>'.$nameMethod.'	<b style="background-color:black; color: white;">LOG:	</b>'.$textLog;
    	}
    	self::$log = '<div style="background-color:red; font-size:8.7pt">'.$reportHTML.'</div>'."\n";
    	}
    }
    
    
    
    /**
     * Metoda projde soubor s nazvy metod a vrátí název metody podle indexu
     * @param int $idMethod
     * @param string $class
     */
    protected static function nameMethod($idMethod,$class)
    {
    	$delimiter='	';
    	$a=0;
    	$trueClass=0;
    	
    	//kontrola existence souboru
    	$e = File::rewind(self::$pathNameMethodFile, FILE_MODE_READ);
        if (PEAR::isError($e)){
    		echo 'Could not rewind the file : ' . $e->getMessage();
    		return "ERRORa";
		} else {
    		//echo "File successfully rewound\n";
		}
    	$e = File::close(self::$pathNameMethodFile, FILE_MODE_READ);
		if (PEAR::isError($e)){
   			echo 'Could not close file : ' . $e->getMessage();
   			return "ERRORb";
		} else {
    		//echo "Successfully closed file\n";
		}
    	
		//prochazeni souboru s nazvy metod
    	while($a<2000){
    		$a++;
    		//reading one line of file using function redLine  of PEAR package
    		$lineFile=File::readLine(self::$pathNameMethodFile);
    		if (PEAR::isError($lineFile)){
    			try {
    				$zprava = "MyWeb_Log_ApplicatioLogger: Could not read from file: ".trim($lineFile->getMessage())."\n";
        			throw new Exception($zprava);
    			}
    			catch (Exception $e){
    				echo $e->getMessage();
    				break;
    			}
    		} else {
    			
    			$partsLine=explode($delimiter, $lineFile);
    			//pokud radek zacina cislem a jsme ve spravne tride
    			if (($a > 77) && ($a < 87)){
    			//	echo "<p>".$lineFile."</p>";
    			}
    			if((is_numeric($partsLine[0])) and ($trueClass == 1)){
    				$index=intval($partsLine[0]);
    				if($index == $idMethod){
    					//echo "<p>souhlas indexu".$index."</p>";
    					$output=$class."=>".$partsLine[1];
    					$e = File::rewind(self::$pathNameMethodFile, FILE_MODE_READ);
    					
        				if (PEAR::isError($e)){
    						echo 'Could not rewind the file : ' . $e->getMessage();
						} else {
    						//echo "File successfully rewound\n";
						}
    					$e = File::close(self::$pathNameMethodFile, FILE_MODE_READ);
						if (PEAR::isError($e)){
   			 				echo 'Could not close file : ' . $e->getMessage();
						} else {
    						//echo "Successfully closed file\n";
						}
						
						return $output;
    				}
    			}else {
    				$trueClass=0;
    				if(strcmp($class, $partsLine[0]) === 0){
    					//echo "<p>trida ok".$class."xxxxxxxxx".$partsLine[0]."</p>";
    					$trueClass=1;
    				}
    			}
    		}
    	}
    	return "ERRORc";
    }
    
    
    
    protected static function textLog($idTextLog)
    {
    	$delimiter='	';
    	$a=0;
    	while($a<1000)
    	{
    		$a++;
    		//reading one line of file using function redLine  of PEAR package
    		$lineFile=File::readLine(self::$pathTextLogFile);
    		if (PEAR::isError($lineFile))
    		{
    			try {
    				$zprava = "MyWeb_Log_ApplicatioLogger: Could not read from file: ".trim($lineFile->getMessage())."\n";
        			throw new Exception($zprava);
    				}
    				catch (Exception $e) {
    					echo $e->getMessage();
    					break;
    				}
    		} else {
    			$partsLine=explode($delimiter, $lineFile);
    			if(is_numeric($partsLine[0]))
    			{
    				$index=intval($partsLine[0]);
    				if($index == $idTextLog)
    				{
    					$e = File::rewind(self::$pathTextLogFile, FILE_MODE_READ);
        				if (PEAR::isError($e))
        				{
    						echo 'Could not rewind the file : ' . $e->getMessage();
						} else {
    						//echo "File successfully rewound\n";
						}
    					$e = File::close(self::$pathTextLogFile, FILE_MODE_READ);
						if (PEAR::isError($e))
						{
   			 				echo 'Could not close file : ' . $e->getMessage();
						} else {
    						//echo "Successfully closed file\n";
						}
    					return $partsLine[1];
    				}else {
    					continue;
    				}
    			}else {
    				try {
    				$zprava = "Index of text log in file textLog isn\'t number";
        			throw new Exception($zprava);
    				}
    				catch (Exception $e) {
    					echo "MyWeb_Log_ApplicationLogger	". $e->getMessage();
    					break;
    				}
    			}
    		}
    	}
    }
     
    
    
    /**
     * metoda vytvari seznam nazvu metod tridy a pridava
     * je do textoveho souboru
     * @param string $class
     * @throws Exception
     */
     protected static function WriteMethodList($class)
     {
     	//musi prohledat soubor jesli v něm neexistuje uz trida
     	//musi prohledat metody tridy jesli se neshoduje
    	//$path='../library/MyWeb/Log/nameMethods.txt';
        
     	if (1){
        	try {
        		$zprava = "<p> WriteMethodListwsssss</p>";
        		throw new Exception($zprava);
        	}
        	catch (Exception $e) {        
        		//echo $class.$e->getMessage();    
        	}
        }
        $logger = new Zend_Log();
        //$format='%timestamp% - %priority% '. '(%priorityName%) -> %message%' . PHP_EOL;
        $format='%message%' . PHP_EOL;
        $formatter = new Zend_Log_Formatter_Simple($format);
        $writer = new Zend_Log_Writer_Stream(self::$pathNameMethodFile);
        $writer->setFormatter($formatter);
        $logger->addWriter($writer);
        $logger->log($class, Zend_Log::INFO);
        $indexMethod=1;
        foreach (get_class_methods($class) as $value) {
        	$logger->log($indexMethod."	".$value, Zend_Log::INFO);
        	//echo "<p>".$class.": ".$value."</p>";
        	$indexMethod++;
        }
            $e = File::rewind(self::$pathNameMethodFile, FILE_MODE_READ);
        	if (PEAR::isError($e)) {
    			echo 'Could not rewind the file : ' . $e->getMessage();
			} else {
    			//echo "File test.txt successfully rewound\n";
			}
			
        	$e = File::close(self::$pathNameMethodFile, FILE_MODE_READ);
			if (PEAR::isError($e)) {
   			 	echo 'Could not close file : ' . $e->getMessage();
			} else {
    			//echo "Successfully closed file test.txt\n";
			}
     }
     
     
     
     public static function deleteLogFile()
     {
     	$e = File::write(self::$pathLogFile,'', FILE_MODE_WRITE);
     	if (PEAR::isError($e))
     	{
    		echo 'Could not write to file : ' . $e->getMessage();
		} else {
    		//echo "Successfully wrote to file test.txt\n";
		}
     }



}