package de.florianmarsch.fussballmanager.live.ticker;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import de.florianmarsch.fussballmanager.db.entity.lineup.ClassicKaderFactory;
import de.florianmarsch.fussballmanager.db.entity.match.Match;
import de.florianmarsch.fussballmanager.db.entity.match.MatchService;
import de.florianmarsch.fussballmanager.db.entity.matchday.Matchday;
import de.florianmarsch.fussballmanager.db.entity.tick.Tick;
import de.florianmarsch.fussballmanager.db.entity.tick.TickService;
import de.florianmarsch.fussballmanager.db.entity.trainer.Trainer;

public class GoalResolver {

	public List<Event> getGoals(Matchday aMatchday) {
		List<Event> returnEvents = new ArrayList<Event>();
		TickService liveTicker = new TickService();
		ClassicKaderFactory ckf = new ClassicKaderFactory();

		List<Match> matches = new MatchService().getAllByMatchday(aMatchday);

		List<Tick> ticks = liveTicker.getAllByMatchday(aMatchday);

		Map<Trainer, Set<String>> allPlayer = null;
		for (Tick tick : ticks) {
			if(allPlayer == null){
				// Load them only if there are goals to process
				allPlayer = ckf.getAll(aMatchday);
			}
			try {
				for (Trainer trainer : allPlayer.keySet()) {
					Set<String> team = allPlayer.get(trainer);

					if (team.contains(tick.getName())) {

						Match match = getMatch(matches, trainer);

						Event event = new Event();
						event.setEvent(tick.getEvent());
						event.setId(tick.getId());
						event.setName(tick.getName());
						event.setTrainer(trainer);
						event.setMatch(match);
						event.addEventToMatch();
						returnEvents.add(event);

					}

				}

			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return returnEvents;
	}

	public Match getMatch(List<Match> matches, Trainer trainer) {
		Match current = null;
		for (Match tempMatch : matches) {
			if (tempMatch.getHome().equals(trainer) || tempMatch.getGuest().equals(trainer)) {
				current = tempMatch;
			}
		}
		return current;
	}

}

@@ -72,24 +72,50 @@
 .ArrayList;%0A
+import java.util.HashSet;%0A
 import java.
@@ -1096,16 +1096,17 @@
  %7B%0A%09%09%09if
+ 
 (allPlay
@@ -1116,16 +1116,17 @@
 == null)
+ 
 %7B%0A%09%09%09%09//
@@ -1297,16 +1297,36 @@
  team = 
+new HashSet%3CString%3E(
 allPlaye
@@ -1331,32 +1331,33 @@
 yer.get(trainer)
+)
 ;%0A%0A%09%09%09%09%09if (team
@@ -1381,16 +1381,54 @@
 tName())
+ %7C%7C containsPart(team,tick.getName() )
 ) %7B%0A%0A%09%09%09
@@ -1472,17 +1472,122 @@
 ainer);%0A
-%0A
+%09%09%09%09%09%09if (match != null) %7B%0A%09%09%09%09%09%09%09// Trainer musst be at a gameday. may happen on semi%0A%09%09%09%09%09%09%09// finals%0A%0A%09
 %09%09%09%09%09%09Ev
@@ -1607,24 +1607,25 @@
 ew Event();%0A
+%09
 %09%09%09%09%09%09event.
@@ -1647,24 +1647,25 @@
 etEvent());%0A
+%09
 %09%09%09%09%09%09event.
@@ -1683,32 +1683,33 @@
 getId());%0A%09%09%09%09%09%09
+%09
 event.setName(ti
@@ -1719,24 +1719,25 @@
 getName());%0A
+%09
 %09%09%09%09%09%09event.
@@ -1759,24 +1759,25 @@
 ner);%0A%09%09%09%09%09%09
+%09
 event.setMat
@@ -1787,16 +1787,17 @@
 match);%0A
+%09
 %09%09%09%09%09%09ev
@@ -1825,16 +1825,17 @@
 ;%0A%09%09%09%09%09%09
+%09
 returnEv
@@ -1851,16 +1851,23 @@
 event);%0A
+%09%09%09%09%09%09%7D
 %0A%09%09%09%09%09%7D%0A
@@ -1964,16 +1964,262 @@
 ts;%0A%09%7D%0A%0A
+%09boolean containsPart(Set%3CString%3E team, String name) %7B%0A%09%09String%5B%5D split = name.split(%22 %22);%0A%09%09String attempt = split%5Bsplit.length-1%5D;%0A%09%09%0A%09%09for (String player : team) %7B%0A%09%09%09if(player.contains(attempt))%7B%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%0A
 %09public 

fix for last gameday