/*
 * Copyright 2014 Open mHealth
 *
 * 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 org.openmhealth.dsu.controller;


import org.openmhealth.dsu.domain.EndUserRegistrationData;
import org.openmhealth.dsu.service.EndUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.springframework.http.HttpStatus.*;
import static org.springframework.web.bind.annotation.RequestMethod.POST;


/**
 * A controller that manages user accounts.
 *
 * @author Emerson Farrugia
 */
@Controller
public class EndUserController {

    @Autowired
    private Validator validator;

    @Autowired
    private EndUserService endUserService;


    /**
     * Registers a new user.
     *
     * @param registrationData the registration data of the user
     * @return a response entity with status OK if the user is registered, BAD_REQUEST if the request is invalid,
     * or CONFLICT if the user exists
     */
    @RequestMapping(value = "/users", method = POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> registerUser(@RequestBody EndUserRegistrationData registrationData) {

        if (registrationData == null) {
            return new ResponseEntity<>(BAD_REQUEST);
        }

        Set<ConstraintViolation<EndUserRegistrationData>> constraintViolations = validator.validate(registrationData);

        if (!constraintViolations.isEmpty()) {
            return new ResponseEntity<>(asErrorMessageList(constraintViolations), BAD_REQUEST);
        }

        if (endUserService.doesUserExist(registrationData.getUsername())) {
            return new ResponseEntity<>(CONFLICT);
        }

        endUserService.registerUser(registrationData);

        return new ResponseEntity<>(CREATED);
    }

    protected List<String> asErrorMessageList(Set<ConstraintViolation<EndUserRegistrationData>> constraintViolations) {

        return constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList());
    }
}

@@ -632,16 +632,59 @@
 ller;%0A%0A%0A
+import org.openmhealth.dsu.domain.EndUser;%0A
 import o
@@ -848,16 +848,60 @@
 owired;%0A
+import org.springframework.http.HttpStatus;%0A
 import o
@@ -983,16 +983,69 @@
 Entity;%0A
+import org.springframework.social.google.api.Google;%0A
 import o
@@ -1209,16 +1209,239 @@
 apping;%0A
+import org.springframework.web.bind.annotation.RequestMethod;%0Aimport org.springframework.web.bind.annotation.ResponseBody;%0Aimport org.springframework.web.util.CookieGenerator;%0A%0Aimport javax.servlet.http.HttpServletResponse;
 %0Aimport 
@@ -1513,16 +1513,77 @@
 idator;%0A
+import java.security.SecureRandom;%0Aimport java.util.HashMap;%0A
 import j
@@ -1597,16 +1597,38 @@
 l.List;%0A
+import java.util.Map;%0A
 import j
@@ -1731,16 +1731,89 @@
 atus.*;%0A
+import static org.springframework.web.bind.annotation.RequestMethod.GET;%0A
 import s
@@ -2011,387 +2011,8 @@
 er %7B
-%0A%0A    @Autowired%0A    private Validator validator;%0A%0A    @Autowired%0A    private EndUserService endUserService;%0A%0A%0A    /**%0A     * Registers a new user.%0A     *%0A     * @param registrationData the registration data of the user%0A     * @return a response entity with status OK if the user is registered, BAD_REQUEST if the request is invalid,%0A     * or CONFLICT if the user exists%0A     */
 %0A   
@@ -2037,18 +2037,11 @@
 alue
- = %22/users
+=%22/
 %22, m
@@ -2049,436 +2049,74 @@
 thod
- 
 = 
-POST, produces = MediaType.APPLICATION_JSON_VALUE)%0A    public ResponseEntity%3C?%3E registerUser(@RequestBody EndUserRegistrationData registrationData) %7B%0A%0A        if (registrationData == null) %7B%0A            return new ResponseEntity%3C%3E(BAD_REQUEST);%0A        %7D%0A%0A        Set%3CCons
+RequestMethod.GET)%0A    @ResponseBody%0A    public S
 tr
-a
 in
-tViolation%3CEndUserRegistrationData%3E%3E constraintViolations = validator.validate(registrationData);%0A%0A        if (!constraintViolations.isEmpty()
+g home(
 ) %7B%0A
-  
     
@@ -2132,574 +2132,29 @@
 urn 
-new ResponseEntity%3C%3E(asErrorMessageList(constraintViolations), BAD_REQUEST);%0A        %7D%0A%0A        if (endUserService.doesUserExist(registrationData.getUsername())) %7B%0A            return new ResponseEntity%3C%3E(CONFLICT);%0A        %7D%0A%0A        endUserService.registerUser(registrationData);%0A%0A        return new ResponseEntity%3C%3E(CREATED);%0A    %7D%0A%0A    protected List%3CString%3E asErrorMessageList(Set%3CConstraintViolation%3CEndUserRegistrationData%3E%3E constraintViolations) %7B%0A%0A        return constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList())
+%22You have signed in.%22
 ;%0A  

remove the code that allow manually sign up new users