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

namespace RobotNet.WebApp.Clients;

public class ProcessorHubClient : WebAssemblyHubClient
{
    public event Action<ProcessorState>? StateChanged;
    public event Action<ProcessorRequest>? RequestChanged;

    public ProcessorHubClient(IAccessTokenProvider tokenProvider, Uri uri) : base(tokenProvider, uri)
    {
        Connection.On<ProcessorState>(nameof(StateChanged), OnStateChanged);
        Connection.On<ProcessorRequest>(nameof(RequestChanged), OnRequestChanged);
    }

    public async Task<ProcessorState> GetState() => await Connection.InvokeAsync<ProcessorState>(nameof(GetState));
    public async Task<ProcessorRequest> GetRequest() => await Connection.InvokeAsync<ProcessorRequest>(nameof(GetRequest));
    public async Task<MessageResult> Build() => await Connection.InvokeAsync<MessageResult>(nameof(Build));
    public async Task<MessageResult> Run() => await Connection.InvokeAsync<MessageResult>(nameof(Run));
    public async Task<MessageResult> Stop() => await Connection.InvokeAsync<MessageResult>(nameof(Stop));
    public async Task<MessageResult> Reset() => await Connection.InvokeAsync<MessageResult>(nameof(Reset));

    private void OnStateChanged(ProcessorState state) => StateChanged?.Invoke(state);
    private void OnRequestChanged(ProcessorRequest request) => RequestChanged?.Invoke(request);

}
