package com.fp.frontend.filter;

import java.io.IOException;

import javax.faces.FactoryFinder;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fp.persistence.pgeneral.safe.TsafeUserDetail;

public class TimeoutManagerFilter implements Filter {

    private String contextPath;

    @Override
    public void doFilter(ServletRequest paramServletRequest, ServletResponse paramServletResponse, FilterChain paramFilterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) paramServletRequest;
        HttpServletResponse response = (HttpServletResponse) paramServletResponse;
        String path = request.getServletPath();

        if ((path.contains("/javax.faces.resource/")) || ((!path.endsWith(".jsf")) && (!path.endsWith(".xhtml")))) {
            paramFilterChain.doFilter(paramServletRequest, paramServletResponse);
            return;
        }
        if (this.contextPath == null) {
            this.contextPath = request.getContextPath();
        }

        try {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            if (facesContext == null) {
                FacesContextFactory contextFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
                LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
                Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

                ServletContext servletContext = request.getSession().getServletContext();
                facesContext = contextFactory.getFacesContext(servletContext, paramServletRequest, paramServletResponse, lifecycle);
                InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);
                if (null == facesContext.getViewRoot()) {
                    facesContext.setViewRoot(new UIViewRoot());
                }
            }
            HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);

            TsafeUserDetail currentUser = session != null ? (TsafeUserDetail) session.getAttribute("TSAFEUSERDETAIL") : null;
            boolean isIndexPage = path.endsWith("index.xhtml") || path.endsWith(this.contextPath + "/");
            boolean isLoginPage = path.endsWith("login.xhtml");

            if (!isLoginPage && !isIndexPage && (currentUser == null)) {
                response.sendRedirect(this.contextPath + "/login.xhtml");
            } else if (isLoginPage && (currentUser != null)) {
                response.sendRedirect(this.contextPath + "/pages/workspace.xhtml");
            }

        } catch (Throwable e) {
            e.printStackTrace();
            return;
        }
        paramFilterChain.doFilter(paramServletRequest, paramServletResponse);
    }

    private abstract static class InnerFacesContext extends FacesContext {

        protected static void setFacesContextAsCurrentInstance(final FacesContext facesContext) {
            FacesContext.setCurrentInstance(facesContext);
        }
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

}
