package org.javacs;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.logging.Logger;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.MessageActionItem;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.ShowMessageRequestParams;
import org.eclipse.lsp4j.services.LanguageClient;

class LanguageServerFixture {

    public static Path DEFAULT_WORKSPACE_ROOT = Paths.get("src/test/test-project/workspace").toAbsolutePath();

    static {
        Main.setRootFormat();
    }

    static JavaLanguageServer getJavaLanguageServer() {
        return getJavaLanguageServer(DEFAULT_WORKSPACE_ROOT, diagnostic -> LOG.info(diagnostic.getMessage()));
    }

    static JavaLanguageServer getJavaLanguageServer(Path workspaceRoot, Consumer<Diagnostic> onError) {
        return getJavaLanguageServer(
                workspaceRoot,
                new LanguageClient() {
                    @Override
                    public void telemetryEvent(Object o) {}

                    @Override
                    public void publishDiagnostics(PublishDiagnosticsParams publishDiagnosticsParams) {
                        publishDiagnosticsParams.getDiagnostics().forEach(onError);
                    }

                    @Override
                    public void showMessage(MessageParams messageParams) {}

                    @Override
                    public CompletableFuture<MessageActionItem> showMessageRequest(
                            ShowMessageRequestParams showMessageRequestParams) {
                        return null;
                    }

                    @Override
                    public void logMessage(MessageParams messageParams) {}
                });
    }

    private static JavaLanguageServer getJavaLanguageServer(Path workspaceRoot, LanguageClient client) {
        var server = new JavaLanguageServer();
        var init = new InitializeParams();

        init.setRootUri(workspaceRoot.toUri().toString());

        server.initialize(init);
        server.installClient(client);

        return server;
    }

    private static final Logger LOG = Logger.getLogger("main");
}

@@ -454,58 +454,8 @@
 ams;
-%0Aimport org.eclipse.lsp4j.services.LanguageClient;
 %0A%0Acl
@@ -1012,16 +1012,22 @@
     new 
+Custom
 Language
@@ -1835,16 +1835,334 @@
 rams) %7B%7D
+%0A%0A                    @Override%0A                    public void javaStartProgress(JavaStartProgressParams params) %7B%7D%0A%0A                    @Override%0A                    public void javaReportProgress(JavaReportProgressParams params) %7B%7D%0A%0A                    @Override%0A                    public void javaEndProgress() %7B%7D
 %0A       
@@ -2261,16 +2261,22 @@
 ceRoot, 
+Custom
 Language
@@ -2464,20 +2464,25 @@
 r.in
-itialize(ini
+stallClient(clien
 t);%0A
@@ -2498,34 +2498,63 @@
 erver.in
-stallClient(client
+itialize(init);%0A        server.initialized(null
 );%0A%0A    

Fix test fixture