package demojsoncrud.servlets;

import demojsoncrud.utilities.PostRequest;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

@WebFilter("/*")
public class RequestFilter implements Filter, ServletContextListener {

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

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        String respuesta = "";
        String indexPath = "http://localhost:8080/proyecto-estandar/vistas/index.jsp";
        String index = "vistas/index.jsp";
        String urlRequested = "http://172.16.2.53:8080/security-rest/api/user/verificarLogin";

        if (String.valueOf(request.getRequestURL()).contains("http://localhost:8080/proyecto-estandar/")) {
            String login = request.getContextPath() + "/vistas/index.jsp";
            String main = request.getContextPath() + "/vistas/main.jsp";
            String plantilla = request.getContextPath() + "/plantilla";
            String js = request.getContextPath() + "/js";
            String url = String.valueOf(request.getRequestURL());
            if (url.contains(login) || url.contains(plantilla) || url.contains(js) || url.contains(main) || url.contains(".jsp")) {
                chain.doFilter(request, response);
            } else {
                String auth = request.getHeader("Authorization");
                PostRequest postRequest = new PostRequest(urlRequested);
                respuesta = postRequest.getRespueta(auth);
                JSONObject valid = new JSONObject(respuesta);
                if (valid.getBoolean("status")) {
                    chain.doFilter(request, response);
                } else {
                    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                    response.sendError(401);
                }
            }
        } else {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.sendError(401);
        }
    }
    @Override
    public void destroy() {

    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}