/**
 * 
 */
package org.pmdreports.report;

import java.io.IOException;
import java.io.Writer;
import java.util.Collection;

import org.pmdreports.manager.StatisticsManager;
import org.pmdreports.model.Statistics;

/**
 * @author Ben Northrop
 * 
 */
public class CodeSizeSnapshotGraph extends AbstractGraph {

	/**
	 * @see org.pmdreports.report.AbstractReport#getFileName()
	 */
	public String getFileName() {
		return "CodeSizeSnapshotReport.html";
	}

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

	/**
	 * @see org.pmdreports.report.AbstractReport#writeReport(java.io.Writer)
	 */
	@Override
	protected void writeReport(Writer writer) throws IOException {
		StatisticsManager statsManager = StatisticsManager.getInstance();
		Collection<Statistics> todaysStats = statsManager
				.getStatisticsForToday();

		long maxLinesOfCode = 0;
		for (Statistics stats : todaysStats) {
			if (stats.getLinesOfCode() > maxLinesOfCode) {
				maxLinesOfCode = stats.getLinesOfCode();
			}
		}
		double maxViolations = 0;
		for (Statistics stats : todaysStats) {
			if (stats.getTotalViolations(false) > maxViolations) {
				maxViolations = stats.getTotalViolations(false);
			}
		}

		super.initDimensions(30, 400, maxLinesOfCode, maxViolations);

		writer
				.write("<table cellspacing='0' cellpadding='0' class='graph' border='1'><tr>");
		super.writeLeftYAxis(writer);
		super.writeSpacer(writer);
		for (Statistics stats : todaysStats) {
			super.writeBar(writer, stats.getLinesOfCode(), true);
			super.writeBar(writer, stats.getTotalViolations(false), false);
			super.writeSpacer(writer);
		}
		super.writeRightYAxis(writer);

		writer.write("</tr>");
		ReportUtilities.writeComponentNames(writer, todaysStats, "3");
		writer.write("</table>");

	}

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

	public boolean isProjectReport() {
		return false;
	}
}
