@inject IJSRuntime jsRuntime;

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

<FormLayoutComponent>
    <FormNumberComponent Max="2048"
                         Min="0"
                         Value="@timingService.GetAttackTime()"
                         OnChange="@OnAttackTimeChanged">
        <FormLabelComponent>Attack Time</FormLabelComponent>
        <FormInfoComponent>
            <i>&emsp; T @Interval.ToTime(timingService.GetAttackTime())</i>
        </FormInfoComponent>
    </FormNumberComponent>

    <FormNumberComponent Max="2048"
                         Min="0"
                         Value="@timingService.GetTravelTime()"
                         OnChange="@OnTravelTimeChanged">
        <FormLabelComponent>Travel Time</FormLabelComponent>
        <FormInfoComponent>
            <i>&emsp; T @Interval.ToTime(timingService.GetTravelTime())</i>
        </FormInfoComponent>
    </FormNumberComponent>

</FormLayoutComponent>

@code {

    void OnAttackTimeChanged(ChangeEventArgs changeEventArgs)
    {
        timingService.SetAttackTime(int.Parse(changeEventArgs.Value!.ToString()!));
        economyService.Calculate(buildOrderService, timingService, buildOrderService.GetLastRequestInterval());
        toastService.AddToast(new ToastModel
        {
            Title = "Attack Time",
            Message = "Attack Time has changed.",
            SeverityType = SeverityType.Success
        });

        StateHasChanged();
    }

    void OnTravelTimeChanged(ChangeEventArgs changeEventArgs)
    {
        timingService.SetTravelTime(int.Parse(changeEventArgs.Value!.ToString()!));
        economyService.Calculate(buildOrderService, timingService, buildOrderService.GetLastRequestInterval());
        toastService.AddToast(new ToastModel
        {
            Title = "Travel Time",
            Message = "Travel Time has changed.",
            SeverityType = SeverityType.Success
        });

        StateHasChanged();
    }

    void OnNameChanged(ChangeEventArgs changeEventArgs)
    {
        buildOrderService.SetName(changeEventArgs.Value!.ToString()!);
    }

    void OnColorChanged(ChangeEventArgs changeEventArgs)
    {
        buildOrderService.DeprecatedSetColor(changeEventArgs.Value!.ToString()!);
    }


    void OnNotesChanged(ChangeEventArgs changeEventArgs)
    {
        buildOrderService.SetNotes(changeEventArgs.Value!.ToString()!);
    }

    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
    }

}