import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import java.util.LinkedList;
import java.util.stream.Stream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;

public class ResultParser {
	private static LinkedList<String> summaryTable = new LinkedList<>();
	private static String basePath = "";
	private static String configFile = "/jmc.mods";
	private static String resultAction = "";

	private static void parseResult(String line) {
		String xmlPath = basePath + "/reports/" + line + "/spotbugsXml.xml";
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		String tagName = "FindBugsSummary";
		try {

			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(xmlPath);
			doc.getDocumentElement().normalize();
			NodeList nList = doc.getElementsByTagName(tagName);

			for (int temp = 0; temp < nList.getLength(); temp++) {
				Node nNode = nList.item(temp);
				if (tagName.equals(nNode.getNodeName())) {
					if (nNode.getNodeType() == Node.ELEMENT_NODE) {
						Element eElement = (Element) nNode;

						String num_packages = eElement.getAttribute("num_packages");
						String total_classes = eElement.getAttribute("total_classes");
						String total_size = eElement.getAttribute("total_size");
						String clock_seconds = eElement.getAttribute("clock_seconds");
						String referenced_classes = eElement.getAttribute("referenced_classes");
						String vm_version = eElement.getAttribute("vm_version");
						String total_bugs = eElement.getAttribute("total_bugs");
						String java_version = eElement.getAttribute("java_version");
						String gc_seconds = eElement.getAttribute("gc_seconds");
						String peak_mbytes = eElement.getAttribute("peak_mbytes");
						String timestamp = eElement.getAttribute("timestamp");

						String tableRow =

								"<tr>" 
								+ "<td>" + line + "</td>" 
								+ "<td>" + num_packages + "</td>" 
								+ "<td>" + total_classes + "</td>" 
								+ "<td>" + total_size + "</td>" 
								+ "<td>" + clock_seconds + "</td>" 
								+ "<td>" + referenced_classes + "</td>" 
								+ "<td>" + vm_version + "</td>" 
								+ "<td>" + total_bugs + "</td>" 
								+ "<td>" + java_version+ "</td>" 
								+ "<td>" + gc_seconds + "</td>" 
								+ "<td>" + peak_mbytes + "</td>"
								+ "<td>" + timestamp + "</td>" 
								+ "</tr>";
						summaryTable.add(tableRow + "\n");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void generateSummary(String basePath) {

		String rowHeader = "<tr>" 
				+ "<th> Project Name 		</th>" 
				+ "<th> Package Count 		</th>"
				+ "<th> Class Count 		</th>" 
				+ "<th> Size 				</th>"
				+ "<th> Clock Seconds 		</th>" 
				+ "<th> Referenced Classes	</th>"
				+ "<th> VM Version			</th>" 
				+ "<th> Bug Count 			</th>" 
				+ "<th> Java Version		</th>"
				+ "<th> GC Seconds 			</th>" 
				+ "<th> Peak Mbytes 		</th>" 
				+ "<th> Time Stamp 			</th>"
				+ "</tr>";

		summaryTable.add(rowHeader + "\n");
		try (Stream<String> stream = Files.lines(Paths.get(basePath + configFile))) {

			stream.filter(ResultParser::checkFileExists).forEach(ResultParser::parseResult);

			System.out.println("<table border=\"1\" align=\"left\">");
			summaryTable.forEach(System.out::println);
			System.out.println("</table>");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static boolean checkFileExists(String moduleName) {
		if (new java.io.File(basePath + "/reports/" + moduleName + "/spotbugsXml.xml").exists()) {
			return true;
		}
		return false;
	}

	private static void generateFileList(String basePath, String fileName) {
		System.out.println("<a href =\"summary.html\" target =\"main\"> Result Summary  </a><br>");
		try (Stream<String> stream = Files.lines(Paths.get(basePath + configFile))) {
			stream.filter(ResultParser::checkFileExists).forEach(line -> {
				System.out.println(
						"<a href =\"../reports/" + line + "/" + fileName + "\"  target =\"main\"> " + line + "</a><br>");
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// TBD Validations
		basePath = args[0];
		resultAction = args[1];
		switch (resultAction) {
		case "summary":
			generateSummary(basePath);
			break;
		case "xml":
			generateFileList(basePath, "spotbugsXml.xml");
			break;
		case "html":
			generateFileList(basePath, "spotbugs.html");
			break;
		}

	}

}