package org.innovateuk.ifs.starters.stubdev.filter;

import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * This might be better a thymeleaf post processor TBF
 */

@Slf4j
public class RewriteFilter implements Filter {

    ImmutableMap<String, String> replacements = ImmutableMap.of(
//        "Innovation Funding Service", "STUB Innovation Funding Service Service",
        "https://localhost:8080/assessment/", "http://localhost:8082/assessment/",
            "https://localhost:8080/management/", "http://localhost:8083/management/"
    );

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse) response);
        chain.doFilter(request, wrapper);

        if (wrapper.getContentType() != null &&
                wrapper.getContentType().contains("text/html")) {
            Stopwatch timer = Stopwatch.createStarted();
            String originalContent = wrapper.toString();
            String alteredContent = originalContent;
            for(Map.Entry<String, String> entry : replacements.entrySet()) {
                alteredContent = alteredContent.replaceAll(entry.getKey(), entry.getValue());
            }
            alteredContent = alteredContent.replaceAll("Innovation Funding Service", "STUB IFS " + timer.stop());
            response.setContentLength(alteredContent.length());
            PrintWriter responseWriter = response.getWriter();
            responseWriter.write(alteredContent);
        }
    }

}


@@ -323,78 +323,147 @@
 p;%0A%0A
+%0A
 /**%0A * 
-This might be better a thymeleaf post processor TBF%0A */%0A%0A@Slf4j
+Work around the web tier wanting to all be on port 8080 for local non-container dev.%0A *%0A * Use a servlet filter to rewrite urls%0A */
 %0Apub
@@ -572,18 +572,16 @@
 Map.of(%0A
-//
         
@@ -585,77 +585,79 @@
    %22
-Innovation Funding Service%22, %22STUB Innovation Funding Service Service
+https://localhost:8080/assessment/%22, %22http://localhost:8081/assessment/
 %22,%0A 
@@ -679,38 +679,38 @@
 /localhost:8080/
-assess
+manage
 ment/%22, %22http://
@@ -724,22 +724,22 @@
 st:8082/
-assess
+manage
 ment/%22,%0A
@@ -738,24 +738,105 @@
 ment/%22,%0A    
+    %22https://localhost:8080/competition/%22, %22http://localhost:8083/competition/%22,%0A
         %22htt
@@ -847,32 +847,46 @@
 /localhost:8080/
+project-setup-
 management/%22, %22h
@@ -908,20 +908,198 @@
 :808
-3/management
+4/project-setup-management/%22,%0A        %22https://localhost:8080/project-setup/%22, %22http://localhost:8085/project-setup/%22,%0A        %22https://localhost:8080/survey/%22, %22http://localhost:8086/survey
 /%22%0A 
@@ -1264,16 +1264,68 @@
 ption %7B%0A
+        Stopwatch timer = Stopwatch.createStarted();
 %0A       
@@ -1454,17 +1454,16 @@
 apper);%0A
-%0A
         
@@ -1501,32 +1501,16 @@
  null &&
-%0A               
  wrapper
@@ -1570,159 +1570,48 @@
   St
-opwatch timer = Stopwatch.createStarted();%0A            String originalContent = wrapper.toString();%0A            String alteredContent = originalContent
+ring alteredContent = wrapper.toString()
 ;%0A  

rewrite urls following our port guide