package org.ossmeter.metricprovider.trans.dailycommits;

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.dailycommits.model.DailyCommits;
import org.ossmeter.metricprovider.trans.dailycommits.model.Day;
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 DailyCommitsMetricProvider implements ITransientMetricProvider<DailyCommits> {
	
	protected List<IMetricProvider> uses;
	protected MetricProviderContext context;
	
	@Override
	public String getIdentifier() {
		return DailyCommitsMetricProvider.class.getCanonicalName();
	}

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

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

	@Override
	public String getSummaryInformation() {
		return "Commits group by the day on 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 DailyCommits adapt(DB db) {
		return new DailyCommits(db);
	}

	/**
	 * FIXME: Currently this will calculate it for ALL repos.
	 */
	@Override
	public void measure(Project project, ProjectDelta delta, DailyCommits db) {
		
		String[] daysOfWeek = new String[]{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

		for (String d : daysOfWeek) {
			Day day = db.getDays().findOneByName(d);
			if (day == null) {
				day = new Day();
				day.setName(d);
				day.setNumberOfCommits(0);
				db.getDays().add(day);
				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()){
				try {
					Date date = new Date(commit.getDate());
					Calendar cal = Calendar.getInstance();
					cal.setTime(date.toJavaDate());
					int dow = cal.get(Calendar.DAY_OF_WEEK);
					
					String dayName = daysOfWeek[dow];
					
					Day day = db.getDays().findOneByName(dayName);
					day.setNumberOfCommits(day.getNumberOfCommits()+1);
					db.sync();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

@@ -2799,24 +2799,237 @@
 %09%09%09%7D%0A%09%09%7D%0A%09%09%0A
+%09%09// 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 (Repos
@@ -3051,24 +3051,25 @@
 repos) %7B%0A%09%09%09
+%09
 for (CommitD
@@ -3099,16 +3099,17 @@
 its())%7B%0A
+%09
 %09%09%09%09try 
@@ -3115,16 +3115,17 @@
  %7B%0A%09%09%09%09%09
+%09
 Date dat
@@ -3152,24 +3152,25 @@
 getDate());%0A
+%09
 %09%09%09%09%09Calenda
@@ -3206,16 +3206,17 @@
 );%0A%09%09%09%09%09
+%09
 cal.setT
@@ -3235,24 +3235,25 @@
 avaDate());%0A
+%09
 %09%09%09%09%09int dow
@@ -3291,22 +3291,24 @@
 );%0A%09%09%09%09%09
+%09
 %0A%09%09%09%09%09
+%09
 String d
@@ -3334,25 +3334,27 @@
 %5Bdow%5D;%0A%09%09%09%09%09
-%0A
+%09%0A%09
 %09%09%09%09%09Day day
@@ -3393,16 +3393,17 @@
 yName);%0A
+%09
 %09%09%09%09%09day
@@ -3452,24 +3452,25 @@
 ()+1);%0A%09%09%09%09%09
+%09
 db.sync();%0A%09
@@ -3464,24 +3464,25 @@
 %09db.sync();%0A
+%09
 %09%09%09%09%7D catch 
@@ -3498,24 +3498,25 @@
 eption e) %7B%0A
+%09
 %09%09%09%09%09e.print
@@ -3525,24 +3525,31 @@
 ackTrace();%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A%09%09%09%7D%0A%09

Fix for daily commits