/**
 * 
 */
package org.yacos.web.system.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;
import org.yacos.core.article.IArticleManager;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.User;
import org.yacos.core.users.UserManagerBean;
import org.yacos.web.system.form.FormUser;

/**
 * FormController class for the user Sign In scenario (user registration)
 * 
 * TODO: implement the invitation system
 * 
 * @author christiancorsano
 *
 */
public class SUserRegistrationController extends SimpleFormController {

	private IUserManager usersManager;
	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		FormUser userCommand = (FormUser) command;
		User user = (User) request.getSession().getAttribute("currentUser");
		
		if((user == null) && (usersManager.getUser(userCommand.getLogin()) != null)){
			// TODO : we have an "already registered" error here, handle it
		}
		
		if(user == null){
			user = new User();
		}
		
		user.setLogin(userCommand.getLogin());
		user.setEmail(userCommand.getEmail());
		user.setFirstName(userCommand.getFirstName());
		user.setLastName(userCommand.getLastName());
		user.setOrganization(userCommand.getOrganization());
		
		if(userCommand.passwordWasModified()){
			user.setPassword(userCommand.getPassword());
		}
		
		// TODO Auto-generated method stub
		return new ModelAndView(new RedirectView(getSuccessView()));
	}

	/**
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		FormUser user = new FormUser();
		
		// We try to see if the user is already registered and logged
		User currentUser = new User();
		// TODO : see how JAAS could change the way of getting the current User
		if(request.getSession().getAttribute("currentUser") != null){
			// If he is logged, we use his profile information to fill the form
			currentUser = (User) request.getSession().getAttribute("currentUser");
		}
		
		user.setLogin(currentUser.getLogin());
		user.setFirstName(currentUser.getFirstName());
		user.setLastName(currentUser.getLastName());
		user.setEmail(currentUser.getEmail());
		user.setOrganization(currentUser.getOrganization());
		
		return super.formBackingObject(request);
	}
	
	public void setUsersManager(IUserManager usersManager) {
		this.usersManager = usersManager;
	}
	
	public IUserManager getUsersManager() {
		return usersManager;
	}
}
