package org.keycloak.testframework.server;

import java.net.ConnectException;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.net.ssl.SSLException;

import org.keycloak.it.utils.Maven;
import org.keycloak.testframework.config.Config;

import static java.lang.System.out;

public class RemoteKeycloakServer implements KeycloakServer {

    private boolean tlsEnabled = false;

    private String kcwCommand;

    @Override
    public void start(KeycloakServerConfigBuilder keycloakServerConfigBuilder, boolean tlsEnabled) {
        this.tlsEnabled = tlsEnabled;
        kcwCommand = Config.getValueTypeConfig(KeycloakServer.class, "kcw", null, String.class);
        if (!verifyRunningKeycloak()) {
            if (kcwCommand != null) {
                printStartupInstructionsKcw(keycloakServerConfigBuilder);
            } else {
                printStartupInstructionsManual(keycloakServerConfigBuilder);
            }
            waitForStartup();
        }
        ReadinessProbe.waitUntilReady(this);
    }

    @Override
    public void stop() {
    }

    @Override
    public String getBaseUrl() {
        if (tlsEnabled) {
            return "https://localhost:8443";
        } else {
            return "http://localhost:8080";
        }
    }

    @Override
    public String getManagementBaseUrl() {
        if (tlsEnabled) {
            return "https://localhost:9000";
        } else {
            return "http://localhost:9000";
        }
    }

    private void printStartupInstructionsManual(KeycloakServerConfigBuilder config) {
        out.println("Remote Keycloak server is not running on " + getBaseUrl() + ", please start Keycloak with:");
        out.println();
        out.println(String.join(" \\\n", config.toArgs()));
        out.println();

        Set<KeycloakDependency> dependencies = config.toDependencies();
        if (!dependencies.isEmpty()) {
            out.println("Requested providers:");
            for (KeycloakDependency d : dependencies) {
                out.println("* " + d.getGroupId() + ":" + d.getArtifactId());
            }
            out.println();
        }
    }

    private void printStartupInstructionsKcw(KeycloakServerConfigBuilder config) {
        out.println("Remote Keycloak server is not running on " + getBaseUrl() + ", please start Keycloak with:");
        out.println();

        Set<KeycloakDependency> dependencies = config.toDependencies();
        if (!dependencies.isEmpty()) {
            String dependencyPaths = dependencies.stream().map(d -> Maven.resolveArtifact(d.getGroupId(), d.getArtifactId()).toString()).collect(Collectors.joining(","));
            out.println("KCW_PROVIDERS=" + dependencyPaths + " \\");
        }

        out.println("kcw " + kcwCommand + " " + String.join(" \\\n", config.toArgs()));
        out.println();
    }

    private boolean verifyRunningKeycloak() {
        try {
            new URL(getBaseUrl()).openConnection().connect();
            return true;
        } catch (ConnectException e) {
            return false;
        } catch (SSLException ignored) {
            // if the kc server is running with https, it is not this class' responsibility to check the certificate
            // we're just checking that keycloak is running
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void waitForStartup() {
        long waitUntil = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5);
        while (!verifyRunningKeycloak() && System.currentTimeMillis() < waitUntil) {
            try {
                Thread.sleep(TimeUnit.SECONDS.toMillis(1));
            } catch (InterruptedException e) {
                return;
            }
        }
    }

}
