/**
 * 
 */
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.Component;
import org.pmdreports.model.Statistics;

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

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

	public String getReportTitle() {
		return "Trend (Cumulative)";
	}

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

		// Don't generate trend report if only one day's worth of data
		if (statsManager.hasOnlyOneDaysStatistics()) {
			super.writeNoGraph(writer);
			return;
		}

		double maxTotalLinesOfCode = statsManager.getMaxTotalLinesOfCode();
		Collection<Component> components = statsManager.getComponents();

		long maxLOC = statsManager.getMaxNumberOfLines();
		double maxViolations = statsManager.getMaxViolations();
		double minViolations = statsManager.getMinViolations();
		for (Component component : components) {

			Collection<Statistics> compStats = statsManager
					.getStatistics(component);
			super.writeGraphTop(writer, component.getName());
			Statistics initialStats = statsManager
					.getEarliestStatistics(component);
			Long initialLOC = initialStats.getLinesOfCode();
			Double initialViolations = initialStats.getTotalViolations(false);

			super.initDimensions(25, 100, 0, maxLOC, initialLOC, minViolations,
					maxViolations, initialViolations);

			super.writeLeftYAxis(writer);

			// Write out the graph
			int i = 2;
			for (Statistics stats : compStats) {
				super.writeBar(writer, stats.getTotalViolations(false)
						- initialViolations, false);
				super.writeBar(writer, stats.getLinesOfCode() - initialLOC,
						true);
				super.writeSpacer(writer);
				i += 3;
			}
			super.writeRightYAxis(writer);
			super.writeXAxis(writer, compStats, i);
		}

	}

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

	public boolean isProjectReport() {
		return false;
	}
}
