package org.ossmeter.metricprovider.trans.hourlycommits;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import org.ossmeter.metricprovider.trans.commits.CommitsTransientMetricProvider;
import org.ossmeter.metricprovider.trans.commits.model.CommitData;
import org.ossmeter.metricprovider.trans.commits.model.Commits;
import org.ossmeter.metricprovider.trans.commits.model.RepositoryData;
import org.ossmeter.metricprovider.trans.hourlycommits.model.Hour;
import org.ossmeter.metricprovider.trans.hourlycommits.model.HourlyCommits;
import org.ossmeter.platform.Date;
import org.ossmeter.platform.IMetricProvider;
import org.ossmeter.platform.ITransientMetricProvider;
import org.ossmeter.platform.MetricProviderContext;
import org.ossmeter.platform.delta.ProjectDelta;
import org.ossmeter.repository.model.Project;

import com.mongodb.DB;

public class HourlyCommitsMetricProvider implements ITransientMetricProvider<HourlyCommits> {
	
	protected List<IMetricProvider> uses;
	protected MetricProviderContext context;
	
	@Override
	public String getIdentifier() {
		return HourlyCommitsMetricProvider.class.getCanonicalName();
	}

	@Override
	public String getShortIdentifier() {
		return "hourlycommits";
	}

	@Override
	public String getFriendlyName() {
		return "Commits by hour";
	}

	@Override
	public String getSummaryInformation() {
		return "Commits group by the hour of the day in which they occurred.";
	}

	@Override
	public boolean appliesTo(Project project) {
		return project.getVcsRepositories().size() > 0;
	}

	@Override
	public void setUses(List<IMetricProvider> uses) {
		this.uses = uses;
	}

	@Override
	public List<String> getIdentifiersOfUses() {
		return Arrays.asList(CommitsTransientMetricProvider.class.getCanonicalName());
	}

	@Override
	public void setMetricProviderContext(MetricProviderContext context) {
		this.context = context;
	}

	@Override
	public HourlyCommits adapt(DB db) {
		return new HourlyCommits(db);
	}

	/**
	 * FIXME: Currently this will calculate it for ALL repos.
	 */
	@Override
	public void measure(Project project, ProjectDelta delta, HourlyCommits db) {
		
		String[] hoursOfDay = new String[]{"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"};
		for (String h : hoursOfDay) {
			Hour hour = db.getHours().findOneByHour(h+":00");
			if (hour == null) {
				hour = new Hour();
				hour.setHour(h+":00");
				hour.setNumberOfCommits(0);
				db.getHours().add(hour);
				db.sync();
			}
		}
		
		List<RepositoryData> repos = new ArrayList<RepositoryData>();
		
		for (IMetricProvider used : uses) {
			Commits usedCommits =  ((CommitsTransientMetricProvider)used).adapt(context.getProjectDB(project));
			for (RepositoryData rd : usedCommits.getRepositories()) {
				repos.add(rd);
			}
		}
		
		for (RepositoryData rd : repos) {
			for (CommitData commit : rd.getCommits()){
				String hourName = commit.getTime().split(":")[0];
				
				Hour hour = db.getHours().findOneByHour(hourName + ":00");
				hour.setNumberOfCommits(hour.getNumberOfCommits()+1);
				db.sync();
			}
		}
	}
}

@@ -55,41 +55,8 @@
 s;%0A%0A
-import java.text.ParseException;%0A
 impo
@@ -108,35 +108,8 @@
 ys;%0A
-import java.util.Calendar;%0A
 impo
@@ -558,43 +558,8 @@
 ts;%0A
-import org.ossmeter.platform.Date;%0A
 impo
@@ -2802,17 +2802,227 @@
 %7D%0A%09%09%7D%0A%09%09
-%0A
+// QUICKFIX: needed because COmmitsTransientMetricPRovider wno't clear %0A%09%09// old data if there are no vcs deltas that day and so this will count old data%0A%09%09if (delta.getVcsDelta().getRepoDeltas().size() %3E0) %7B %0A%09
 %09%09for (R
@@ -3049,16 +3049,17 @@
 epos) %7B%0A
+%09
 %09%09%09for (
@@ -3100,16 +3100,17 @@
 ))%7B%0A%09%09%09%09
+%09
 String h
@@ -3155,21 +3155,23 @@
 0%5D;%0A%09%09%09%09
+%09
 %0A%09%09%09%09
+%09
 Hour hou
@@ -3217,32 +3217,33 @@
 e + %22:00%22);%0A%09%09%09%09
+%09
 hour.setNumberOf
@@ -3280,16 +3280,17 @@
 s()+1);%0A
+%09
 %09%09%09%09db.s
@@ -3288,30 +3288,36 @@
 %09%09%09%09%09db.sync();%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%7D%0A

Fix for horuly commits.