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>()
    .OnInitialize((server, request, ct) =>
    {
        server.SendNotification("TEST");
        var workspaceManager = server.GetRequiredService<WorkspaceManager>();

        if (request.RootPath != null)
        {
            foreach (var file in Directory.GetFiles(request.RootPath, "*.nub", SearchOption.AllDirectories))
            {
                workspaceManager.UpdateFile(file);
            }
        }

        return Task.CompletedTask;
    })
);

await server.WaitForExit;