package nl.vpro.poel.configuration;

import nl.vpro.poel.domain.*;
import nl.vpro.poel.repository.UserGroupRepository;
import nl.vpro.poel.repository.MatchRepository;
import nl.vpro.poel.repository.MessageRepository;
import nl.vpro.poel.repository.UserRepository;
import nl.vpro.poel.service.UserGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Component
@Profile("development")
public class DummyDataLoader {

    private final UserGroupRepository userGroupRepository;
    private final UserGroupService userGroupService;

    private final MatchRepository matchRepository;

    private final MessageRepository messageRepository;

    private final UserRepository userRepository;

    @Autowired
    DummyDataLoader(UserGroupRepository userGroupRepository, UserGroupService userGroupService, MatchRepository matchRepository, MessageRepository messageRepository, UserRepository userRepository) {

        this.userGroupRepository = userGroupRepository;
        this.userGroupService = userGroupService;
        this.matchRepository = matchRepository;
        this.messageRepository = messageRepository;
        this.userRepository = userRepository;

        groups();
        adminUsers();
        matches();
        messages();
    }

    private void adminUsers() {

        UserGroup userGroupDigitaal = userGroupService.findByName("Digitaal").orElse(null);

        List<User> admins = Arrays.asList(
                new User("n.breunese@vpro.nl", Role.ADMIN, "Nils Breunese", "Van Breunhorst", userGroupDigitaal),
                new User("f.bosma@vpro.nl", Role.ADMIN, "Frank Bosma", "Bosmatic", userGroupDigitaal),
                new User("t.klok@vpro.nl", Role.ADMIN, "Timo Klok", " Ibratimovich", userGroupDigitaal),
                new User("d.pronk@vpro.nl", Role.ADMIN, "David Pronk", "van Pronkhorst", userGroupDigitaal),
                new User("f.hermsen@vpro.nl", Role.ADMIN, "Fred Hermsen", "The Herminator", userGroupDigitaal),
                new User("l.de.knijff@gmail.com", Role.ADMIN, "Laurens de Knijff", "Databeest", userGroupDigitaal),
                new User("l.schipperheyn@vpro.nl", Role.ADMIN, "Luuk Schipperheyn", "Luuk de Jonguh", userGroupDigitaal)
        );
        userRepository.save(admins);
    }

    private void matches() {
        Date now = new Date();
        Date lastWeek = Date.from(now.toInstant().minus(7, ChronoUnit.DAYS));
        Date nextWeek = Date.from(now.toInstant().plus(7, ChronoUnit.DAYS));
        Date almost = Date.from(now.toInstant().plus(2, ChronoUnit.HOURS));

        List<Match> defaultMatches = Arrays.asList(

                // Finished
                new Match("Zwitserland", "Noord-Ierland", lastWeek, new MatchResult(3, 1)),
                new Match("België", "Engeland", lastWeek, new MatchResult(5, 4)),

                // Unfinished
                new Match("Frankrijk", "Duitsland", now),
                new Match("Spanje", "Engeland", now),

                // Future
                new Match("Portugal", "België", almost),
                new Match("Engeland", "Oostenrijk", nextWeek)
        );
        matchRepository.save(defaultMatches);
    }

    private void messages() {

        List<Message> defaultMessages = Arrays.asList(

                new Message("/predictions", "Hier een melding voor invullers"),
                new Message("/user", "Hier een melding voor je profiel"),
                new Message("/ranking", "Hier een melding voor de ranking")
        );
        messageRepository.save(defaultMessages);
    }

    private void groups() {

        List<UserGroup> defaultUserGroups = Arrays.asList(
                new UserGroup("Digitaal"),
                new UserGroup("Voetbalvrouwen")
        );
        userGroupRepository.save(defaultUserGroups);
    }
}

@@ -95,172 +95,9 @@
 ory.
-UserGroupRepository;%0Aimport nl.vpro.poel.repository.MatchRepository;%0Aimport nl.vpro.poel.repository.MessageRepository;%0Aimport nl.vpro.poel.repository.UserRepository
+*
 ;%0Aim
@@ -597,17 +597,16 @@
 ervice;%0A
-%0A
     priv
@@ -640,33 +640,93 @@
 atchRepository;%0A
+    private final PredictionRepository predictionRepository;
 %0A
-
     private fina
@@ -760,25 +760,24 @@
 Repository;%0A
-%0A
     private 
@@ -849,16 +849,29 @@
 aLoader(
+%0A            
 UserGrou
@@ -898,24 +898,36 @@
 pRepository,
+%0A           
  UserGroupSe
@@ -949,16 +949,28 @@
 Service,
+%0A           
  MatchRe
@@ -994,16 +994,83 @@
 ository,
+%0A            PredictionRepository predictionRepository,%0A           
  Message
@@ -1098,16 +1098,28 @@
 ository,
+%0A           
  UserRep
@@ -1140,21 +1140,25 @@
 pository
+%0A    
 ) %7B%0A
-%0A
         
@@ -1295,32 +1295,90 @@
 atchRepository;%0A
+        this.predictionRepository = predictionRepository;%0A
         this.mes
@@ -1515,24 +1515,47 @@
  matches();%0A
+        predictions();%0A
         mess
@@ -3412,32 +3412,32 @@
 eek)%0A        );%0A
-
         matchRep
@@ -3465,32 +3465,667 @@
 atches);%0A    %7D%0A%0A
+    private void predictions() %7B%0A        User user1 = userRepository.findOne(1L);%0A        predictionRepository.save(Arrays.asList(%0A                new Prediction(user1, matchRepository.findOne(1L), new MatchResult(1, 0)),%0A                new Prediction(user1, matchRepository.findOne(2L), new MatchResult(2, 2), true)%0A        ));%0A%0A        User user2 = userRepository.findOne(2L);%0A        predictionRepository.save(Arrays.asList(%0A                new Prediction(user2, matchRepository.findOne(2L), new MatchResult(2, 1), true),%0A                new Prediction(user2, matchRepository.findOne(3L), new MatchResult(0, 2))%0A        ));%0A    %7D%0A%0A
     private void

Use the dummy predictions, Luke!