package org.pmdreports.report;

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

import org.pmdreports.model.Ruleset;
import org.pmdreports.model.Statistics;
import org.pmdreports.util.StringUtil;

/**
 * Utilities for generating a report. Note that the design is not optimal (e.g.
 * writing HTML out in Java code, but is easy to implement, and by separating it
 * here, easy to refactor.
 * 
 * @author Ben Northrop Aug 27, 2007
 */
public class ReportUtilities {

	// DATA MEMBERS
	protected static final NumberFormat DEFAULT_FORMATTER = new DecimalFormat(
			"#,###,###");

	protected static final NumberFormat DECIMAL_FORMATTER = new DecimalFormat(
			"0.00");

	protected static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
			"MM/dd");

	// CONSTRUCTORS
	/**
	 * Private constructor, since utilities class.
	 */
	private ReportUtilities() {
		// do nothing
	}

	// METHODS

	/**
	 * Get the appropriate formatter for displaying the statistic, depending on
	 * if it should be shown absolute or not.
	 */
	protected static NumberFormat getStatisticFormatter(Ruleset ruleset) {
		return ruleset.getIsShowAbsolute() ? ReportUtilities.DEFAULT_FORMATTER
				: ReportUtilities.DECIMAL_FORMATTER;
	}




	/**
	 * Helper method to write title bar out.
	 */
	protected static void writeHeader(Writer writer, List<Ruleset> rulesets)
			throws IOException {
		writer.write("<tr height=\"25\">");
		writer
				.write("<th style=\"text-align: left\">&nbsp;&nbsp;Component</th>");
		writer.write("<th width=\"70\">&nbsp;&nbsp;# Files&nbsp;</th>");
		writer.write("<th width=\"60\">&nbsp;&nbsp;LoC&nbsp;</th>");
		writer.write("<th width=\"60\">&nbsp;&nbsp;Lines/File&nbsp;</th>");
		writer.write("<th width=\"60\">&nbsp;&nbsp;Violations&nbsp;</th>");
		for (Ruleset ruleset : rulesets) {
			writer
					.write("<th><a class='columnHeader' href='http://www.pmdreports.org/analysis/rulesets.php?ruleset=");
			writer.write(ruleset.getName());
			writer.write("'>");
			writer.write(ruleset.getName());
			writer.write("</a></th>");
		}
		writer.write("</tr>");
	}

	/**
	 * Write out the names of the components at the bottom of a graph.
	 */
	protected static void writeComponentNames(Writer writer,
			Collection<Statistics> allStats, String colspan) throws IOException {
		writer.write("<tr height='2'>");
		writer.write("<td colspan='1'></td>");
		for (Statistics stats : allStats) {
			writer.write("<td colspan='" + colspan + "' class='line'></td>");
		}
		writer.write("<td colspan='" + colspan + "' class='line'></td>");
		writer.write("</tr>");
		writer.write("<tr>");
		writer.write("<td colspan='2'></td>");
		for (Statistics stats : allStats) {
			writer.write("<td align='center' colspan='" + colspan + "' title='"
					+ stats.getComponent().getName() + "'>"
					+ StringUtil.truncate(stats.getComponent().getName(), 5)
					+ "</td>");
		}
		writer.write("</tr>");
	}
}
