package io.quarkus.test.common.http;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;

/**
 *
 */
public class TestHTTPConfigSourceProvider implements ConfigSourceProvider {

    static final String TEST_URL_VALUE = "http://${quarkus.http.host:localhost}:${quarkus.http.test-port:8081}${quarkus.http.root-path:${quarkus.servlet.context-path:}}";
    static final String TEST_URL_KEY = "test.url";

    static final String TEST_URL_SSL_VALUE = "https://${quarkus.http.host:localhost}:${quarkus.http.test-ssl-port:8444}${quarkus.http.root-path:${quarkus.servlet.context-path:}}";
    static final String TEST_URL_SSL_KEY = "test.url.ssl";

    static final Map<String, String> entries;

    static {
        Map<String, String> map = new HashMap<>();
        map.put(TEST_URL_KEY, sanitizeURL(TEST_URL_VALUE));
        map.put(TEST_URL_SSL_KEY, sanitizeURL(TEST_URL_SSL_VALUE));
        map.put("%dev." + TEST_URL_KEY, sanitizeURL(
                "http://${quarkus.http.host:localhost}:${quarkus.http.test-port:8080}${quarkus.http.root-path:${quarkus.servlet.context-path:}}"));
        entries = Collections.unmodifiableMap(map);
    }

    private static String sanitizeURL(String url) {
        return url.replace("0.0.0.0", "localhost");
    }

    public Iterable<ConfigSource> getConfigSources(final ClassLoader forClassLoader) {
        return Collections.singletonList(new TestURLConfigSource());
    }

    static class TestURLConfigSource implements ConfigSource, Serializable {
        private static final long serialVersionUID = 4841094273900625000L;

        public Map<String, String> getProperties() {
            return entries;
        }

        public Set<String> getPropertyNames() {
            return entries.keySet();
        }

        public String getValue(final String propertyName) {
            return entries.get(propertyName);
        }

        public String getName() {
            return "test URL provider";
        }

        public int getOrdinal() {
            return Integer.MIN_VALUE + 1000;
        }
    }
}

@@ -94,34 +94,8 @@
 ns;%0A
-import java.util.HashMap;%0A
 impo
@@ -857,90 +857,17 @@
 ries
-;%0A%0A    static %7B%0A        Map%3CString, String%3E map = new HashMap%3C%3E();%0A        map.put
+ = Map.of
 (TES
@@ -896,34 +896,33 @@
 (TEST_URL_VALUE)
-);
+,
 %0A        map.put
@@ -914,24 +914,20 @@
         
-map.put(
+    
 TEST_URL
@@ -967,18 +967,17 @@
 L_VALUE)
-);
+,
 %0A       
@@ -981,16 +981,12 @@
     
-map.put(
+    
 %22%25de
@@ -1018,16 +1018,20 @@
 izeURL(%0A
+    
         
@@ -1173,66 +1173,8 @@
 %22));
-%0A        entries = Collections.unmodifiableMap(map);%0A    %7D
 %0A%0A  

Use Map.of to simplify TestHTTPConfigSourceProvider