// Path: src/main/java/se/omegapoint/facepalm/client/adapters/OperationResult.java
// public class OperationResult {
//     private final String message;
//     private final boolean success;
// 
//     private OperationResult(final boolean success, final String message) {
//         this.message = message;
//         this.success = success;
//     }
// 
//     public static OperationResult success() {
//         return new OperationResult(true, null);
//     }
// 
//     public static OperationResult failure(final String message) {
//         return new OperationResult(false, message);
//     }
// 
//     public String message() {
//         if (success) {
//             throw new IllegalStateException("No message available for successful operations");
//         }
//         return message;
//     }
// 
//     public boolean isSuccess() {
//         return success;
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/client/adapters/UserAdapter.java
// @Adapter
// public class UserAdapter {
// 
//     private final UserService userService;
// 
//     @Autowired
//     public UserAdapter(final UserService userService) {
//         this.userService = notNull(userService);
//     }
// 
//     public OperationResult registerNewUser(final RegisterCredentials credentials) {
//         notNull(credentials);
// 
//         final Result<UserSuccess, UserFailure> result = userService.registerUser(credentials.getUsername(), credentials.getEmail(), credentials.getFirstname(),
//                 credentials.getLastname(), credentials.getPassword());
//         return result.isSuccess() ? success() : failure("User already exists"); // TODO [dh] Proper error mapping!
//     }
// 
//     public Optional<Profile> profileFor(final String username) {
//         final Result<User, UserFailure> result = userService.getUserWith(username);
//         return result.isSuccess() ? Optional.of(new Profile(result.success())) : Optional.empty();
//     }
// 
//     public Profile profileForCurrentUser() {
//         final AuthenticatedUser authenticatedUser = (AuthenticatedUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//         final Result<User, UserFailure> result = userService.getUserWith(authenticatedUser.userName);
//         return new Profile(result.success());
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/client/models/RegisterCredentials.java
// public class RegisterCredentials {
// 
//     @Length(min = 3, max = 20, message = "Username must be between 3 and 20 characters")
//     private String username;
// 
//     @Length(min = 2, max = 40, message = "Firstname must be between 2 and 40 characters")
//     private String firstname;
// 
//     @Length(min = 2, max = 40, message = "Firstname must be between 2 and 40 characters")
//     private String lastname;
// 
//     @NotBlank
//     @Email(message = "Invalid email format")
//     private String email;
// 
//     @NotBlank
//     private String password;
// 
//     public String getUsername() {
//         return username;
//     }
// 
//     public void setUsername(String username) {
//         this.username = username;
//     }
// 
//     public String getFirstname() {
//         return firstname;
//     }
// 
//     public void setFirstname(final String firstname) {
//         this.firstname = firstname;
//     }
// 
//     public String getLastname() {
//         return lastname;
//     }
// 
//     public void setLastname(final String lastname) {
//         this.lastname = lastname;
//     }
// 
//     public String getEmail() {
//         return email;
//     }
// 
//     public void setEmail(String email) {
//         this.email = email;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     public void resetPasswords() {
//         password = null;
//     }
// }


import static org.apache.commons.lang3.Validate.notNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import se.omegapoint.facepalm.client.adapters.OperationResult;
import se.omegapoint.facepalm.client.adapters.UserAdapter;
import se.omegapoint.facepalm.client.models.RegisterCredentials;
import javax.validation.Valid;
/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package se.omegapoint.facepalm.client.controllers;




@Controller
public class UserController {

    private final UserAdapter userAdapter;

