@inject IJSRuntime jsRuntime;
@inject IKeyService keyService
@inject IImmortalSelectionService filterService
@inject IBuildOrderService buildOrderService

@implements IDisposable

@if (entity != null)
{
    <div class="entityClickView">
        <CascadingValue Value="entity">
            <CascadingValue Value="@viewType">
                <EntityViewComponent></EntityViewComponent>
            </CascadingValue>
        </CascadingValue>
    </div>
}
<style>
    .entityClickView {
    overflow-y: scroll; width: 100%; overflow-x: hidden; height: 550px;
    }
</style>

@code {
    private EntityModel? entity = default!;
    private string viewType = "Detailed";

    protected override void OnInitialized()
    {
        keyService.Subscribe(HandleClick);
    }

    void IDisposable.Dispose()
    {
        keyService.Unsubscribe(HandleClick);
    }

    protected override bool ShouldRender()
    {
#if DEBUG
        jsRuntime.InvokeVoidAsync("console.time", "EntityClickViewComponent");
#endif
        return true;
    }

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

    private void HandleClick()
    {
        var hotkey = keyService.GetHotkey();
        var hotkeyGroup = keyService.GetHotkeyGroup();
        var isHoldSpace = keyService.IsHoldingSpace();
        var faction = filterService.GetFactionType();
        var immortal = filterService.GetImmortalType();

        var foundEntity = EntityModel.GetFrom(hotkey!, hotkeyGroup, isHoldSpace, faction, immortal);

        if (foundEntity != null && entity != foundEntity)
        {
            entity = foundEntity;
            StateHasChanged();
        }
    }

}