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

namespace RobotNet.WebApp.Clients;

public class ScriptTaskHubClient : WebAssemblyHubClient
{
    public event Action<string>? TaskPaused;
    public event Action<string>? TaskResumed;

    public ScriptTaskHubClient(IAccessTokenProvider tokenProvider, Uri uri) : base(tokenProvider, uri)
    {
        Connection.On<string>(nameof(TaskPaused), OnTaskPaused);
        Connection.On<string>(nameof(TaskResumed), OnTaskResumed);
    }

    public async Task<MessageResult> Pause(string name) => await Connection.InvokeAsync<MessageResult>(nameof(Pause), name);
    public async Task<MessageResult> Resume(string name) => await Connection.InvokeAsync<MessageResult>(nameof(Resume), name);
    public async Task<IDictionary<string, bool>> GetTaskStates() => await Connection.InvokeAsync<IDictionary<string, bool>>(nameof(GetTaskStates));


    private void OnTaskPaused(string name) => TaskPaused?.Invoke(name);
    private void OnTaskResumed(string name) => TaskResumed?.Invoke(name);
}
