package objects;

import java.util.ArrayList;

import com.google.common.collect.Lists;

public class DaysSinceLastRaceSummary {

	ArrayList<DaysSinceLastRace> summary = Lists.newArrayList();

	public DaysSinceLastRaceSummary() {
	}

	public DaysSinceLastRaceSummary(DaysSinceLastRace lastRace) {
		if (summary.stream().filter(p -> p.getDaysSinceLastGame() == lastRace.daysSinceLastGame).findAny().isPresent()) {
			addToExisting(lastRace);
		} else {
			summary.add(lastRace);
		}
	}

	public void addStats(DaysSinceLastRace lastRace) {
		if (summary.stream().filter(p -> p.getDaysSinceLastGame() == lastRace.daysSinceLastGame).findAny().isPresent()) {
			addToExisting(lastRace);
		} else {
			summary.add(lastRace);
		}
	}

	private void addToExisting(DaysSinceLastRace lastRace) {
		final DaysSinceLastRace daysSinceLastRace = summary.stream().filter(p -> p.getDaysSinceLastGame() == lastRace.getDaysSinceLastGame()).findFirst().get();
		int racesCount = daysSinceLastRace.getRacesCount();
		int wins = daysSinceLastRace.getWins();

		racesCount += lastRace.getRacesCount();
		wins += lastRace.getWins();

		daysSinceLastRace.setPercent(wins / Float.valueOf(racesCount));
		daysSinceLastRace.setWins(wins);
		daysSinceLastRace.setRacesCount(racesCount);
	}
}
