package ub.scp2.managedBeans;

import java.io.IOException;
import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import ub.scp2.bo.UserBo;
import ub.scp2.entities.Task;
import ub.scp2.entities.User;


/**
 * LoginBean.java -a Bean to control the Login, and control the sesion.
 * 
 *
 */
@ManagedBean(name="loginBean")
@SessionScoped
public class LoginBean implements Serializable{

	private UserBo userbo;
	
	private User user;
	private Task task;
	
	public LoginBean(){
		
	}
	
	/**
	 * Initialize the parameters required for the class
	 */
    @PostConstruct
    public void init() { 	
    	WebApplicationContext ctx =  FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        userbo = ctx.getBean(UserBo.class);
    }
	
    /**
     * Does the user logging
     * @return
     * @throws IOException
     * @throws ServletException
     */
	public String doLogin() throws IOException, ServletException{
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        
        RequestDispatcher dispatcher = ((ServletRequest) context.getRequest())
                 .getRequestDispatcher("/j_spring_security_check");
        
 
        dispatcher.forward((ServletRequest) context.getRequest(),
                (ServletResponse) context.getResponse());
        
        FacesContext.getCurrentInstance().responseComplete();
    	
        user = userbo.getUserByUsername(getRemoteUsername());
        
        return null;
	}

	/**
	 * Does the user logout
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	public String doLogout() throws IOException, ServletException{
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        
        RequestDispatcher dispatcher = ((ServletRequest) context.getRequest())
                 .getRequestDispatcher("/j_spring_security_logout");
        
 
        dispatcher.forward((ServletRequest) context.getRequest(),
                (ServletResponse) context.getResponse());
        
        FacesContext.getCurrentInstance().responseComplete();

        return null;
	}
	

	
	public String getRemoteUsername(){
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		return ec.getRemoteUser();
	}

	/**
	 * Retrieve user
	 * @return A type User
	 */
	public User getUser() {
		return user;
	}
	
	/**
	 * Set a task
	 * @param task A type of Task
	 */
	public void setTask(Task task) {
		this.task = task;
	}
	
	/**
	 * Retrieve task
	 * @return A type Task
	 */
	public Task getTask() {
		return task;
	}
	
	/**
	 * Update the user with function getUserByeUsername
	 * @see UserBoImpl.java
	 */
	public void updateUser(){
		user = userbo.getUserByUsername(getRemoteUsername());
	}
}
