@page "/admin/nodes/view/{id:int}"

@using Moonlight.App.Repositories
@using Moonlight.App.Database.Entities
@using Moonlight.App.Helpers
@using Moonlight.App.Services
@using Moonlight.App.ApiClients.Wings.Resources
@using Moonlight.App.ApiClients.Daemon.Resources

@inject NodeRepository NodeRepository
@inject NodeService NodeService

<OnlyAdmin>
<LazyLoader Load="Load">
@if (Node == null)
{
    <div class="alert alert-warning">
        <TL>No node with this id found</TL>
    </div>
}
else
{
    <div class="d-flex flex-center">
    <div class="row">
    <div class="card">
    <div class="card-header">
        <h3 class="card-title">
            <span class="fw-bold fs-3">
                @(Node.Name) <TL>details</TL>
            </span>
        </h3>
    </div>
    <div class="card-body">
    <div class="row g-3 g-lg-6">
        <div class="col">
            <div class="bg-gray-100 bg-opacity-70 rounded-2 px-6 py-5">
                <div class="symbol symbol-30px me-5 mb-8">
                    <span class="symbol-label">
                        <i class="text-primary bx bx-lg bx-chip"></i>
                    </span>
                </div>
                <div class="m-0">
                    <span class="fw-bolder d-block fs-2qx lh-1 ls-n1 mb-1">
                        @if (CpuMetrics == null)
                        {
                            <span class="text-muted">
                                <TL>Loading</TL>
                            </span>
                        }
                        else
                        {
                            <span>
                                @(CpuMetrics.CpuUsage)% <TL>of CPU used</TL>
                            </span>
                        }
                    </span>
                    <span class="fw-semibold fs-6">
                        @if (CpuMetrics == null)
                        {
                            <span class="text-muted">
                                <TL>Loading</TL>
                            </span>
                        }
                        else
                        {
                            <span>@(CpuMetrics.CpuModel)</span>
                        }
                    </span>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="bg-gray-100 bg-opacity-70 rounded-2 px-6 py-5">
                <div class="symbol symbol-30px me-5 mb-8">
                    <span class="symbol-label">
                        <i class="text-primary bx bx-lg bx-microchip"></i>
                    </span>
                </div>
                <div class="m-0">
                    <span class="fw-bolder d-block fs-2qx lh-1 ls-n1 mb-1">
                        @if (MemoryMetrics == null)
                        {
                            <span class="text-muted">
                                <TL>Loading</TL>
                            </span>
                        }
                        else
                        {
                            <span>
                                @(Formatter.FormatSize(MemoryMetrics.Used)) <TL>of</TL> @(Formatter.FormatSize(MemoryMetrics.Total)) <TL>memory used</TL>
                            </span>
                        }
                    </span>
                    <span class="fw-semibold fs-6">
                        @*IDK what to put here*@
                    </span>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="bg-gray-100 bg-opacity-70 rounded-2 px-6 py-5">
                <div class="symbol symbol-30px me-5 mb-8">
                    <span class="symbol-label">
                        <i class="text-primary bx bx-lg bx-microchip"></i>
                    </span>
                </div>
                <div class="m-0">
                    <span class="fw-bolder d-block fs-2qx lh-1 ls-n1 mb-1">
                        @if (DiskMetrics == null)
                        {
                            <span class="text-muted">
                                <TL>Loading</TL>
                            </span>
                        }
                        else
                        {
                            <span>
                                @(Formatter.FormatSize(DiskMetrics.Used)) <TL>of</TL> @(Formatter.FormatSize(DiskMetrics.Total)) <TL>used</TL>
                            </span>
                        }
                    </span>
                    <span class="fw-semibold fs-6">
                        @*IDK what to put here*@
                    </span>
                </div>
            </div>
        </div>
    </div>
    <div class="mt-3 row g-3 g-lg-6">
        <div class="col">
            <div class="bg-gray-100 bg-opacity-70 rounded-2 px-6 py-5">
                <div class="symbol symbol-30px me-5 mb-8">
                    <span class="symbol-label">
                        <i class="text-primary bx bx-lg bx-purchase-tag"></i>
                    </span>
                </div>
                <div class="m-0">
                    <span class="fw-bolder d-block fs-2qx lh-1 ls-n1 mb-1">
                        @if (SystemStatus == null)
                        {
                            <span class="text-muted">
                                <TL>Loading</TL>
                            </span>
                        }
                        else
                        {
                            <span class="text-success">
                                <TL>Online</TL>
                            </span>
                        }
                    </span>
                    <span class="fw-semibold fs-6">
                        @if (SystemStatus == null)
                        {
                            <span class="text-muted">
                                <TL>Loading</TL>
                            </span>
                        }
                        else
                        {
                            <span>@(SystemStatus.Version)</span>
                        }
                    </span>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="bg-gray-100 bg-opacity-70 rounded-2 px-6 py-5">
                <div class="symbol symbol-30px me-5 mb-8">
                    <span class="symbol-label">
                        <i class="text-primary bx bx-lg bx-fingerprint"></i>
                    </span>
                </div>
                <div class="m-0">
                    <span class="fw-bolder d-block fs-2qx lh-1 ls-n1 mb-1">
                        @if (SystemStatus == null)
                        {
                            <span class="text-muted">
                                <TL>Loading</TL>
                            </span>
                        }
                        else
                        {
                            <span>
                                @(SystemStatus.KernelVersion) - @(SystemStatus.Architecture)
                            </span>
                        }
                    </span>
                    <span class="fw-semibold fs-6">
                        @if (SystemStatus == null)
                        {
                            <span class="text-muted">
                                <TL>Loading</TL>
                            </span>
                        }
                        else
                        {
                            <TL>Host system information</TL>
                        }
                    </span>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="bg-gray-100 bg-opacity-70 rounded-2 px-6 py-5">
                <div class="symbol symbol-30px me-5 mb-8">
                    <span class="symbol-label">
                        <i class="text-primary bx bx-lg bxl-docker"></i>
                    </span>
                </div>
                <div class="m-0">
                    <span class="fw-bolder d-block fs-2qx lh-1 ls-n1 mb-1">
                        @if (DockerMetrics == null)
                        {
                            <span class="text-muted">
                                <TL>Loading</TL>
                            </span>
                        }
                        else
                        {
                            <span>
                                <TL>@(DockerMetrics.Containers.Length)</TL>
                            </span>
                        }
                    </span>
                    <span class="fw-semibold fs-6">
                        @if (DockerMetrics == null)
                        {
                            <span class="text-muted">
                                <TL>Loading</TL>
                            </span>
                        }
                        else
                        {
                            <TL>Docker containers running</TL>
                        }
                    </span>
                </div>
            </div>
        </div>
    </div>
    </div>
    </div>
    <div class="mt-5 card card-body">
        <div class="d-flex justify-content-end">
            <a href="/admin/nodes" class="btn btn-primary">
                <TL>Cancel</TL>
            </a>
        </div>
    </div>
    </div>
    </div>
}
</LazyLoader>
</OnlyAdmin>

@code
{
    [Parameter]
    public int Id { get; set; }

    private Node? Node;

    private CpuMetrics CpuMetrics;
    private MemoryMetrics MemoryMetrics;
    private DiskMetrics DiskMetrics;
    private DockerMetrics DockerMetrics;
    private SystemStatus SystemStatus;

    private async Task Load(LazyLoader arg)
    {
        Node = NodeRepository
            .Get()
            .FirstOrDefault(x => x.Id == Id);

        if (Node != null)
        {
            Task.Run(async () =>
            {
                try
                {
                    SystemStatus = await NodeService.GetStatus(Node);
                    await InvokeAsync(StateHasChanged);

                    CpuMetrics = await NodeService.GetCpuMetrics(Node);
                    await InvokeAsync(StateHasChanged);

                    MemoryMetrics = await NodeService.GetMemoryMetrics(Node);
                    await InvokeAsync(StateHasChanged);

                    DiskMetrics = await NodeService.GetDiskMetrics(Node);
                    await InvokeAsync(StateHasChanged);

                    DockerMetrics = await NodeService.GetDockerMetrics(Node);
                    await InvokeAsync(StateHasChanged);
                }
                catch (Exception e)
                {
    // ignored
                }
            });
        }
    }
}