@inject IJSRuntime jsRuntime
@inject IEconomyService economyService
@inject IBuildOrderService buildOrderService

@implements IDisposable

<Virtualize Items="@economyService.GetOverTime()" Context="economyAtSecond" ItemSize="400" OverscanCount="4">
    <div style="display: grid; gap: 8px; grid-template-columns: 1fr 1fr;">
        <div>
            <div>
                @economyAtSecond.Interval
            </div>
            <div>
                T @Interval.ToTime(economyAtSecond.Interval) | A @economyAtSecond.Alloy | E @economyAtSecond.Ether
            </div>
            <div>
                Worker Count: @(economyAtSecond.WorkerCount)
            </div>
            <div>
                Free Worker Count: @(economyAtSecond.WorkerCount - economyAtSecond.BusyWorkerCount)
            </div>
            <div>
                Busy Worker Count: @economyAtSecond.BusyWorkerCount
            </div>
            <div>
                Creating Worker Count: @economyAtSecond.CreatingWorkerCount
            </div>
            <br/>
        </div>
        <div>

            @if (buildOrderService.StartedOrders.TryGetValue(economyAtSecond.Interval, out var ordersAtTime))
            {
                @foreach (var order in ordersAtTime)
                {
                    <div>
                        Requested: @order.Info().Name
                    </div>
                }
            }


            @if (buildOrderService.CompletedOrders.TryGetValue(economyAtSecond.Interval, out var ordersCompletedAtTime))
            {
                @foreach (var order in ordersCompletedAtTime)
                {
                    <div>
                        New: @order.Info().Name
                    </div>
                }
            }
        </div>
    </div>
</Virtualize>


@code {

    protected override void OnInitialized()
    {
        base.OnInitialized();
        economyService.Subscribe(StateHasChanged);
        buildOrderService.Subscribe(StateHasChanged);
    }

    void IDisposable.Dispose()
    {
        economyService.Unsubscribe(StateHasChanged);
        buildOrderService.Unsubscribe(StateHasChanged);
    }

    protected override bool ShouldRender()
    {
#if DEBUG
        jsRuntime.InvokeVoidAsync("console.time", "TimelineComponent");
#endif

        return true;
    }

    protected override void OnAfterRender(bool firstRender)
    {
#if DEBUG
        jsRuntime.InvokeVoidAsync("console.timeEnd", "TimelineComponent");
#endif
    }

}