package org.pmdreports.report;

import java.io.IOException;
import java.io.Writer;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.List;

import org.pmdreports.manager.ComponentStatisticsCalculator;
import org.pmdreports.manager.FileManager;
import org.pmdreports.manager.StatisticsManager;
import org.pmdreports.model.Ruleset;
import org.pmdreports.model.Statistics;
import org.pmdreports.util.PMDReportsException;

/**
 * Snapshot of today's statistics.
 * 
 * @author Ben Northrop Aug 22, 2007
 */
public class ComponentComparisonReport extends AbstractReport {

	// DATA MEMBERS
	private final List<Ruleset> rulesets;

	/**
	 * Default constructor.
	 */
	public ComponentComparisonReport(List<Ruleset> rulesets) {
		this.rulesets = rulesets;
	}

	/**
	 * The file name of the report.
	 */
	public String getFileName() {
		return "ComponentQualitySnapshotToday.html";
	}

	public String getReportTitle() {
		return "Comparison (Detail)";
	}

	/**
	 * Generate the report.
	 */
	protected void writeReport(Writer writer) throws IOException {
		boolean isPerLine = true;
		StatisticsManager cm = StatisticsManager.getInstance();
		Collection<Statistics> componentStatistics = cm.getStatisticsForToday();

		final int size = componentStatistics.size();

		writer.write("<table class='statistics'>");
		ReportUtilities.writeHeader(writer, rulesets);

		for (Statistics result : componentStatistics) {
			writer.write("<tr height=\"25\">");
			writer.write("<td class='component' >&nbsp;&nbsp;&nbsp;");
			writer.write(result.getComponent().getName());
			writer.write("&nbsp;</td>");
			writeCell(writer, ReportUtilities.DEFAULT_FORMATTER.format(result
					.getNumberOfFiles()));
			writeCell(writer, ReportUtilities.DEFAULT_FORMATTER.format(result
					.getLinesOfCode()));
			writeCell(writer, ReportUtilities.DEFAULT_FORMATTER.format(result
					.getNumberOfFiles() == 0 ? 0 : result.getLinesOfCode()
					/ result.getNumberOfFiles()));
			writeCell(writer, ReportUtilities.DEFAULT_FORMATTER.format(result
					.getTotalViolations(false)));

			for (Ruleset ruleset : rulesets) {
				writeCell(writer, ruleset, result, isPerLine);
			}
			writer.write("</tr>");
		}
	}

	/**
	 * Write out a basic cell value.
	 */
	private void writeCell(Writer writer, String value) throws IOException {
		writer.write("<td class='normal'>&nbsp;");
		writer.write(value);
		writer.write("</td>");
	}

	/**
	 * Helper method to write simple integer value.
	 */
	private void writeCell(Writer writer, Ruleset ruleset, Statistics stats,
			boolean isPerLine) throws IOException {
		NumberFormat formatter = ReportUtilities.getStatisticFormatter(ruleset);
		isPerLine = !ruleset.getIsShowAbsolute();
		String cssStyle = "normalValue";
		double value = stats.getStatistic(ruleset, isPerLine).doubleValue();
		if (ruleset.getMaxViolations() != null
				&& value > ruleset.getMaxViolations()) {
			cssStyle = "highValue";
		} else if (ruleset.getMinViolations() != null
				&& value < ruleset.getMinViolations()) {
			cssStyle = "lowValue";
		}
		writer.write(" <td class=\"");
		writer.write(cssStyle);
		writer.write("\">&nbsp;");
		writer.write("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"");
		writer.write(new FileManager().getPmdHtmlFilePath(stats.getComponent(),
				ruleset, true));
		writer.write("\" class=\"");
		writer.write(cssStyle);
		writer.write("\">");
		writer.write(formatter.format(value));
		writer.write("</a>");
		writer.write("&nbsp;</td>");
	}

	// OVERRIDDEN METHODS
	public boolean isComponentReport() {
		return true;
	}

	public boolean isProjectReport() {
		return false;
	}
}
