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

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

import org.pmdreports.manager.StatisticsManager;

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

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

	public String getReportTitle() {
		return "Trend (Per Line)";
	}

	/**
	 * @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;
		}
		
		long maxLOC = statsManager.getDailyMaxTotalLinesOfCode();
		double maxViolations = statsManager.getDailyMaxTotalViolations();
		double maxPerLineViolations = (maxViolations / maxLOC) * 1000;
		
		super.initDimensions(50, 500, 100);

		Collection<Date> dates = statsManager.getDates();

		double initialViolations = 0d;
		long initialLOC = 0l;
		for (Date date : dates) {
			initialViolations = statsManager.getDailyTotalViolations(date);
			initialLOC = statsManager.getDailyTotalLinesOfCode(date);
			break;
		}
		int i = 1;

		super.writeGraphTop(writer, "Project");
		super.writeLeftYAxis(writer);
		for (Date date : dates) {
			double violationsPerLine = (statsManager
					.getDailyTotalViolations(date) / statsManager
					.getDailyTotalLinesOfCode(date)) * 1000;
			super.writeBar(writer, violationsPerLine, false);
			super.writeSpacer(writer);
			super.writeSpacer(writer);
			i += 3;
		}
		super.writeRightYAxis(writer);
		super.writeXAxisForDates(writer, dates, i);

	}

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

	public boolean isProjectReport() {
		return true;
	}
}
