/**
 * 
 */
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 ProjectTrendGraphNonCumulative extends AbstractGraph {

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

	public String getReportTitle() {
		return "Trend (Non-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;
		}
		
		long maxLOC = statsManager.getDailyMaxTotalLinesOfCode();
		double maxViolations = statsManager.getDailyMaxTotalViolations();
		double minViolations = statsManager.getDailyMinTotalViolations();

		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 = 2;

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

		super.writeGraphTop(writer, "Project");
		super.writeLeftYAxis(writer);
		for (Date date : dates) {
			super.writeBar(writer, statsManager.getDailyTotalViolations(date)
					- initialViolations, false);
			super.writeBar(writer, statsManager.getDailyTotalLinesOfCode(date)
					- initialLOC, true);
			super.writeSpacer(writer);
			initialLOC = statsManager.getDailyTotalLinesOfCode(date);
			initialViolations = statsManager.getDailyTotalViolations(date);
			i += 3;
		}
		super.writeRightYAxis(writer);
		super.writeXAxisForDates(writer, dates, i);

	}

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

	public boolean isProjectReport() {
		return true;
	}
}
