package loc.statistics;

import loc.filter.*;

import java.util.*;

public class StatisticsSerializer implements IStatisticsSerializer {
	public static char space              = ' ';
	public static String separator        = " - ";
	public static String lineBreak        = "\n";
	public static char underlineCharacter = '-';
//	public static int underlineLength     = 10;
	private Statistics statistics;
	private SortedSet<Map.Entry<Filter, FileStatistics>> sortedStatistics;
	private StringBuffer buffer;
	private List<String> filterStrings;
	private List<FileStatistics> fileStatistics;

	public StatisticsSerializer(Statistics statistics) {
		this.statistics = statistics;
		sortedStatistics = new TreeSet<>(new StatisticsComparator());
		sortedStatistics.addAll(statistics.getFilterFileStatisticsMap().entrySet());
	}

	@Override
	public String serialize() throws Exception {
		buffer = new StringBuffer();
		filterStrings = new ArrayList<>();
		fileStatistics = new ArrayList<>();
		int maxFilterStringLength = 0;
		for (Map.Entry<Filter, FileStatistics> entry: sortedStatistics) {
			Filter filter = entry.getKey();
			String filterString = filter.getSerializer().serialize(filter);
			filterStrings.add(filterString);
			fileStatistics.add(entry.getValue());
			maxFilterStringLength = Math.max(maxFilterStringLength, filterString.length());
		}
		appendSerialized(maxFilterStringLength);
		return buffer.toString();
	}

	private void appendSerialized(int maxFilterStringLength) {
		buffer.append("Total - ");
		buffer.append(statistics.getTotalLineCount());
		buffer.append(" lines in ");
		buffer.append(statistics.getTotalFileCount());
		buffer.append(" files");
		buffer.append('\n');
		append(underlineCharacter, maxFilterStringLength);
		buffer.append('\n');
		for (int i = 0; i < filterStrings.size(); i++) {
			String filterString = filterStrings.get(i);
			buffer.append(filterString);
			append(space, maxFilterStringLength - filterString.length());
			buffer.append(separator);
			append(fileStatistics.get(i));
			buffer.append(lineBreak);
		}
	}

	private void append(char c, int amount) {
		for (int i = 0; i < amount; i++) {
			buffer.append(c);
		}
	}

	private void append(FileStatistics fileStatistics) {
		buffer.append(fileStatistics.lineCount);
		buffer.append(" \tlines in ");
		buffer.append(fileStatistics.fileCount);
		buffer.append(" \tfiles");
	}
}

@@ -321,54 +321,8 @@
 -';%0A
-//%09public static int underlineLength     = 10;
 %0A%09pr
@@ -534,16 +534,90 @@
 tistics;
+%0A%09private int maxLineCountLength = 0;%0A%09private int maxFileCountLength = 0;
 %0A%0A%09publi
@@ -1444,24 +1444,417 @@
 ring();%0A%09%7D%0A%0A
+%09private void calculateMaxLineAndFileCounts() %7B%0A%09%09int maxLineCount = 0;%0A%09%09int maxFileCount = 0;%0A%09%09for (FileStatistics stats: fileStatistics) %7B%0A%09%09%09maxLineCount = Math.max(maxLineCount, stats.lineCount);%0A%09%09%09maxFileCount = Math.max(maxFileCount, stats.fileCount);%0A%09%09%7D%0A%09%09maxLineCountLength = String.valueOf(maxLineCount).length();%0A%09%09maxFileCountLength = String.valueOf(maxFileCount).length();%0A%09%7D%0A%0A
 %09private voi
@@ -1923,20 +1923,45 @@
 d(%22Total
- - %22
+%22);%0A%09%09buffer.append(separator
 );%0A%09%09buf
@@ -2123,28 +2123,33 @@
 ffer.append(
-'%5Cn'
+lineBreak
 );%0A%09%09append(
@@ -2212,14 +2212,56 @@
 end(
-'%5Cn');
+lineBreak);%0A%0A%09%09calculateMaxLineAndFileCounts();%0A
 %0A%09%09f
@@ -2750,32 +2750,121 @@
 ics.lineCount);%0A
+%09%09append(space, maxLineCountLength - String.valueOf(fileStatistics.lineCount).length());%0A
 %09%09buffer.append(
@@ -2865,18 +2865,16 @@
 ppend(%22 
-%5Ct
 lines in
@@ -2921,16 +2921,105 @@
 Count);%0A
+%09%09append(space, maxFileCountLength - String.valueOf(fileStatistics.fileCount).length());%0A
 %09%09buffer
@@ -3032,10 +3032,8 @@
 d(%22 
-%5Ct
 file

Fix Statistics serialization Add automatic indentation