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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;

import ca.ubc.ece.salt.sdjsb.ControlFlowDifferencing;
import ca.ubc.ece.salt.sdjsb.analysis.learning.ast.FeatureVector;
import ca.ubc.ece.salt.sdjsb.analysis.learning.ast.LearningDataSet;
import ca.ubc.ece.salt.sdjsb.analysis.learning.ast.LearningAnalysis;
import ca.ubc.ece.salt.sdjsb.batch.AnalysisMetaInformation;
import ca.ubc.ece.salt.sdjsb.batch.AnalysisRunner;

public class LearningAnalysisRunner extends AnalysisRunner {
	
	/** Stores the feature vectors that make up the data set. **/
	LearningDataSet dataset;
	
	/**
	 * @param packages The list of packages we want to learn repair patterns for.
	 */
	public LearningAnalysisRunner(List<String> packages) {
		this.dataset = new LearningDataSet(packages);
	}

	@Override
	protected void analyze(ControlFlowDifferencing cfd, AnalysisMetaInformation ami) throws Exception {

		LearningAnalysis analysis = new LearningAnalysis(this.dataset);
		cfd.analyze(analysis);

	}

	@Override
	public void printResults(String outFile, String supplementaryFolder) {
		
		/* Filters out unwanted rows and columns. */
		this.dataset.preProcess();
		
		/* Open the file stream for writing if a file has been given. */
		PrintStream stream = System.out;
		
		if(outFile != null) {
			try {
				stream = new PrintStream(new FileOutputStream(outFile));
			}
			catch (IOException e) {
				System.err.println(e.getMessage());
			}
		}
		
		/* Write the header for the feature vector. */
		stream.println(this.dataset.getFeatureVectorHeader());
		
		/* Write the data set. */
		stream.println(this.dataset.getFeatureVector());
		
		/* Write the source code to a folder so we can examine it later. */
		this.printSupplementaryFiles(supplementaryFolder);
		
	}
	
	/**
	 * Writes the source code from each of the inspected functions to a file.
	 * @param supplementaryFolder The folder to place the files in.
	 */
	private void printSupplementaryFiles(String supplementaryFolder) {

		if(supplementaryFolder != null) {
			
			for(FeatureVector featureVector : this.dataset.getFeatureVectors()) {

				/* The path to the supplementary folder may not exist. Create 
				 * it if needed. */
				File path = new File(supplementaryFolder);
				path.mkdirs();

				File src = new File(supplementaryFolder, featureVector.id + "_src.js");
				File dst = new File(supplementaryFolder, featureVector.id + "_dst.js");
				
				try (PrintStream srcStream = new PrintStream(new FileOutputStream(src));
					 PrintStream dstStream = new PrintStream(new FileOutputStream(dst));) {

					srcStream.print(featureVector.sourceCode);
					dstStream.print(featureVector.destinationCode);
					
					srcStream.close();
					dstStream.close();
					
				} catch (FileNotFoundException e) {
					System.err.println(e.getMessage());
				} 
				
			}
			
		}
		
	}

}

@@ -385,23 +385,24 @@
 Learning
-DataSet
+Analysis
 ;%0Aimport
@@ -450,32 +450,31 @@
 ast.Learning
-Analysis
+DataSet
 ;%0Aimport ca.
@@ -636,18 +636,17 @@
 unner %7B%0A
-%09
 %0A
+
 %09/** Sto
@@ -726,18 +726,17 @@
 ataset;%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -1216,19 +1216,17 @@
 lder) %7B%0A
-%09%09
 %0A
+
 %09%09/* Fil
@@ -1293,19 +1293,17 @@
 cess();%0A
-%09%09
 %0A
+
 %09%09/* Ope
@@ -1396,19 +1396,17 @@
 em.out;%0A
-%09%09
 %0A
+
 %09%09if(out
@@ -1430,16 +1430,182 @@
 %09%09try %7B%0A
+%09%09%09%09/*%0A%09%09%09%09 * The path to the output folder may not exist. Create it if%0A%09%09%09%09 * needed.%0A%09%09%09%09 */%0A%09%09%09%09File path = new File(outFile);%0A%09%09%09%09path.getParentFile().mkdirs();%0A%0A
 %09%09%09%09stre
@@ -1738,19 +1738,17 @@
 %09%09%7D%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09/* Wri
@@ -1841,27 +1841,25 @@
 rHeader());%0A
-%09%09
 %0A
+
 %09%09/* Write t
@@ -1925,19 +1925,17 @@
 tor());%0A
-%09%09
 %0A
+
 %09%09/* Wri
@@ -2053,16 +2053,13 @@
 r);%0A
-%09%09
 %0A
+
 %09%7D%0A
-%09
 %0A
+
 %09/**
@@ -2309,20 +2309,17 @@
 null) %7B%0A
-%09%09%09
 %0A
+
 %09%09%09for(F
@@ -2449,17 +2449,16 @@
 . Create
- 
 %0A%09%09%09%09 * 
@@ -2693,21 +2693,17 @@
 t.js%22);%0A
-%09%09%09%09
 %0A
+
 %09%09%09%09try 
@@ -2950,22 +2950,17 @@
 nCode);%0A
-%09%09%09%09%09
 %0A
+
 %09%09%09%09%09src
@@ -3003,14 +3003,9 @@
 ();%0A
-%09%09%09%09%09
 %0A
+
 %09%09%09%09
@@ -3090,30 +3090,20 @@
 %09%09%09%7D
- %0A%09%09%09%09
+%0A
 %0A%09%09%09%7D%0A
-%09%09%09
 %0A
+
 %09%09%7D%0A
-%09%09
 %0A%09%7D%0A

Fix LearningAnalysisRunner not creating folder to output file