package com.nosoftskills.predcomposer.common;

import com.nosoftskills.predcomposer.model.Competition;
import com.nosoftskills.predcomposer.model.Game;
import com.nosoftskills.predcomposer.model.Prediction;
import com.nosoftskills.predcomposer.model.User;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.stream.Stream;

import static com.nosoftskills.predcomposer.user.PasswordHashUtil.hashPassword;

/**
 * @author Ivan St. Ivanov
 */
public final class TestData {

    public static User user1;
    public static User user2;
    public static Game game1;
    public static Game game2;
    public static Game game3;
    public static Game game4;
    public static Game game5;
    public static Game game6;
    public static Competition competition;
    public static Prediction prediction1;
    public static Prediction prediction2;
    public static Prediction prediction3;

    static {
        initialize();
    }

    public static void initialize() {
        user1 = new User("ivan", hashPassword("ivan"), "ivan@example.com", "Ivan", "Ivanov", true);
        user2 = new User("koko", hashPassword("koko"), "koko@example.com", "Koko", "Stefanov", false);

        game1 = new Game("Manchester United", "Club Brugge", "3:1", LocalDateTime
                .of(2015, 8, 18, 21, 45), true);
        game2 = new Game("Club Brugge", "Manchester United", "0:4", LocalDateTime.of(2015, 8,
                26, 21, 45), true);
        game3 = new Game("Paris SG", "Malmo", LocalDateTime.of(2015, 9, 15, 21, 45));
        game4 = new Game("Real Madrid", "Shakhtar Donetsk", LocalDateTime.of(2015, 9, 15, 21, 45));
        game5 = new Game("Leverkusen", "BATE", LocalDateTime.of(2015, 9, 16, 21, 45));
        game6 = new Game("Roma", "Barcelona", LocalDateTime.of(2015, 9, 16, 21, 45));

        prediction1 = new Prediction(user1, game2, "0:0");
        prediction2 = new Prediction(user2, game2, "0:2");
        prediction3 = new Prediction(user1, game3, "2:0");

        user1.getPredictions().addAll(Arrays.asList(prediction1, prediction3));
        user2.getPredictions().add(prediction2);

        game2.getPredictions().addAll(Arrays.asList(prediction1, prediction2));
        game3.getPredictions().add(prediction3);

        competition = new Competition("Champions League 2015-2016");
        competition.getGames().addAll(Arrays.asList(game1, game2, game3, game4, game5, game6));
    }

    private static long currentId;

    public static void generateEntityIds() {
        currentId = 1;
        Stream.of(TestData.class.getFields()).forEach(TestData::setId);
    }

    private static void setId(Field field) {
        try {
            Object entityInstance = field.get(null);
            Field idField = entityInstance.getClass().getDeclaredField("id");
            idField.setAccessible(true);
            idField.set(entityInstance, currentId++);
        } catch (IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}

@@ -971,31 +971,24 @@
 %0A    %7D%0A%0A    
-public 
 static void 
@@ -1063,23 +1063,28 @@
 , %22ivan@
-example
+nosoftskills
 .com%22, %22
@@ -1172,15 +1172,20 @@
 oko@
-example
+nosoftskills
 .com
@@ -1246,45 +1246,26 @@
 me(%22
-Manchester United%22, %22Club Brugge
+Porto%22, %22Roma
 %22, %22
-3
+1
 :1%22,
@@ -1306,16 +1306,16 @@
 (201
-5
+6
 , 8, 1
-8
+7
 , 21
@@ -1358,47 +1358,28 @@
 me(%22
-Club Brugge%22, %22Manchester United
+Roma%22, %22Porto
 %22, %220:
-4
+3
 %22, L
@@ -1397,17 +1397,17 @@
 e.of(201
-5
+6
 , 8,%0A   
@@ -1420,17 +1420,17 @@
        2
-6
+3
 , 21, 45
@@ -1476,18 +1476,31 @@
 is S
-G%22, %22Malmo
+aint-Germain%22, %22Arsenal
 %22, L
@@ -1514,32 +1514,32 @@
 eTime.of(201
-5
+6
 , 9, 1
-5
+3
 , 21, 45));%0A
@@ -1568,39 +1568,27 @@
 me(%22
-Real Madrid%22, %22Shakhtar Donetsk
+Barcelona%22, %22Celtic
 %22, L
@@ -1610,16 +1610,16 @@
 (201
-5
+6
 , 9, 1
-5
+3
 , 21
@@ -1656,26 +1656,31 @@
 me(%22
-Leverkusen%22, %22BATE
+Real Madrid%22, %22Sporting
 %22, L
@@ -1694,32 +1694,32 @@
 eTime.of(201
-5
+6
 , 9, 1
-6
+4
 , 21, 45));%0A
@@ -1748,24 +1748,26 @@
 me(%22
-Roma%22, %22Barcelon
+Juventus%22, %22Sevill
 a%22, 
@@ -1793,17 +1793,17 @@
 15, 9, 1
-6
+4
 , 21, 45
@@ -2308,14 +2308,14 @@
  201
-5
+6
 -201
-6
+7
 %22);%0A

Update test data with 2016/2017 matches