package pingis.controllers;

import java.security.Principal;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pingis.controllers.UserController.LiveType;
import pingis.entities.Challenge;
import pingis.entities.TaskInstance;
import pingis.entities.TmcUserDto;
import pingis.entities.User;
import pingis.services.ChallengeService;
import pingis.services.GameplayService;
import pingis.services.UserService;

@Controller
public class UserController {

  public enum LiveType {
    CONTINUE, CREATE, JOIN
  }

  @Autowired
  UserService userService;

  @Autowired
  ChallengeService challengeService;

  @Autowired
  GameplayService gameplayService;

  Logger logger = Logger.getLogger(UserController.class);


  @RequestMapping(value = "/login", method = RequestMethod.GET)
  public String login(Model model) {
    return "redirect:/oauth2/authorization/code/tmc";
  }

  @RequestMapping(value = "/user", method = RequestMethod.GET)
  public String user(Model model, Principal principal) {
    User user = userService.handleUserAuthenticationByName(principal.getName());
    LiveType liveType = null;

    MultiValueMap<Challenge, TaskInstance> myTasksInChallenges = new LinkedMultiValueMap<>();

    user.getTaskInstances().stream()
        .filter(e -> !e.getChallenge().getIsOpen())
        .forEach(e -> myTasksInChallenges.add(e.getChallenge(), e));

    List<Challenge> availableChallenges = challengeService.findAll().stream()
        .filter(e -> !e.getIsOpen())
        .filter(e -> e.getLevel() <= user.getLevel())
        .filter(e -> !myTasksInChallenges.containsKey(e))
        .collect(Collectors.toList());

    Challenge liveChallenge = gameplayService.getParticipatingLiveChallenge();
    Challenge randomLiveChallenge = challengeService.getRandomLiveChallenge(user);

    if (liveChallenge == null && randomLiveChallenge == null) {
      liveType = LiveType.CREATE;
      model.addAttribute("liveChallengeType", liveType);
    } else if (liveChallenge == null) {
      liveType = LiveType.JOIN;
      liveChallenge = randomLiveChallenge;
      model.addAttribute("liveChallengeType", liveType);
    } else {
      liveType = LiveType.CONTINUE;
      model.addAttribute("liveChallengeType", liveType);
    }

    model.addAttribute("availableChallenges", availableChallenges);
    model.addAttribute("myTasksInChallenges", myTasksInChallenges);
    model.addAttribute("liveChallenge", liveChallenge);
    model.addAttribute("user", user);

    return "user";
  }

  @RequestMapping(value = "/admin", method = RequestMethod.GET)
  public String admin(Model model) {
    return "admin";
  }
}

@@ -1566,38 +1566,8 @@
 ());
-%0A    LiveType liveType = null;
 %0A%0A  
@@ -2244,24 +2244,54 @@
 nge(user);%0A%0A
+    LiveType liveType = null;%0A
     if (live
@@ -2346,42 +2346,8 @@
 ) %7B%0A
-      liveType = LiveType.CREATE;%0A
     
@@ -2384,32 +2384,39 @@
 lengeType%22, 
-l
+L
 iveType
+.CREATE
 );%0A    %7D els
@@ -2450,83 +2450,8 @@
 ) %7B%0A
-      liveType = LiveType.JOIN;%0A      liveChallenge = randomLiveChallenge;%0A
     
@@ -2488,32 +2488,37 @@
 lengeType%22, 
-l
+L
 iveType
+.JOIN
 );%0A    %7D els
@@ -2525,44 +2525,8 @@
 e %7B%0A
-      liveType = LiveType.CONTINUE;%0A
     
@@ -2567,24 +2567,33 @@
 eType%22, 
-l
+L
 iveType
+.CONTINUE
 );%0A    %7D

fix LiveType usage in UserController