@inherits LayoutComponentBase;
@inject IJSRuntime jsRuntime

@inject ISearchService searchService
@inject IWebsiteService webService;
@implements IDisposable;

<div class="pageContents">
    <div class="layoutContainer">
        @if (!webService.IsLoaded())
        {
            <LoadingComponent/>
        }
        else
        {
            <div class="content">
                @Body
            </div>


            <DesktopNavComponent WebSections=webService.WebSectionModels
                                 WebPages=webService.WebPageModels/>
            <TabletNavComponent WebSections=webService.WebSectionModels
                                WebPages=webService.WebPageModels/>
            <MobileNavComponent WebSections=webService.WebSectionModels
                                WebPages=webService.WebPageModels/>
        }
    </div>

</div>

@code {

    protected override void OnInitialized()
    {
        webService.Subscribe(HasChanged);
    }

    protected override async Task OnInitializedAsync()
    {
        await webService.Load();

        await Focus();
    }

    private async Task Focus()
    {
    //   await jsRuntime.InvokeVoidAsync("SetFocusToElement", pageContents);        
    }

    protected override async void OnAfterRender(bool firstRender)
    {
    // await jsRuntime.InvokeVoidAsync("SetFocusToElement", pageContents);
    }

    void IDisposable.Dispose()
    {
        webService.Unsubscribe(HasChanged);
    }

    void HasChanged()
    {
        StateHasChanged();
    }

    private void HandleKeyDown(KeyboardEventArgs keyboardEventArgs)
    {
        if ((keyboardEventArgs.CtrlKey || keyboardEventArgs.MetaKey) && keyboardEventArgs.Key.ToLower() == "k")
        {
            searchService.Show();
        }
    }


}