package cn.edu.seu.ise.dca.analysis.util;

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

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

import cn.edu.seu.ise.dca.analysis.RepositoryFactory;
import cn.edu.seu.ise.dca.analysis.SVNCollector;
import cn.edu.seu.ise.dca.global.Global;
import cn.edu.seu.ise.dca.model.Revision;
import cn.edu.seu.ise.dca.util.ExcelReader;

public class ChangedLocCalculation {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//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 schemaUrl = properties.getProperty("schema");
		String username = properties.getProperty("username");
		String password = properties.getProperty("password");
		String includes = properties.getProperty("includeURL");
		String excludes = properties.getProperty("excludeURL");
		long startRevision = Long.parseLong(properties.getProperty("start"));
		long endRevision = Long.parseLong(properties.getProperty("end"));
		
		List<String> includedUrls = new ArrayList<String>();
		List<String> excludedUrls = new ArrayList<String>();
		//Set the included path for the project
		if(includes.length() > 0) {
			String[] splitPaths = includes.split(",");
			if(splitPaths.length > 0) {
				for(String path : splitPaths) {
					if(path.trim() != "") {
						includedUrls.add(path.trim());
					}	
				}
			}
		}
		
		//Set the excluded path for the project
		if(excludes.length() > 0) {
			String[] splitPaths = excludes.split(",");
			if(splitPaths.length > 0) {
				for(String path : splitPaths) {
					if(path.trim() != "") {
						excludedUrls.add(path.trim());
					}
				}
			}
		}
		
		//Set the output path for checkout and file path
		Global.setOutputDir(projectName);
		//Set the file path of generated statistical file
		//String reportPath = Global.OUTPUT_DIR + projectName + "_Rev" + startRevision + "_" + endRevision + ".xls";
		//Set the file path of changed files downloaded for each project
		Global.setDownloadDir(projectName + "/PHP");
		
		//Set the list of selected revisions
		List<Long> selectedRevisions = new ArrayList<Long>();
		String filePath = Global.OUTPUT_DIR + projectName + "_selectedRevisions.xls";
		ExcelReader reader = new ExcelReader(filePath);
		for(int row = 0; row < reader.getRows(); row++) {
			selectedRevisions.add(Long.parseLong(reader. read(0, row)));
		}
		
		//Connect to the selected SVN repository
		SVNRepository repository = RepositoryFactory.getRepository(url, username, password);
		SVNCollector collector = new SVNCollector(repository);
		List<Revision> revisions = collector.getRevisions(new String[] {}, startRevision, endRevision, null, includedUrls, excludedUrls, true);
		
		long changedLoC = 0;
		for(Revision revision : revisions) {
			//Revision filterRivision = RFilter.filter(revision);
			changedLoC += revision.sizeOfChangedLines(true, true);
		}
		System.out.println("Total Changed Lines in total revisions:" + changedLoC);	
		
		
		revisions = collector.getRevisions(new String[] {schemaUrl}, startRevision, endRevision, selectedRevisions, includedUrls, excludedUrls,  true);
		changedLoC = 0;
		
		for(Revision revision : revisions) {
			changedLoC += revision.sizeOfChangedLines(true, false);
		}
		System.out.println("Total Changed Lines in all valid DB revisions:" + changedLoC);
	}
}
