package at.fhj.swd13.pse.controller;

import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;

import org.jboss.logging.Logger;
import org.primefaces.context.RequestContext;

import at.fhj.swd13.pse.db.entity.Person;
import at.fhj.swd13.pse.domain.user.UserService;

@ManagedBean
@Stateless
public class LoginController {

	private String username;

	private String password;

	@Inject
	private Logger logger;

	@Inject
	private UserService userService;


	public void login(ActionEvent event) {

		RequestContext context = RequestContext.getCurrentInstance();
		FacesMessage message = null;
		boolean loggedIn = false;

		if (username != null && password != null) {

			Person user = userService.loginUser(username, password);

			if (user != null) {
				loggedIn = true;
				logger.info("[LOGIN] logged-in-user " + user);
				message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", user.getFirstName() + " " + user.getLastName());
			} else {
				loggedIn = false;

				logger.info("[LOGIN] login failed for " + username + " from " + context.toString());

				message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error", "Invalid credentials");
			}
		} else {
			loggedIn = false;

			logger.info("[LOGIN] login failed for " + username + " from " + context.toString());

			message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error", "Invalid credentials");
		}

		FacesContext.getCurrentInstance().addMessage(null, message);
		context.addCallbackParam("loggedIn", loggedIn);
	}

	public void logout() {
		userService.logoutCurrentUser();
	}
	
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {

		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {

		this.password = password;
	}
}

@@ -31,16 +31,45 @@
 oller;%0A%0A
+import java.io.IOException;%0A%0A
 import j
@@ -170,16 +170,60 @@
 edBean;%0A
+import javax.faces.context.ExternalContext;%0A
 import j
@@ -682,17 +682,16 @@
 rvice;%0A%0A
-%0A
 %09public 
@@ -1764,16 +1764,17 @@
 out() %7B%0A
+%0A
 %09%09userSe
@@ -1800,20 +1800,248 @@
 User();%0A
+%0A%09%09ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();%0A%09%09try %7B%0A%09%09%09context.redirect(%22/%22);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09logger.error(%22%5BLOGIN%5D error redirecting after logout: %22 + e.getMessage());%0A%09
 %09%7D%0A%09
+%7D%0A
 %0A%09/**%0A%09 

update redirect on logout