package cn.edu.seu.ise.dca.app.php.parser.test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;
import org.junit.BeforeClass;
import org.junit.Test;
import org.tmatesoft.svn.core.io.SVNRepository;

import cn.edu.seu.ise.dca.analysis.SVNCollector;
import cn.edu.seu.ise.dca.analysis.RepositoryFactory;
import cn.edu.seu.ise.dca.app.php.model.PHPClass;
import cn.edu.seu.ise.dca.app.php.model.PHPFile;
import cn.edu.seu.ise.dca.app.php.model.PHPFunction;
import cn.edu.seu.ise.dca.app.php.parser.PHPParser;
import cn.edu.seu.ise.dca.global.Global;
import cn.edu.seu.ise.dca.util.URLUtil;

public class PHPParserTest {
	
	private static SVNRepository repository;
	
	@BeforeClass
	public static void init() {
		//Set the project you want to test
		String project = "prestashop";
		
		//Set the log properties
		PropertyConfigurator.configure( "log4j.properties" );
		
		//Get the properties defined for project settings
		String settings = "cn/edu/seu/ise/dca/properties/projects.properties";
		Properties properties = new Properties();
		InputStream in = ClassLoader.getSystemResourceAsStream(settings);  
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}  
		//Get the properties of the selected project
		String projectSettings = properties.getProperty(project);
		in = ClassLoader.getSystemResourceAsStream(projectSettings);
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//Get the information of selected project
		String projectName = properties.getProperty("name");
		String url = properties.getProperty("url");
		String username = properties.getProperty("username");
		String password = properties.getProperty("password");

		//Set the output path for checkout and file path
		Global.setOutputDir(projectName);
		
		//Set the file path of changed files downloaded for each project
		Global.setDownloadDir(projectName + "/PHP");
		
		/*
		 * Get the repository of the selected projects
		 */
		repository = RepositoryFactory.getRepository(url, username, password);
	}

	@Test
	public void testGetPHPFile() {
		/*GetPHPFile("/trunk/phase3/includes/Article.php", 1284);
		GetPHPFile("/trunk/phase3/includes/DatabaseFunctions.php", 1284);
		GetPHPFile("/trunk/phase3/includes/DefaultSettings.php", 1284);
		GetPHPFile("/trunk/phase3/includes/DifferenceEngine.php", 1284);
		GetPHPFile("/trunk/phase3/includes/FulltextStoplist.php", 1284);
		GetPHPFile("/trunk/phase3/includes/GlobalFunctions.php", 1284);
		GetPHPFile("/trunk/phase3/includes/Parser.php", 6710);
		GetPHPFile("/trunk/phase3/includes/SpecialRecentchangeslinked.php", 2687);
		GetPHPFile("/trunk/phase3/update.php", 1694);
		GetPHPFile("/trunk/phase3/update.php", 1695);
		GetPHPFile("trunk/e107_0.7/request.php", 2136);
		GetPHPFile("trunk/dotproject/lib/jpgraph/src/jpgraph_gantt.php", 309);*/
		GetPHPFile("/trunk/addressContainer.php", 320);
	}
	
	//@Test
	public void testPHPFileLines() {
		//printSizeOfLine("/trunk/phase3/includes/Title.php", 1589);
		//printSizeOfLine("/trunk/phase3/includes/Interwiki.php", 1589);
		//printSizeOfLine("/trunk/phase3/includes/DefaultSettings.php", 1695);
		printSizeOfLine("/trunk/phase3/maintenance/indexes.sql", 1350);
	}
	/**
	 * Parse the certain file according to the revision and URL 
	 * @param url
	 * @param revision
	 */
	private void GetPHPFile(String url, long revision) {
		String filePath = URLUtil.generateFilePath(Global.OUTPUT_DIR, url, revision);
		File f = new File(filePath);
		if(!f.exists()) {
			//Check the file we need to analyze
			SVNCollector collector = new SVNCollector(repository);
			collector.checkoutFile(url, revision);
		}
		PHPFile phpFile = PHPParser.getPHPFile(url, revision);
		//Print out the information of global functions
		System.out.println(phpFile);
		List<PHPFunction> globalFunctions = phpFile.getGlobalFunctions();
		if(globalFunctions.size() > 0) {
			System.out.println("The information of global functions: ");
			System.out.println("-------");
			for(PHPFunction function: globalFunctions) {
				System.out.println(function);
			}
			System.out.println("-------------------------------------------------------------------------------");
		}
		//Print out the information of classes
		List<PHPClass> classes = phpFile.getClasses();
		if(classes.size() > 0) {
			for(PHPClass phpclass : classes) {
				System.out.println(phpclass);
				System.out.println("The information of local functions in class: " + phpclass.getName());
				System.out.println("-------");
				List<PHPFunction> functions = phpclass.getFunctions();
				for(PHPFunction function : functions) {
					System.out.println(function);
				}
				System.out.println("-------------------------------------------------------------------------------");
			}
		}
		System.out.println();
		System.out.println("========================================================================================");
		System.out.println();
	}
	
	
	private void printSizeOfLine(String url, long revision) {
		String filePath = URLUtil.generateFilePath(Global.OUTPUT_DIR, url, revision);
		File f = new File(filePath);
		if(!f.exists()) {
			//Check the file we need to analyze
			SVNCollector collector = new SVNCollector(repository);
			collector.checkoutFile(url, revision);
		}
		PHPFile phpFile = PHPParser.getPHPFile(url, revision);
		System.out.println("The total lines are " + phpFile.getLines());
		System.out.println("The lines without comments and blanks are " + phpFile.getActualLines());
	}
}
