@inject IJSRuntime jsRuntime;

@inject IEconomyService economyService


@implements IDisposable

<div class="bankContainer">
    <FormDisplayComponent Label="Time">
        <Display>@BuildOrderService.GetLastRequestInterval() | T @Interval.ToTime(BuildOrderService.GetLastRequestInterval())</Display>
    </FormDisplayComponent>
    <div class="bankRow">
        <FormDisplayComponent Label="Alloy">
            <Display>@_economy.Alloy</Display>
        </FormDisplayComponent>
        <FormDisplayComponent Label="Ether">
            <Display>@_economy.Ether</Display>
        </FormDisplayComponent>
    </div>
    <div class="bankRow">
        <FormDisplayComponent Label="Pyre">
            <Display>@_economy.Pyre</Display>
        </FormDisplayComponent>
        <FormDisplayComponent Label="Supply">
            <Display>@_supplyTaken / @_supplyGranted (@(_supplyGranted / 16)@(_extraBuildings > 0 ? "+" + _extraBuildings : ""))</Display>
        </FormDisplayComponent>
    </div>

    <div>
        <div class="workerText">Workers</div>
        <div class="bankRow">
            <FormDisplayComponent Label="Current">
                <Display>@_economy.WorkerCount</Display>
            </FormDisplayComponent>
            <FormDisplayComponent Label="Busy">
                <Display>@_economy.BusyWorkerCount</Display>
            </FormDisplayComponent>
            <FormDisplayComponent Label="Creating">
                <Display>@_economy.CreatingWorkerCount</Display>
            </FormDisplayComponent>
        </div>
    </div>
</div>

<style>
    .bankContainer {
        display: flex;
        flex-direction: column;
        gap: 5px;
    }
    
    .workerText {
        margin-bottom: -2px;
        font-size: 0.8em;
    }

    .bankRow {
        display: flex;
        gap: 8px;
    }
</style>

@code {

    [Inject]
    IBuildOrderService BuildOrderService { get; set; } = default!;

    [Inject]
    IEconomyService EconomyService { get; set; } = default!;

    EconomyModel _economy = new();
    int _supplyGranted;
    int _supplyTaken;
    int _extraBuildings;

    protected override void OnInitialized()
    {
        BuildOrderService.Subscribe(OnBuildOrderChanged);
    }

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

        return true;
    }

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

    void IDisposable.Dispose()
    {
        BuildOrderService.Unsubscribe(OnBuildOrderChanged);
    }

    void OnBuildOrderChanged()
    {
        _economy = EconomyService.GetEconomy(BuildOrderService.GetLastRequestInterval());

        var ordersOverTime = BuildOrderService.GetOrders();

        _supplyTaken = (from ordersAtInterval in ordersOverTime
            from order in ordersAtInterval.Value
            where order.Supply() != null
            where order.Supply().Takes > 0
            select order.Supply().Takes).Sum();

        _supplyGranted = (from ordersAtInterval in ordersOverTime
            from order in ordersAtInterval.Value
            where order.Supply() != null
            where order.Supply().Grants > 0
            select order.Supply().Grants).Sum();

        _extraBuildings = 0;
        if (_supplyGranted > 160)
        {
            _extraBuildings = (_supplyGranted - 160) / 16;
            _supplyGranted = 160;
        }


        StateHasChanged();
    }

}