using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace SignalRTestClient;

internal class Program
{
    static void Main()
    {
        var connectionBuilder = new HubConnectionBuilder();
        var connection = connectionBuilder
            .WithUrl("http://localhost:5000/hubs/limitingParameters", connectionOptions => { 
                connectionOptions.AccessTokenProvider = AccessTokenProvider; 
            })
            .WithAutomaticReconnect()
            .AddJsonProtocol()
            .ConfigureLogging(ConfigureLogging)
            .Build();
        
        connection.Reconnected += (connectionId) => Task.Run(()=> Console.WriteLine($"Reconnected {connectionId}"));
        connection.Closed += (exception) => Task.Run(()=> Console.WriteLine($"Closed {exception?.Message}"));
        connection.Reconnecting += (exception) => Task.Run(()=> Console.WriteLine($"Reconnecting {exception?.Message}"));

        Console.WriteLine("connecting");
        connection.StartAsync().Wait();

        //Console.WriteLine("OnConnected");
        connection.SendCoreAsync("OnConnectedAsync", new object[] { 1 }, CancellationToken.None).Wait();

        var subsction = connection.On<object>("well_info_update", (str1) => { 
            Console.WriteLine(str1);
        } );
        
        //connection.On("ReceiveDataSaub");
        //connection.On("ReceiveDataSpin");
        Console.ReadLine();

        connection.SendCoreAsync("RemoveFromGroup", new object[] { "well_1" }).Wait();

        Console.WriteLine("Done!");
    }

    private static void ConfigureLogging(ILoggingBuilder builder)
    {
        ILoggerProvider provider = new LoggerProvider();
        builder.AddProvider(provider);
    }

    private static Task<string> AccessTokenProvider()
    {
        return Task.FromResult("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImN0eSI6IkpXVCJ9.eyJpZCI6IjEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiZGV2IiwiaWRDb21wYW55IjoiMSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InJvb3QiLCJuYmYiOjE2NjI1NDgxNjIsImV4cCI6MTY5NDEwNTc2MiwiaXNzIjoiYSIsImF1ZCI6ImEifQ.OEAlNzxi7Jat6pzDBTAjTbChskc-tdJthJexyWwwUKE");
    }
}

internal class LoggerProvider : ILoggerProvider
{
    public ILogger CreateLogger(string categoryName)
    {
        ILogger logger = new ConsoleLogger();
        return logger;
    }

    public void Dispose()
    {
        //throw new NotImplementedException();
    }
}

internal class ConsoleLogger : ILogger
{
    public IDisposable? BeginScope<TState>(TState state) where TState : notnull
    {
        return null;
    }

    public bool IsEnabled(LogLevel logLevel)
    {
        return true;
    }

    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
    {
        Console.WriteLine(formatter(state, exception));
    }
}