package org.keycloak.testframework.server;

import java.util.concurrent.TimeoutException;

import org.keycloak.Keycloak;
import org.keycloak.common.Version;
import org.keycloak.testframework.util.MavenProjectUtil;

public class EmbeddedKeycloakServer implements KeycloakServer {

    private Keycloak keycloak;
    private boolean tlsEnabled = false;

    @Override
    public void start(KeycloakServerConfigBuilder keycloakServerConfigBuilder, boolean tlsEnabled) {
        Keycloak.Builder builder = Keycloak.builder().setVersion(Version.VERSION);
        this.tlsEnabled = tlsEnabled;

        for(KeycloakDependency dependency : keycloakServerConfigBuilder.toDependencies()) {
            KeycloakDependency updatedDependency = MavenProjectUtil.updateDependencyDetails(dependency);
            builder.addDependency(updatedDependency.getGroupId(), updatedDependency.getArtifactId(), updatedDependency.getVersion());
        }

        keycloak = builder.start(keycloakServerConfigBuilder.toArgs());
        ReadinessProbe.waitUntilReady(this);
    }

    @Override
    public void stop() {
        try {
            keycloak.stop();
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }
    }

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

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