package web.rufer.swissunihockey.matcher;

import org.junit.Before;
import org.junit.Test;
import web.rufer.swissunihockey.matcher.domain.League;
import web.rufer.swissunihockey.matcher.domain.MatchingResult;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class MatchingPrinterTest {

    private League league1;
    private League league2;
    private static final String LEAGUE_NAME_1 = "league1";
    private static final String LEAGUE_NAME_2 = "league2";
    private static final int nbrOfTeamsInLeague = 4;
    private Map<LocalDate, List<Integer>> keysPerDateMapLeague1 = new HashMap();
    private MatchingResult matchingResult = new MatchingResult();

    @Before
    public void init() {
        league1 = new League(LEAGUE_NAME_1, nbrOfTeamsInLeague, keysPerDateMapLeague1);
        league2 = new League(LEAGUE_NAME_2, nbrOfTeamsInLeague, keysPerDateMapLeague1);
        matchingResult.setMatchForCombination("0" + KeyMatcher.BLANKS + "1", 2);
    }

    @Test
    public void printResultPrintsExpectedResultToConsole() {
        ByteArrayOutputStream boas = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(boas);
        System.setOut(ps);
        MatchingPrinter.printResult(league1, league2, matchingResult);
        assertEquals(createExpectedConsoleOutput().trim(), boas.toString().trim());
    }

    private String createExpectedConsoleOutput() {
        return new StringBuilder().append(createExpectedHeader()).
                append(System.getProperty("line.separator")).append("0").
                append(KeyMatcher.BLANKS).append("1").
                append(MatchingPrinter.TWO_TABS).append(2).toString();
    }

    @Test
    public void createHeader() {
        String header = MatchingPrinter.createHeader(league1, league2);
        assertEquals(createExpectedHeader(), header);
    }

    private String createExpectedHeader() {
        return new StringBuilder().append(LEAGUE_NAME_1).
                append(MatchingPrinter.SPACES).append(LEAGUE_NAME_2).
                append(MatchingPrinter.SPACES).append(MatchingPrinter.MATCHES).toString();
    }
}

@@ -1116,24 +1116,105 @@
  + %221%22, 2);%0A
+        matchingResult.setMatchForCombination(%221%22 + KeyMatcher.BLANKS + %221%22, 1);%0A
     %7D%0A%0A    @
@@ -1765,18 +1765,195 @@
 %22)).
-append(%220%22
+%0A                append(%221%22).append(KeyMatcher.BLANKS).append(%221%22).%0A                append(MatchingPrinter.TWO_TABS).append(1).%0A                append(System.getProperty(%22line.separator%22)
 ).%0A 
@@ -1963,24 +1963,36 @@
             
+append(%220%22).
 append(KeyMa

test extended