package prope.reporting;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public final class ReportWriter {

	private final Report report;

	private final String excelDelimiter = ";";

	private final String rDelimiter = ",";

	public ReportWriter(Report report) {
		this.report = report;
	}

	public void printToConsole() {
		for (ReportEntry entry : report) {
			System.out.println(entry);
		}
	}

	public void writeToExcelFile(String outputFile) {
		writeToFile(outputFile, excelDelimiter);
	}

	private void writeToFile(String outputFile, String delimiter) {
		try (PrintWriter writer = new PrintWriter(new FileWriter(outputFile))) {
			printHeader(writer, delimiter);
			printBody(writer, delimiter);
		} catch (IOException e) {
			System.err.println("Could no write to file " + outputFile + ": "
					+ e.getMessage());
			e.printStackTrace();
		}
	}

	private void printHeader(PrintWriter writer, String delimiter) {
		writer.print("filename" + delimiter);
		for (String varName : report.getEntries().get(0).getVariableNames()) {
			writer.print(varName + delimiter);
		}
		writer.println();
	}

	private void printBody(PrintWriter writer, String delimiter) {
		for (ReportEntry entry : report) {
			writer.println(entry.toStringWithSeparator(delimiter));
		}
	}

	public void writeToRFile(String outputFile) {
		writeToFile(outputFile, rDelimiter);
	}
}

@@ -471,32 +471,181 @@
 g outputFile) %7B%0A
+%09%09if(report.getEntries().isEmpty())%7B%0A%09%09%09System.out.println(%22There is nothing to write... Did you use the correct analysis type?%22);%0A%09%09%09return;%0A%09%09%7D%0A%09%09%0A
 %09%09writeToFile(ou
@@ -1140,16 +1140,18 @@
 imiter);
+%09%09
 %0A%09%09for (

Print a warning if no relevant files have been found