package com.ryanpmartz.booktrackr.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ryanpmartz.booktrackr.controller.dto.SignupDto;
import com.ryanpmartz.booktrackr.domain.User;
import com.ryanpmartz.booktrackr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RestController
public class UserController {

    private static final String EMAIL_EXISTS_MESSAGE = "This email is in use";

    private final UserService userService;


    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public ResponseEntity<?> createUser(@Valid @RequestBody SignupDto signupDto) throws JsonProcessingException {
        if (!signupDto.getPassword().equals(signupDto.getConfirmPassword())) {
            Map<String, String> errorJson = new HashMap<>();
            errorJson.put("field", "confirmPassword");
            errorJson.put("error", "Password and confirm password must match");

            ObjectMapper mapper = new ObjectMapper();

            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .contentType(MediaType.APPLICATION_JSON_UTF8).body(mapper.writeValueAsString(errorJson));
        }

        final String requestedEmail = signupDto.getEmail();

        Optional<User> existingUserOptional = userService.getUserByEmail(requestedEmail);
        if (existingUserOptional.isPresent()) {
            return ResponseEntity.badRequest().body(EMAIL_EXISTS_MESSAGE);
        }

        User user = new User();
        user.setEnabled(true);
        // never save plain text passwords, we'll address this in the next post
        user.setPassword("*****");
        user.setEmail(signupDto.getEmail());
        user.setFirstName(signupDto.getFirstName());
        user.setLastName(signupDto.getLastName());

        User savedUser = userService.createUser(user);

        return ResponseEntity.status(HttpStatus.CREATED)
                .contentType(MediaType.APPLICATION_JSON_UTF8).body(savedUser);
    }
}

@@ -508,16 +508,85 @@
 Entity;%0A
+import org.springframework.security.crypto.password.PasswordEncoder;%0A
 import o
@@ -1103,16 +1103,66 @@
 ervice;%0A
+    private final PasswordEncoder passwordEncoder;
 %0A%0A    @A
@@ -1219,16 +1219,41 @@
 rService
+, PasswordEncoder encoder
 ) %7B%0A    
@@ -1284,24 +1284,64 @@
 serService;%0A
+        this.passwordEncoder = encoder;%0A
     %7D%0A%0A    @
@@ -2385,123 +2385,8 @@
 e);%0A
-        // never save plain text passwords, we'll address this in the next post%0A        user.setPassword(%22*****%22);%0A
     
@@ -2529,16 +2529,173 @@
 Name());
+%0A        user.setPassword(passwordEncoder.encode(signupDto.getPassword()));%0A%0A        signupDto.setPassword(null);%0A        signupDto.setConfirmPassword(null);
 %0A%0A      

encode password