@using Moonlight.Features.Servers.Entities
@using Moonlight.Features.Servers.Api.Resources
@using Moonlight.Features.Servers.Services
@using MoonCore.Helpers

@inject NodeService NodeService

@implements IDisposable

<LazyLoader Load="Load">
    <div class="row g-5 mt-5">
        <div class="col-md-3 col-12">
            @{
                var cpuName = Status.Hardware.Cores.Any() ? Status.Hardware.Cores.First().Name : "N/A";
            }

            <StatCard Value="@cpuName" Description="Cpu model" Icon="bxs-chip"/>
        </div>
        <div class="col-md-3 col-12">
            @{
                var memoryText = $"{Formatter.FormatSize(Status.Hardware.Memory.Total - Status.Hardware.Memory.Available - Status.Hardware.Memory.Free)} / {Formatter.FormatSize(Status.Hardware.Memory.Total)}";
            }

            <StatCard Value="@memoryText" Description="Memory usage" Icon="bxs-microchip"/>
        </div>
        <div class="col-md-3 col-12">
            <StatCard Value="@Status.Version" Description="Version" Icon="bxs-purchase-tag"/>
        </div>
        <div class="col-md-3 col-12">
            <StatCard Value="@Formatter.FormatUptime(Status.Hardware.Uptime)" Description="Uptime" Icon="bxs-time-five"/>
        </div>
        <div class="col-md-3 col-12">
            <StatCard Value="@Status.OperatingSystem" Description="Operating system" Icon="bx-fingerprint"/>
        </div>
        <div class="col-md-3 col-12">
            <StatCard Value="@Status.Containers.ToString()" Description="Containers" Icon="bxl-docker"/>
        </div>
        <div class="col-md-3 col-12">
            @{
                var diskText = $"{Formatter.FormatSize(Status.Hardware.Disk.Total - Status.Hardware.Disk.Free)} / {Formatter.FormatSize(Status.Hardware.Disk.Total)}";
            }
            
            <StatCard Value="@diskText" Description="Diskspace" Icon="bx-hdd"/>
        </div>
    </div>
    <SectionTitle>CPU Details</SectionTitle>
    <div class="row g-5">
        @foreach (var core in Status.Hardware.Cores)
        {
            <div class="col-md-3 col-12">
                <div class="card card-body p-5">
                    <div class="fw-semibold text-primary fs-2">
                        @{
                            var color = "primary";

                            if (core.Usage >= 60 && core.Usage < 80)
                                color = "warning";

                            if (core.Usage >= 80)
                                color = "danger";

                            var percent = Math.Round(core.Usage);
                            var percentText = Math.Round(core.Usage, 2) + "%";
                        }

                        <div class="progress">
                            <div class="progress-bar bg-@(color) fs-4" role="progressbar" aria-valuenow="@(percent)" aria-valuemin="0" aria-valuemax="100" style="width: @(percent)%">
                            </div>
                        </div>
                    </div>
                    <div class="fs-4 mt-2">
                        <div class="d-flex justify-content-between">
                            <span>Core #@(core.Id)</span>
                            <span class="fs-3">@percentText</span>
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>
</LazyLoader>

@code
{
    [Parameter] public ServerNode Node { get; set; }

    private Timer? UpdateTimer;
    private SystemStatus Status;

    private Task Load(LazyLoader arg)
    {
        Status = new()
        {
            Containers = 0,
            Version = "N/A",
            OperatingSystem = "N/A",
            Hardware = new()
            {
                Cores = Array.Empty<SystemStatus.HardwareInformationData.CpuCoreData>(),
                Disk = new(),
                Memory = new(),
                Uptime = TimeSpan.Zero
            }
        };

        UpdateTimer = new Timer(async _ =>
        {
            try
            {
                Status = await NodeService.GetStatus(Node);
            }
            catch (Exception)
            {
                Status = new()
                {
                    Containers = 0,
                    Version = "N/A",
                    OperatingSystem = "N/A",
                    Hardware = new()
                    {
                        Cores = new SystemStatus.HardwareInformationData.CpuCoreData[0],
                        Disk = new(),
                        Memory = new(),
                        Uptime = TimeSpan.Zero
                    }
                };
            }

            await InvokeAsync(StateHasChanged);
        }, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));

        return Task.CompletedTask;
    }

    public void Dispose()
    {
        if (UpdateTimer != null)
            UpdateTimer.Dispose();
    }
}