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

namespace RobotNet.WebApp.Clients;

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

    public HMIHubClient(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<IDictionary<string, string>> GetVariables(IEnumerable<string> keys) => await Connection.InvokeAsync<IDictionary<string, string>>(nameof(GetVariables), keys);
    public async Task<MessageResult> SetVariable(string key, string value) => await Connection.InvokeAsync<MessageResult>(nameof(SetVariable), key, value);

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