@inject IJSRuntime JsRuntime;

@inject IBuildOrderService BuildOrderService
@inject IEconomyService EconomyService
@inject IToastService ToastService
@inject ITimingService TimingService
@implements IDisposable

<FormLayoutComponent>
    <FormNumberComponent Max="600"
                         Min="0"
                         Value="BuildDelay"
                         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 BuildDelay { get; set; } = 2;
    private int WaitTime { get; set; } = 30;
    private int WaitTo { get; set; } = 30;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        TimingService.Subscribe(RefreshDefaults);

        RefreshDefaults();
    }

    void IDisposable.Dispose()
    {
        TimingService.Unsubscribe(RefreshDefaults);
    }

    void RefreshDefaults()
    {
        BuildDelay = TimingService.BuildingInputDelay;
        WaitTime = TimingService.WaitTime;
        WaitTo = TimingService.WaitTo;

        StateHasChanged();
    }

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

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

    void OnWaitToChanged(ChangeEventArgs changeEventArgs)
    {
        TimingService.WaitTo = (int)changeEventArgs.Value!;
        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
    }

}