using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NubLang.LSP;
using OmniSharp.Extensions.LanguageServer.Server;

var server = await LanguageServer.From(options => options
    .WithInput(Console.OpenStandardInput())
    .WithOutput(Console.OpenStandardOutput())
    .WithServices(services =>
    {
        services.AddSingleton<DiagnosticsPublisher>();
        services.AddSingleton<WorkspaceManager>();
    })
    .ConfigureLogging(x => x
        .AddLanguageProtocolLogging()
        .SetMinimumLevel(LogLevel.Debug))
    .WithHandler<TextDocumentSyncHandler>()
    .WithHandler<HoverHandler>()
    .WithHandler<CompletionHandler>()
    .WithHandler<DefinitionHandler>()
    .OnInitialize((server, request, ct) =>
    {
        var workspaceManager = server.GetRequiredService<WorkspaceManager>();

        if (request.RootPath != null)
        {
            workspaceManager.Init(request.RootPath);
        }

        return Task.CompletedTask;
    })
);

await server.WaitForExit;