package web.rufer.swissunihockey.matcher;

import web.rufer.swissunihockey.matcher.domain.MatchingResult;
import web.rufer.swissunihockey.matcher.domain.League;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

public class KeyMatcher {

    public static final String BLANKS = "       ";

    public MatchingResult findMatchesForTwoLeagues(League league1, League league2) {
        MatchingResult result = new MatchingResult();
        for (int i = 0; i < league1.getNbrOfTeamsInLeague(); i++) {
            for (int j = 0; j < league2.getNbrOfTeamsInLeague(); j++) {
                int matchesPerCombination = 0;
                Map<LocalDate, List<Integer>> keysPerDateForLeague1 = league1.getKeysWithHomeGamePerDate();
                Map<LocalDate, List<Integer>> keysPerDateForLeague2 = league2.getKeysWithHomeGamePerDate();

                for (LocalDate date : keysPerDateForLeague1.keySet()) {
                    if (keysPerDateForLeague1.containsKey(date)) {
                        if (keysForDateExistInBothLeagues(keysPerDateForLeague1, keysPerDateForLeague2, date)) {
                            if (keysPerDateForLeague1.get(date).contains(i) && keysPerDateForLeague2.get(date).contains(j)) {
                                matchesPerCombination++;
                            }
                        }
                    }
                }
                result.setMatchForCombination(i + BLANKS + j, matchesPerCombination);
            }
        }
        return result;
    }

    private boolean keysForDateExistInBothLeagues(Map<LocalDate, List<Integer>> keysPerDateForLeague1, Map<LocalDate, List<Integer>> keysPerDateForLeague2, LocalDate date) {
        return keysPerDateForLeague1.get(date) != null && keysPerDateForLeague2.get(date) != null;
    }
}

@@ -83,30 +83,22 @@
 .domain.
-MatchingResult
+League
 ;%0Aimport
@@ -138,22 +138,30 @@
 .domain.
-League
+MatchingResult
 ;%0A%0Aimpor
@@ -603,16 +603,298 @@
         
+result.setMatchForCombination(i + BLANKS + j, calculateMatchesPerCombination(league1, league2, i, j));%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    private int calculateMatchesPerCombination(League league1, League league2, int indexLeague1, int indexLeague2) %7B%0A        
 int matc
@@ -916,24 +916,16 @@
 on = 0;%0A
-        
         
@@ -1016,24 +1016,16 @@
 Date();%0A
-        
         
@@ -1121,32 +1121,24 @@
 );%0A%0A        
- 
-       
 for (LocalDa
@@ -1197,107 +1197,21 @@
     
-   
-     if (keysPerDateForLeague1.containsKey(date)) %7B%0A                        if (keysForD
+if (d
 ateExist
 InBo
@@ -1198,32 +1198,33 @@
    if (dateExist
+s
 InBothLeagues(ke
@@ -1221,16 +1221,22 @@
 Leagues(
+date, 
 keysPerD
@@ -1275,14 +1275,8 @@
 gue2
-, date
 )) %7B
@@ -1292,28 +1292,16 @@
         
-   
-         
 if (keys
@@ -1338,16 +1338,27 @@
 ntains(i
+ndexLeague1
 ) && key
@@ -1399,21 +1399,25 @@
 ins(
-j)) %7B%0A       
+indexLeague2)) %7B%0A
     
@@ -1432,21 +1432,16 @@
         
-     
 matchesP
@@ -1473,33 +1473,18 @@
         
-            %7D%0A   
+%7D%0A
         
@@ -1483,33 +1483,24 @@
             
-         
 %7D%0A          
@@ -1501,171 +1501,46 @@
     
-            %7D%0A                %7D%0A                result.setMatchForCombination(i + BLANKS + j, matchesPerCombination);%0A            %7D%0A        %7D%0A        return result
+%7D%0A        return matchesPerCombination
 ;%0A  
@@ -1568,24 +1568,17 @@
 ean 
-keysForD
+d
 ateExist
 InBo
@@ -1573,16 +1573,17 @@
 ateExist
+s
 InBothLe
@@ -1588,16 +1588,32 @@
 Leagues(
+LocalDate date, 
 Map%3CLoca
@@ -1712,24 +1712,8 @@
 gue2
-, LocalDate date
 ) %7B%0A

structure of keyMatcher optimized