package banane.io.pdb.web;

import banane.io.pdb.model.Player;
import banane.io.pdb.model.User;
import banane.io.pdb.repository.MapPointRepository;
import banane.io.pdb.repository.PlayerRepository;
import banane.io.pdb.validator.PlayerValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/player")
public class PlayerController {
    @Autowired
    private PlayerValidator playerValidator;

    @Autowired
    private PlayerRepository playerRepository;

    @Autowired
    private MapPointRepository mapPointRepository;


    private static String VIEW_FOLDER = "player/";

    @GetMapping
    public String show(Model model) {
        final User loggedInUser = new User();//securityService.findLoggedInUser();
        model.addAttribute("player", loggedInUser.getPlayer());

        return VIEW_FOLDER + "show";
    }

    @GetMapping("/creation")
    public String creation(Model model) {
        model.addAttribute("player", new Player());
        return VIEW_FOLDER + "creation";
    }

    @PostMapping("/creation")
    public Player creation(@ModelAttribute("player") Player player, BindingResult bindingResult, Model model) {
        playerValidator.validate(player, bindingResult);

        if (bindingResult.hasErrors()) {
            model.addAttribute("formErrors", bindingResult.getAllErrors());
            model.addAttribute("fieldErrors", getFieldErrors(bindingResult));
            //TODO : return the error to the view return VIEW_FOLDER + "creation";
        }
        User user = new User();//TODO use a correct service for user securityService.findLoggedInUser();
        player.setOwner(user);
        player.setCurrentZone(mapPointRepository.getOne(1L));
        playerRepository.save(player);

        return player;
    }//TODO: Add check for duplicate since player is unique

    private Map<String, ObjectError> getFieldErrors(BindingResult result) {
        Map<String, ObjectError> map = new HashMap<>();
        for (FieldError error : result.getFieldErrors()) {
            map.put(error.getField(), error);
        }
        return map;
    }
}

@@ -32,219 +32,77 @@
 ort 
-banane.io.pdb.model.Player;%0Aimport banane.io.pdb.model.User;%0Aimport banane.io.pdb.repository.MapPointRepository;%0Aimport banane.io.pdb.repository.PlayerRepository;%0Aimport banane.io.pdb.validator.PlayerValidator
+java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Optional
 ;%0A
+%0A
 impo
@@ -671,47 +671,264 @@
 ort 
-java.util.HashMap;%0Aimport java.util.Map
+banane.io.pdb.model.Player;%0Aimport banane.io.pdb.model.User;%0Aimport banane.io.pdb.repository.MapPointRepository;%0Aimport banane.io.pdb.repository.PlayerRepository;%0Aimport banane.io.pdb.security.SecurityService;%0Aimport banane.io.pdb.validator.PlayerValidator
 ;%0A%0A@
@@ -1196,16 +1196,77 @@
 itory;%0A%0A
+    @Autowired%0A    private SecurityService securityService;%0A%0A
 %0A    pri
@@ -1341,31 +1341,33 @@
 lic 
-String show(Model model
+Player currentUserPlayer(
 ) %7B%0A
@@ -1400,29 +1400,16 @@
 nUser = 
-new User();//
 security
@@ -1448,100 +1448,39 @@
     
-model.addAttribute(%22player%22, loggedInUser.getPlayer());%0A%0A        return VIEW_FOLDER + %22show%22
+return loggedInUser.getPlayer()
 ;%0A  

Fix imports and change GETting the player