package org.podcastpedia.web.user;

import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
import org.apache.log4j.Logger;
import org.podcastpedia.common.domain.User;
import org.podcastpedia.core.searching.SearchData;
import org.podcastpedia.core.user.EmailNotificationService;
import org.podcastpedia.core.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.ServletRequest;
import java.util.UUID;


@Controller
@RequestMapping("users/registration")
public class RegistrationController {

	protected static Logger LOG = Logger.getLogger(RegistrationController.class);

	@Autowired
    UserService userService;

    @Autowired
    private UserRegistrationFormValidator userRegistrationFormValidator;

    @Autowired
    private EmailNotificationService emailNotificationService;

    @Autowired
    private ReCaptchaImpl reCaptcha;

	/**
	 * Add an empty searchData object to the model
	 */
	@ModelAttribute
	public void addDataToModel(ModelMap model){
		SearchData dataForSearchBar = new SearchData();
		dataForSearchBar.setSearchMode("natural");
		dataForSearchBar.setCurrentPage(1);
		dataForSearchBar.setQueryText(null);
		dataForSearchBar.setNumberResultsPerPage(10);
		model.put("advancedSearchData", dataForSearchBar);
	}

    @RequestMapping(method=RequestMethod.GET)
    public String prepareUserRegistrationForm(
        @ModelAttribute("user") User user,
        Model model){
        LOG.debug("------ prepareUserRegistrationForm : Received request to show user registration form -----");

        user = new User();
        model.addAttribute("user", user);

        return "user_registration_def";
    }

    @RequestMapping(method=RequestMethod.POST)
    public String prepareUserRegistrationRequest(
        @ModelAttribute("user") User user,
        BindingResult result,
        Model model,
        @RequestParam("recaptcha_challenge_field") String challangeField,
        @RequestParam("recaptcha_response_field") String responseField,
        ServletRequest servletRequest, SessionStatus sessionStatus
    ){

        LOG.debug("------ processContactForm : form is being validated and processed -----");
        userRegistrationFormValidator.validate(user, result);

        /** TODO introduce recaptcha...

        String remoteAddress = servletRequest.getRemoteAddr();
        ReCaptchaResponse reCaptchaResponse = this.reCaptcha.checkAnswer(
            remoteAddress, challangeField, responseField);
         */
        model.addAttribute("user", user);
        if(!result.hasErrors()){
        //if(!result.hasErrors() && reCaptchaResponse.isValid()){

            user.setRegistrationToken(UUID.randomUUID().toString());
            userService.submitUserForRegistration(user);
            emailNotificationService.sendRegistrationEmailConfirmation(user);
            emailNotificationService.sendUserRegistrationNotification(user);
            sessionStatus.setComplete();
            String queryString="?email=" + user.getUsername() + "&displayName=" + user.getDisplayName();
            return "redirect:/users/registration/confirm-email" + queryString;
            //return "user_registration_sent_email_def";
        } else {
            /*
            if (!reCaptchaResponse.isValid()) {
                result.rejectValue("invalidRecaptcha", "invalid.captcha");
                model.addAttribute("invalidRecaptcha", true);
            }
            */
            return "user_registration_def";
        }


    }

    @RequestMapping(value = "confirm-email", method=RequestMethod.GET)
    public String requestEmailConfirmationPage(
        @RequestParam(value="email", required=false) String email,
        @RequestParam(value="displayName", required=false) String displayName,

        @ModelAttribute("user") User user,
        Model model){

        LOG.debug("------ display page asking the user to confirm the email -----");

        model.addAttribute("email", email);
        model.addAttribute("displayName", displayName);

        return "user_registration_sent_email_def";
    }

    @RequestMapping(value = "confirmed", method=RequestMethod.GET)
    public String emailConfirmated(
        @RequestParam(value="user", required=true) String username,
        @RequestParam(value="token", required=true) String registrationToken
        ){

        LOG.debug("------ received confirmation email -----");
        userService.enableUser(username, registrationToken);

        return "redirect:/login/custom_login?isConfirmedEmail=true";
    }
}

@@ -282,16 +282,20 @@
 re.user.
+User
 EmailNot
@@ -304,32 +304,32 @@
 icationService;%0A
-
 import org.podca
@@ -422,16 +422,60 @@
 owired;%0A
+import org.springframework.http.HttpStatus;%0A
 import o
@@ -681,163 +681,36 @@
 web.
-bind.annotation.ModelAttribute;%0Aimport org.springframework.web.bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RequestMethod
+HttpSessionRequiredException
 ;%0Aim
@@ -754,28 +754,17 @@
 otation.
-RequestParam
+*
 ;%0Aimport
@@ -1206,16 +1206,20 @@
 private 
+User
 EmailNot
@@ -2709,16 +2709,28 @@
 ptcha...
+          */
 %0A%0A      
@@ -2812,16 +2812,17 @@
 esponse 
+ 
 reCaptch
@@ -2920,28 +2920,17 @@
 Field);%0A
-         */
 %0A
+
         
@@ -2971,16 +2971,18 @@
         
+//
 if(!resu
@@ -2998,34 +2998,32 @@
 ors())%7B%0A        
-//
 if(!result.hasEr
@@ -3647,23 +3647,8 @@
 e %7B%0A
-            /*%0A
     
@@ -3846,23 +3846,9 @@
   %7D%0A
-            */
 %0A
+
     
@@ -3898,17 +3898,16 @@
      %7D%0A%0A
-%0A
     %7D%0A%0A 
@@ -4863,24 +4863,24 @@
 ionToken);%0A%0A
-
         retu
@@ -4941,11 +4941,200 @@
 %22;%0A    %7D
+%0A%0A    @ExceptionHandler(HttpSessionRequiredException.class)%0A    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)%0A    public String handleSessionExpired() %7B%0A        return %22login_def%22;%0A    %7D
 %0A%7D%0A

enable recaptcha