
package managedbean.common;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import TO.LoggedUserTO;
import common.UserType;

public class SessionUtils {
	public static final String SESSION_VAR_USERNAME = "userName";
	public static final String SESSION_VAR_USERID = "userId";
	public static final String SESSION_VAR_USERTYPE = "userType";
	public static final String SESSION_VAR_USER = "loggedOnUser";
	public static final String SESSION_VAR_MESSAGE = "facesMessage";

	public static HttpSession getSession(boolean create) {
		FacesContext ctx = FacesContext.getCurrentInstance();
		if (ctx != null)
			return (HttpSession) ctx.getExternalContext().getSession(create);
		else
			return null;
	}
	
	public static HttpSession getSession() {
		return getSession(false);
	}

	public static HttpServletRequest getRequest() {
		return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	}

	public static HttpSession getSession(ServletRequest request) {
		return ((HttpServletRequest) request).getSession(false);
	}

	public static void createOrUpdateSession(LoggedUserTO usr) {
		HttpSession ses = getSession(true);
		ses.setAttribute(SessionUtils.SESSION_VAR_USERNAME, usr.getName());
		ses.setAttribute(SessionUtils.SESSION_VAR_USERID, usr.getId());
		ses.setAttribute(SessionUtils.SESSION_VAR_USERTYPE, usr.getUserType());
		ses.setAttribute(SessionUtils.SESSION_VAR_USER, usr);
	}

	public static void DestroySession() {
		HttpSession ses = getSession();
		ses.invalidate();
	}

	public static boolean isLogedIn() {
		return isLogedIn(getSession());
	}

	public static boolean isLogedIn(HttpSession session) {
		if (session != null && session.getAttribute(SessionUtils.SESSION_VAR_USERID) != null)
			return true;
		else
			return false;
	}

	public static String getUserName() {
		HttpSession session = getSession();
		if (session != null && session.getAttribute(SessionUtils.SESSION_VAR_USERNAME) != null)
			return session.getAttribute(SessionUtils.SESSION_VAR_USERNAME).toString();
		else
			return "";
	}

	public static String getUserId() {
		HttpSession session = getSession();
		if (session != null && session.getAttribute(SessionUtils.SESSION_VAR_USERID) != null)
			return session.getAttribute(SessionUtils.SESSION_VAR_USERID).toString();
		else
			return "";
	}

	public static UserType getUserType() {
		return getUserType(getSession());
	}

	public static UserType getUserType(HttpSession session) {
		if (session != null && session.getAttribute(SessionUtils.SESSION_VAR_USERTYPE) != null)
			return UserType.class.cast(session.getAttribute(SessionUtils.SESSION_VAR_USERTYPE));
		else
			return null;
	}

	public static LoggedUserTO getloggedOnUser() {
		HttpSession session = getSession();
		if (session != null && session.getAttribute(SessionUtils.SESSION_VAR_USER) != null)
			return LoggedUserTO.class.cast(session.getAttribute(SessionUtils.SESSION_VAR_USER));
		else
			return null;
	}

	public static FacesMessage getMessage() {
		HttpSession session = getSession();
		if (session != null && session.getAttribute(SessionUtils.SESSION_VAR_MESSAGE) != null) {
			FacesMessage msg = FacesMessage.class.cast(session.getAttribute(SessionUtils.SESSION_VAR_MESSAGE));
			session.removeAttribute(SessionUtils.SESSION_VAR_MESSAGE);
			return msg;
		} else
			return null;
	}

	public static void addMessage(FacesMessage.Severity severity, String summary, String detail) {
		addMessage(getSession(), severity, summary, detail);
	}

	public static void addMessage(HttpSession session, FacesMessage.Severity severity, String summary, String detail) {
		if (session != null)
			session.setAttribute(SessionUtils.SESSION_VAR_MESSAGE, new FacesMessage(severity, summary, detail));
	}

}
