package ca.ubc.ece.salt.sdjsb.learning;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.List;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

import ca.ubc.ece.salt.sdjsb.batch.GitProjectAnalysis;
import ca.ubc.ece.salt.sdjsb.batch.GitProjectAnalysisException;

public class LearningAnalysisMain {

	/** The directory where repositories are checked out. **/
	public static final String CHECKOUT_DIR =  new String("repositories");
	
	/**
	 * Creates the learning data set for extracting repair patterns.
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
		LearningAnalysisOptions options = new LearningAnalysisOptions();
		CmdLineParser parser = new CmdLineParser(options);

		try {
			parser.parseArgument(args);
		} catch (CmdLineException e) {
			LearningAnalysisMain.printUsage(e.getMessage(), parser);
			return;
		}
		
		/* Print the help page. */
		if(options.getHelp()) {
			LearningAnalysisMain.printHelp(parser);
			return;
		}
		
		/* Create the runner that will run the analysis. */
		LearningAnalysisRunner runner = new LearningAnalysisRunner(options.getDataSetPath(), options.getSupplementaryFolder());
		
        GitProjectAnalysis gitProjectAnalysis;

		/* A URI was given. */
		if(options.getURI() != null) {

			try {
                gitProjectAnalysis = GitProjectAnalysis.fromURI(options.getURI(), CHECKOUT_DIR, runner);
			} 
			catch(GitProjectAnalysisException e) {
                LearningAnalysisMain.printUsage(e.getMessage(), parser);
                return;
			}
			
			gitProjectAnalysis.analyze();

		}
		/* A list of URIs was given. */
		else if(options.getRepoFile() != null) {
			
			/* Parse the file into a list of URIs. */
			List<String> uris = new LinkedList<String>();
			
			try(BufferedReader br = new BufferedReader(new FileReader(options.getRepoFile()))) {
			    for(String line; (line = br.readLine()) != null; ) {
			    	uris.add(line);
			    }
			}
			catch(Exception e) {
				System.err.println("Error while reading URI file: " + e.getMessage());
			}
			
			/* Analyze all projects. */
			for(String uri : uris) {

				try {
					gitProjectAnalysis = GitProjectAnalysis.fromURI(uri, LearningAnalysisMain.CHECKOUT_DIR, runner);
				} 
				catch(GitProjectAnalysisException e) {
					LearningAnalysisMain.printUsage(e.getMessage(), parser);
					return;
				}
				
				/* Perform the analysis (this may take some time) */
				gitProjectAnalysis.analyze();
				
			}
			
		}
		else {
			System.out.println("No repository given.");
			LearningAnalysisMain.printUsage("No repository given.", parser);
			return;
		}

	}

	/**
	 * Prints the help file for main.
	 * @param parser The args4j parser.
	 */
	private static void printHelp(CmdLineParser parser) {
        System.out.print("Usage: DataSetMain ");
        parser.setUsageWidth(Integer.MAX_VALUE);
        parser.printSingleLineUsage(System.out);
        System.out.println("\n");
        parser.printUsage(System.out);
        System.out.println("");
        return;
	}
	
	/**
	 * Prints the usage of main.
	 * @param error The error message that triggered the usage message.
	 * @param parser The args4j parser.
	 */
	private static void printUsage(String error, CmdLineParser parser) {
        System.out.println(error);
        System.out.print("Usage: DataSetMain ");
        parser.setUsageWidth(Integer.MAX_VALUE);
        parser.printSingleLineUsage(System.out);
        System.out.println("");
        return;
	}

}

@@ -519,18 +519,17 @@
 ries%22);%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -628,17 +628,16 @@
 xception
- 
 %0A%09 */%0A%09p
@@ -693,19 +693,17 @@
 ption %7B%0A
-%09%09
 %0A
+
 %09%09Learni
@@ -958,27 +958,25 @@
 return;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09/* Print t
@@ -1076,19 +1076,17 @@
 rn;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09/* Cre
@@ -1253,19 +1253,17 @@
 der());%0A
-%09%09
 %0A
+
         
@@ -1475,25 +1475,24 @@
 unner);%0A%09%09%09%7D
- 
 %0A%09%09%09catch(Gi
@@ -1624,20 +1624,17 @@
 n;%0A%09%09%09%7D%0A
-%09%09%09
 %0A
+
 %09%09%09gitPr
@@ -1740,20 +1740,17 @@
 null) %7B%0A
-%09%09%09
 %0A
+
 %09%09%09/* Pa
@@ -1835,20 +1835,17 @@
 ing%3E();%0A
-%09%09%09
 %0A
+
 %09%09%09try(B
@@ -2121,25 +2121,34 @@
 age());%0A
+%09%09%09%09return;%0A
 %09%09%09%7D%0A
-%09%09%09
 %0A
+
 %09%09%09/* An
@@ -2316,17 +2316,16 @@
 );%0A%09%09%09%09%7D
- 
 %0A%09%09%09%09cat
@@ -2369,61 +2369,35 @@
 %09%09%09%09
-LearningAnalysisMain.printUsage(e.getMessage(), parse
+e.printStackTrace(System.er
 r);%0A
@@ -2405,22 +2405,24 @@
 %09%09%09%09
-return
+continue
 ;%0A%09%09%09%09%7D%0A
 %09%09%09%09
@@ -2417,21 +2417,17 @@
 ;%0A%09%09%09%09%7D%0A
-%09%09%09%09
 %0A
+
 %09%09%09%09/* P
@@ -2513,22 +2513,15 @@
 ();%0A
-%09%09%09%09
 %0A
+
 %09%09%09%7D%0A
-%09%09%09
 %0A
+
 %09%09%7D%0A
@@ -3072,17 +3072,16 @@
 urn;%0A%09%7D%0A
-%09
 %0A%09/**%0A%09 

Update LearningAnalysis so it doesn't exit on git exceptions