using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.AspNetCore.SignalR.Client;

namespace RobotNet.WebApp.Clients;

public class ConsoleHubClient : WebAssemblyHubClient
{
    public event Action<string>? MessageInfo;
    private IDisposable? disMessageInfo;

    public event Action<string>? MessageWarning;
    private IDisposable? disMessageWarning;

    public event Action<string>? MessageError;
    private IDisposable? disMessageError;

    public ConsoleHubClient(IAccessTokenProvider tokenProvider, Uri uri) : base(tokenProvider, uri)
    {
        disMessageInfo = Connection.On<string>(nameof(MessageInfo), message => MessageInfo?.Invoke(message));
        disMessageWarning = Connection.On<string>(nameof(MessageWarning), message => MessageWarning?.Invoke(message));
        disMessageError = Connection.On<string>(nameof(MessageError), message => MessageError?.Invoke(message));
    }

    public override async Task StopAsync()
    {
        disMessageInfo?.Dispose();
        disMessageInfo = null;
        disMessageWarning?.Dispose();
        disMessageWarning = null;
        disMessageError?.Dispose();
        disMessageError = null;
        await base.StopAsync();
    }

    public Task RegisterTaskConsole(string name) => Connection.InvokeAsync("RegisterTasksConsole", name);
    public Task UnregisterTaskConsole(string name) => Connection.InvokeAsync("UnregisterTasksConsole", name);
    public Task RegisterTaskConsoles() => Connection.InvokeAsync("RegisterTaskConsoles");
    public Task UnregisterTaskConsoles() => Connection.InvokeAsync("UnregisterTaskConsoles");
    public Task RegisterMissionConsole(Guid missionId) => Connection.InvokeAsync("RegisterMissionConsole", missionId);
    public Task UnregisterMissionConsole(Guid missionId) => Connection.InvokeAsync("UnregisterMissionConsole", missionId);
    public Task RegisterMissionConsoles() => Connection.InvokeAsync("RegisterMissionConsoles");
    public Task UnregisterMissionConsoles() => Connection.InvokeAsync("UnregisterMissionConsoles");
    public Task RegisterAllConsoles() => Connection.InvokeAsync("RegisterAllConsoles");
    public Task UnregisterAllConsoles() => Connection.InvokeAsync("UnregisterAllConsoles");

}
