@inject IJSRuntime jsRuntime;

@inject IBuildOrderService buildOrderService
@inject IEconomyService economyService
@inject IToastService toastService
@inject ITimingService timingService

<FormLayoutComponent>
    <FormNumberComponent Max="600"
                         Min="0"
                         Value="@buildOrderService.BuildingInputDelay"
                         OnChange="@OnBuildingInputDelayChanged">
        <FormLabelComponent>Building Input Delay</FormLabelComponent>
        <FormInfoComponent>Add a input delay to constructing buildings for simulating worker movement and player micro.</FormInfoComponent>
    </FormNumberComponent>
    <div class="optionRow">
        <FormLayoutComponent>
            <FormNumberComponent Max="600"
                                 Min="1"
                                 Value="@WaitTime"
                                 OnChange="@OnWaitTimeChanged">
                <FormLabelComponent>Wait Time</FormLabelComponent>
            </FormNumberComponent>
            <ButtonComponent OnClick="OnWaitClicked">Add Wait</ButtonComponent>
        </FormLayoutComponent>
        <FormLayoutComponent>
            <FormNumberComponent Max="2048"
                                 Min="1"
                                 Value="@WaitTo"
                                 OnChange="@OnWaitToChanged">
                <FormLabelComponent>Wait To</FormLabelComponent>
            </FormNumberComponent>
            <ButtonComponent OnClick="OnWaitToClicked">Add Wait</ButtonComponent>
        </FormLayoutComponent>
    </div>
</FormLayoutComponent>

<style>
    .optionRow {
        display: flex;
        gap: 12px;
    }
</style>

@code {
    private int WaitTime { get; set; } = 30;
    private int WaitTo { get; set; } = 30;

    
    void OnBuildingInputDelayChanged(ChangeEventArgs changeEventArgs)
    {
        buildOrderService.BuildingInputDelay = int.Parse(changeEventArgs.Value!.ToString()!);
    }

    void OnWaitTimeChanged(ChangeEventArgs changeEventArgs)
    {
        WaitTime = (int)changeEventArgs.Value!;
    }
    
    void OnWaitToChanged(ChangeEventArgs changeEventArgs)
    {
        WaitTo = (int)changeEventArgs.Value!;
    }

    private void OnWaitClicked()
    {
        if (buildOrderService.AddWait(WaitTime))
        {
            economyService.Calculate(buildOrderService, timingService, buildOrderService.GetLastRequestInterval());
        }
    }

    private void OnWaitToClicked()
    {
        if (buildOrderService.AddWaitTo(WaitTo))
        {
            economyService.Calculate(buildOrderService, timingService, buildOrderService.GetLastRequestInterval());
        }
    }

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

        return true;
    }

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


}