@page "/robots/detail/{RobotId}"
@attribute [Authorize]

@implements IAsyncDisposable

@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using RobotNet.RobotShares.Dtos
@using RobotNet.Shares
@using RobotNet.WebApp.Clients
@using RobotNet.WebApp.Robots.Components.Robot

@inject RobotHubClient RobotHub
@inject IHttpClientFactory HttpFactory

<PageTitle>Robot Detail</PageTitle>

<div class="d-flex flex-row w-100 h-100 p-1 position-relative">
    <div style="width: 35%; max-width: 560px">
        <RobotAction @ref="@RobotActionRef" Robot="@Robot" />
        <RobotTestRandom @ref="@RobotTestRandomRef" Robot="@Robot" />
    </div>
    <div style="width: 65%" class="ms-2  flex-grow-1">
        <RobotInfomation @ref="@RobotInfomationRef" />
    </div>

    <MudOverlay @bind-Visible="IsDeactive" DarkBackground="true" Absolute="true">
        <MudText Typo="Typo.h1" Color="@Color.Error" Style="font-weight: bold;">@DeactiveStr</MudText>
    </MudOverlay>
</div>

@code {
    [Parameter]
    public string RobotId { get; set; } = "";

    private RobotDto Robot = new();
    private RobotInfomation RobotInfomationRef = default!;
    private RobotAction RobotActionRef = default!;
    private RobotTestRandom RobotTestRandomRef = default!;
    private bool IsDeactive;
    private string DeactiveStr = "Robot Empty";

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (!firstRender) return;

        RobotHub.VDA5050InfoChanged += VDA5050InfoChanged;
        RobotHub.RobotDetailDeactive += RobotDetailDeactive;
        await RobotHub.StartAsync();

        await LoadRobot();
    }

    private async Task LoadRobot()
    {
        try
        {
            IsDeactive = true;
            StateHasChanged();

            using var Http = HttpFactory.CreateClient("RobotManagerAPI");
            var result = await Http.GetFromJsonAsync<MessageResult<RobotDto?>>($"api/Robots/{RobotId}");
            if (result is null || result.Data is null || !result.IsSuccess) return;
            Robot = result.Data;

            await RobotHub.RobotDetailActive(RobotId);

            IsDeactive = false;
            StateHasChanged();
            await RobotActionRef.LoadNodes();
            await RobotActionRef.LoadActionAsync();
            await RobotTestRandomRef.LoadNodes();
        }
        catch (AccessTokenNotAvailableException ex)
        {
            ex.Redirect();
        }
    }

    private void VDA5050InfoChanged(RobotVDA5050StateDto state)
    {
        if (state.RobotId == RobotId)
        {
            Robot.Online = state.Online;
            RobotInfomationRef.UpdateState(state);
            RobotActionRef.UpdateState(state.Online, state.IsWorking);
            RobotTestRandomRef.UpdateState(state.Online, state.IsWorking);
        }
        StateHasChanged();
    }

    private void RobotDetailDeactive()
    {
        DeactiveStr = "Deactive";
        IsDeactive = true;
        StateHasChanged();
        RobotHub.VDA5050InfoChanged -= VDA5050InfoChanged;
        RobotHub.RobotDetailDeactive -= RobotDetailDeactive;
        _ = RobotHub.StopAsync();
    }

    public async ValueTask DisposeAsync()
    {
        RobotHub.VDA5050InfoChanged -= VDA5050InfoChanged;
        RobotHub.RobotDetailDeactive -= RobotDetailDeactive;
        await RobotHub.StopAsync();
    }
}
