package i5.las2peer.connectors.webConnector.handler;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import i5.las2peer.connectors.webConnector.WebConnector;
import i5.las2peer.logging.L2pLogger;
import i5.las2peer.tools.SimpleTools;

@Path(SwaggerUIHandler.RESOURCE_PATH)
public class SwaggerUIHandler extends AbstractFileHandler {

	public static final String RESOURCE_PATH = DefaultHandler.ROOT_RESOURCE_PATH + "/swagger-ui";

	private final L2pLogger logger = L2pLogger.getInstance(SwaggerUIHandler.class);

	private final WebConnector connector;

	public SwaggerUIHandler(WebConnector connector) {
		this.connector = connector;
	}

	@GET
	public Response rootPath() throws IOException, URISyntaxException {
		return Response.temporaryRedirect(new URI(RESOURCE_PATH + "/index.html")).build();
	}

	@GET
	@Path("{any: .*}")
	public Response processRequest(@PathParam("any") String path) throws IOException, URISyntaxException {
		if (path.isEmpty() || path.endsWith("/")) { // don't list directories
			return rootPath();
		} else {
			return serveFile(path);
		}
	}

	@Override
	protected Response serveFile(String filename) throws IOException {
		String resourceName = "/META-INF/resources/webjars/swagger-ui/2.2.10/" + filename;
		InputStream is = getClass().getResourceAsStream(resourceName);
		if (is == null) {
			logger.info("File not found: '" + resourceName + "'");
			return Response.status(Status.NOT_FOUND).build();
		}
		byte[] bytes = SimpleTools.toByteArray(is);
		String strContent = new String(bytes, StandardCharsets.UTF_8);
		if (filename.equalsIgnoreCase("index.html")) {
			String oidcClientId = connector.getOidcClientId();
			if (oidcClientId != null) {
				strContent = strContent.replace("clientId: \"your-client-id\",", "clientId: \"" + oidcClientId + "\",");
			}
			String oidcClientSecret = connector.getOidcClientSecret();
			strContent = strContent.replace("clientSecret: \"your-client-secret-if-required\",",
					"clientSecret: \"" + oidcClientSecret + "\",");
			return Response.ok(strContent).build();
		} else {
			return Response.ok(bytes).build();
		}
	}
}

@@ -278,24 +278,59 @@
 .PathParam;%0A
+import javax.ws.rs.core.MediaType;%0A
 import javax
@@ -389,24 +389,63 @@
 se.Status;%0A%0A
+import org.apache.commons.io.IOUtils;%0A%0A
 import i5.la
@@ -530,46 +530,8 @@
 ger;
-%0Aimport i5.las2peer.tools.SimpleTools;
 %0A%0A@P
@@ -1528,16 +1528,66 @@
 lename;%0A
+%09%09if (filename.equalsIgnoreCase(%22index.html%22)) %7B%0A%09
 %09%09InputS
@@ -1643,16 +1643,17 @@
 eName);%0A
+%09
 %09%09if (is
@@ -1667,16 +1667,17 @@
 l) %7B%0A%09%09%09
+%09
 logger.i
@@ -1719,24 +1719,25 @@
 ame + %22'%22);%0A
+%09
 %09%09%09return Re
@@ -1783,56 +1783,12 @@
 ;%0A%09%09
+%09
 %7D%0A%09
-%09byte%5B%5D bytes = SimpleTools.toByteArray(is);%0A
 %09%09St
@@ -1809,23 +1809,26 @@
 t = 
-new 
+IOUtils.to
 String(
-byte
+i
 s, S
@@ -1855,57 +1855,8 @@
 8);%0A
-%09%09if (filename.equalsIgnoreCase(%22index.html%22)) %7B%0A
 %09%09%09S
@@ -2112,16 +2112,52 @@
 cret();%0A
+%09%09%09if (oidcClientSecret != null) %7B%0A%09
 %09%09%09strCo
@@ -2241,16 +2241,17 @@
 %22,%0A%09%09%09%09%09
+%09
 %22clientS
@@ -2290,16 +2290,21 @@
 %22%5C%22,%22);%0A
+%09%09%09%7D%0A
 %09%09%09retur
@@ -2327,16 +2327,37 @@
 rContent
+, MediaType.TEXT_HTML
 ).build(
@@ -2384,33 +2384,36 @@
 urn 
-Response.ok(bytes).build(
+super.serveFile(resourceName
 );%0A%09

add SwaggerUIHandler mime type detection