    @Autowired
    public UserController(final UserAdapter userAdapter) {
        this.userAdapter = notNull(userAdapter);
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(final Model model) {
        if (userIsLoggedIn()) {
            return "redirect:/";
        }

        model.addAttribute("registerCredentials", new RegisterCredentials());
        return "login";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(final @Valid RegisterCredentials credentials, final BindingResult bindingResult, final Model model) {
        if (bindingResult.hasErrors()) {
            credentials.resetPasswords();
            return "login";
        }

// Path: src/main/java/se/omegapoint/facepalm/client/adapters/OperationResult.java
// public class OperationResult {
//     private final String message;
//     private final boolean success;
// 
//     private OperationResult(final boolean success, final String message) {
//         this.message = message;
//         this.success = success;
//     }
// 
//     public static OperationResult success() {
//         return new OperationResult(true, null);
//     }
// 
//     public static OperationResult failure(final String message) {
//         return new OperationResult(false, message);
//     }
// 
//     public String message() {
//         if (success) {
//             throw new IllegalStateException("No message available for successful operations");
//         }
//         return message;
//     }
// 
//     public boolean isSuccess() {
//         return success;
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/client/adapters/UserAdapter.java
// @Adapter
// public class UserAdapter {
// 
//     private final UserService userService;
// 
//     @Autowired
//     public UserAdapter(final UserService userService) {
//         this.userService = notNull(userService);
//     }
// 
//     public OperationResult registerNewUser(final RegisterCredentials credentials) {
//         notNull(credentials);
// 
//         final Result<UserSuccess, UserFailure> result = userService.registerUser(credentials.getUsername(), credentials.getEmail(), credentials.getFirstname(),
//                 credentials.getLastname(), credentials.getPassword());
//         return result.isSuccess() ? success() : failure("User already exists"); // TODO [dh] Proper error mapping!
//     }
// 
//     public Optional<Profile> profileFor(final String username) {
//         final Result<User, UserFailure> result = userService.getUserWith(username);
//         return result.isSuccess() ? Optional.of(new Profile(result.success())) : Optional.empty();
//     }
// 
//     public Profile profileForCurrentUser() {
//         final AuthenticatedUser authenticatedUser = (AuthenticatedUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//         final Result<User, UserFailure> result = userService.getUserWith(authenticatedUser.userName);
//         return new Profile(result.success());
//     }
// }
// 
// Path: src/main/java/se/omegapoint/facepalm/client/models/RegisterCredentials.java
// public class RegisterCredentials {
// 
//     @Length(min = 3, max = 20, message = "Username must be between 3 and 20 characters")
//     private String username;
// 
//     @Length(min = 2, max = 40, message = "Firstname must be between 2 and 40 characters")
//     private String firstname;
// 
//     @Length(min = 2, max = 40, message = "Firstname must be between 2 and 40 characters")
//     private String lastname;
// 
//     @NotBlank
//     @Email(message = "Invalid email format")
//     private String email;
// 
//     @NotBlank
//     private String password;
// 
//     public String getUsername() {
//         return username;
//     }
// 
//     public void setUsername(String username) {
//         this.username = username;
//     }
// 
//     public String getFirstname() {
//         return firstname;
//     }
// 
//     public void setFirstname(final String firstname) {
//         this.firstname = firstname;
//     }
// 
//     public String getLastname() {
//         return lastname;
//     }
// 
//     public void setLastname(final String lastname) {
//         this.lastname = lastname;
//     }
// 
//     public String getEmail() {
//         return email;
//     }
// 
//     public void setEmail(String email) {
//         this.email = email;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     public void resetPasswords() {
//         password = null;
//     }
// }


// Path: src/main/java/se/omegapoint/facepalm/client/controllers/UserController.java
import static org.apache.commons.lang3.Validate.notNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import se.omegapoint.facepalm.client.adapters.OperationResult;
import se.omegapoint.facepalm.client.adapters.UserAdapter;
import se.omegapoint.facepalm.client.models.RegisterCredentials;
import javax.validation.Valid;

/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package se.omegapoint.facepalm.client.controllers;




@Controller
public class UserController {

    private final UserAdapter userAdapter;

    @Autowired
    public UserController(final UserAdapter userAdapter) {
        this.userAdapter = notNull(userAdapter);
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(final Model model) {
        if (userIsLoggedIn()) {
            return "redirect:/";
        }

        model.addAttribute("registerCredentials", new RegisterCredentials());
        return "login";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(final @Valid RegisterCredentials credentials, final BindingResult bindingResult, final Model model) {
        if (bindingResult.hasErrors()) {
            credentials.resetPasswords();
            return "login";
        }

        final OperationResult result = userAdapter.registerNewUser(credentials);