@using System.Diagnostics;
@using LANCommander.Extensions;
@using AntDesign.Charts;

<Spin Spinning="Loading">
    <Area @ref="Chart" Config="Config" />
</Spin>

@code {
    [Parameter] public int TimerHistory { get; set; }
    [Parameter] public int TimerInterval { get; set; }
    IChartComponent? Chart;
    System.Timers.Timer Timer;
    bool Loading = true;

    double[] Data;

    PerformanceCounter PerformanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");

    string JsConfig = @"{
        meta: {
            value: {
                alias: '% Usage',
                formatter: (v) => v + '%'
            }
        }
    }";

    AreaConfig Config = new AreaConfig
    {
        Name = "Processor Utilization",
        Padding = "auto",
        YField = "value",
        XField = "index",
        Animation = false,
        IsPercent = true,
        YAxis = new ValueAxis
        {
            Min = 0,
            Max = 100
        },
        XAxis = new ValueCatTimeAxis
        {
            Visible = false
        }
    };

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            if (Timer == null)
            {
                Timer = new System.Timers.Timer();

                Timer.Interval = TimerInterval;

                Timer.Elapsed += async (s, e) =>
                {
                    await RefreshData();
                };
            }

            await Chart.UpdateChart(Config, null, null, JsConfig);
            Timer.Start();

            Loading = false;

            StateHasChanged();
        }
    }

    private async Task RefreshData()
    {
        #if WINDOWS
        Data = Data.ShiftArrayAndInsert((double)Math.Ceiling(PerformanceCounter.NextValue()), TimerHistory);

        try
        {
            await Chart.ChangeData(Data.Select((x, i) => new { value = x, index = i }), true);
        }
        catch { }
        #endif
    }
}
