package de.fred4jupiter.fredbet.data;

import java.time.LocalDateTime;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import de.fred4jupiter.fredbet.FredBetRole;
import de.fred4jupiter.fredbet.domain.AppUser;
import de.fred4jupiter.fredbet.domain.Group;
import de.fred4jupiter.fredbet.domain.Match;
import de.fred4jupiter.fredbet.domain.MatchBuilder;
import de.fred4jupiter.fredbet.service.BettingService;
import de.fred4jupiter.fredbet.service.MatchService;
import de.fred4jupiter.fredbet.service.UserService;

@Component
public class DataBasePopulator {

    private static final Logger LOG = LoggerFactory.getLogger(DataBasePopulator.class);

    @Autowired
    private MatchService matchService;

    @Autowired
    private Environment environment;

    @Autowired
    private UserService userService;

    @Autowired
    private BettingService bettingService;

    @PostConstruct
    public void initDatabaseWithDemoData() {
        LOG.info("initDatabaseWithDemoData: inserting demo data...");

        // will also be used for remote shell login
        final AppUser adminUser = new AppUser("admin", "admin", FredBetRole.ROLE_USER, FredBetRole.ROLE_ADMIN, FredBetRole.ROLE_EDIT_MATCH);
        userService.save(adminUser);
        final AppUser editUser = new AppUser("edit", "edit", FredBetRole.ROLE_USER, FredBetRole.ROLE_EDIT_MATCH);
        userService.save(editUser);
        
        final AppUser normalUser = new AppUser("normal", "normal", FredBetRole.ROLE_USER);
        userService.save(normalUser);

        // this we be executed in demodata profile only
        if (environment.acceptsProfiles("demodata")) {

            // for (int i = 0; i < 10; i++) {
            // userService.save(new AppUser("test" + i, "test" + i,
            // FredBetRole.ROLE_USER));
            // }

            Match match1 = MatchBuilder.create().withTeams("Bulgarien", "Irland").withGroup(Group.GROUP_A)
                    .withStadium("Westfalenstadium, Dortmund").withKickOffDate(LocalDateTime.now().plusMinutes(5)).build();
            matchService.save(match1);

            Match match2 = MatchBuilder.create().withTeams("Deutschland", "Frankfreich").withGroup(Group.GROUP_B)
                    .withStadium("Weserstadium, bremen").withKickOffDate(LocalDateTime.now().plusMinutes(10)).build();
            matchService.save(match2);

            Match match3 = MatchBuilder.create().withTeams("Belgien", "England").withGroup(Group.GROUP_D).withStadium("AOL Arena, München")
                    .withKickOffDate(LocalDateTime.now().plusMinutes(15)).build();
            matchService.save(match3);

            bettingService.createAndSaveBetting(adminUser, match1, 2, 1);
            bettingService.createAndSaveBetting(adminUser, match2, 3, 1);
            bettingService.createAndSaveBetting(adminUser, match3, 2, 4);

            bettingService.createAndSaveBetting(editUser, match1, 0, 1);
            bettingService.createAndSaveBetting(editUser, match2, 1, 2);
            bettingService.createAndSaveBetting(editUser, match3, 5, 0);
        }
    }

}

@@ -561,63 +561,8 @@
 er;%0A
-import de.fred4jupiter.fredbet.service.BettingService;%0A
 impo
@@ -962,67 +962,8 @@
 e;%0A%0A
-    @Autowired%0A    private BettingService bettingService;%0A%0A
     
@@ -1105,802 +1105,141 @@
     
-// will also be used for remote shell login%0A        final AppUser adminUser = new AppUser(%22admin%22, %22admin%22, FredBetRole.ROLE_USER, FredBetRole.ROLE_ADMIN, FredBetRole.ROLE_EDIT_MATCH);%0A        userService.save(adminUser);%0A        final AppUser editUser = new AppUser(%22edit%22, %22edit%22, FredBetRole.ROLE_USER, FredBetRole.ROLE_EDIT_MATCH);%0A        userService.save(editUser);%0A        %0A        final AppUser normalUser = new AppUser(%22normal%22, %22normal%22, FredBetRole.ROLE_USER);%0A        userService.save(normalUser);%0A%0A        // this we be executed in demodata profile only%0A        if (environment.acceptsProfiles(%22demodata%22)) %7B%0A%0A            // for (int i = 0; i %3C 10; i++) %7B%0A            // userService.save(new AppUser(%22test%22 + i, %22test%22 + i,%0A            // FredBetRole.ROLE_USER));%0A            // %7D%0A
+createDefaultUsers();%0A%0A        // this we be executed in demodata profile only%0A        if (environment.acceptsProfiles(%22demodata%22)) %7B
 %0A   
@@ -2045,460 +2045,447 @@
 3);%0A
-%0A
+  
       
+%7D%0A
     
-  bettingService.createAndSaveBetting(adminUser, match1, 2, 1);%0A            bettingService.createAndSaveBetting(adminUser, match2, 3, 1);%0A            bettingService.createAndSaveBetting(adminUser, match3, 2, 4);%0A%0A            bettingService.createAndSaveBetting(editUser, match1, 0, 1);%0A            bettingService.createAndSaveBetting(editUser, match2, 1, 2);%0A            bettingService.createAndSaveBetting(editUser, match3, 5, 0);%0A        %7D
+%7D%0A%0A    private void createDefaultUsers() %7B%0A        // will also be used for remote shell login%0A        userService.save(new AppUser(%22admin%22, %22admin%22, FredBetRole.ROLE_USER, FredBetRole.ROLE_ADMIN, FredBetRole.ROLE_EDIT_MATCH));%0A        %0A        userService.save(new AppUser(%22edit%22, %22edit%22, FredBetRole.ROLE_USER, FredBetRole.ROLE_EDIT_MATCH));%0A        userService.save(new AppUser(%22normal%22, %22normal%22, FredBetRole.ROLE_USER));
 %0A   

update default demo data