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

<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;

    Dictionary<string, double[]> Data = new Dictionary<string, double[]>();

    ConcurrentDictionary<string, PerformanceCounter> PerformanceCounters = new ConcurrentDictionary<string, PerformanceCounter>();

    string JsConfig = @"{
        meta: {
            value: {
                alias: 'Speed',
                formatter: (v) => humanFileSize(v, true) + '/s'
            }
        }
    }";

    AreaConfig Config = new AreaConfig
    {
        Name = "Network Upload Rate",
        Padding = "auto",
        SeriesField = "series",
        YField = "value",
        XField = "index",
        Animation = false,
        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
        var category = new PerformanceCounterCategory("Network Interface");

        foreach (var instance in category.GetInstanceNames())
        {
            if (!Data.ContainsKey(instance))
                Data[instance] = new double[TimerHistory];

            if (!PerformanceCounters.ContainsKey(instance))
                PerformanceCounters[instance] = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);

            Data[instance] = Data[instance].ShiftArrayAndInsert((double)PerformanceCounters[instance].NextValue(), TimerHistory);
        }

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