@inherits LayoutComponentBase
@inject ISearchService SearchService
@inject IDataCollectionService DataCollectionService
@inject NavigationManager NavigationManager
@using Model.Website.Data
@using Services.Website
@implements IDisposable

<MudThemeProvider @ref="@_mudThemeProvider" @bind-IsDarkMode="@_isDarkMode"/>
<MudPopoverProvider/>
<MudDialogProvider/>
<MudSnackbarProvider/>

<MudLayout>
    <MudAppBar Elevation="1">
        <MudHidden Breakpoint="Breakpoint.SmAndDown" Invert="true">
            <MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start"
                           OnClick="@(e => DrawerToggle())"/>
        </MudHidden>
        <MudButton Class="ml-3 mr-8"
                   Href="/"
                   Target="_self"
                   Variant="Variant.Text"
                   Color="Color.Default">
            <MudText Typo="Typo.h5"> IGP Fan Reference</MudText>
        </MudButton>
        <MudHidden Breakpoint="Breakpoint.MdAndUp" Invert="true">
            @foreach (var page in WebsiteData.GetPages())
            {
                <MudButton Href="@(page.Href)"
                           Variant="Variant.Text"
                           Color="Color.Default"
                           Class="mr-4">
                    <MudIcon Icon="@(page.Icon)" Class="mr-2"/>
                    @(page.Name)
                </MudButton>
            }
        </MudHidden>
        <MudSpacer/>
        <SearchButtonComponent Id="desktop-searchButton"/>
    </MudAppBar>
    <MudHidden Breakpoint="Breakpoint.SmAndDown" Invert="true">
        <MudDrawer @bind-Open="_drawerOpen" ClipMode="DrawerClipMode.Always" Elevation="2">
            <MudPaper Width="250px" Class="d-inline-flex py-3" Elevation="0">
                <MudNavMenu Class="mud-width-full flex-grow-1">
                    @foreach (var page in WebsiteData.GetPages())
                    {
                        <MudNavLink Href="@(page.Href)" Icon="@(page.Icon)">@(page.Name)</MudNavLink>
                    }
                </MudNavMenu>
            </MudPaper>
        </MudDrawer>
    </MudHidden>

    <MudMainContent>
        <MudContainer Class="px-8" MaxWidth="MaxWidth.False">
            <div id="content" class="content">
                @Body
            </div>
        </MudContainer>
    </MudMainContent>
</MudLayout>

<FooterComponent></FooterComponent>



@code {

    private bool _isDarkMode = true;
    private MudThemeProvider _mudThemeProvider;

    bool _drawerOpen = true;

    void DrawerToggle()
    {
        _drawerOpen = !_drawerOpen;
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();

        CollectFirstPageLoaded();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            //TODO: Support light mode
            //_isDarkMode = await _mudThemeProvider.GetSystemPreference();
            //StateHasChanged();
        }
    }

    private void CollectFirstPageLoaded()
    {
        var skipBaseUri = NavigationManager.Uri.Substring(NavigationManager.BaseUri.Length,
            NavigationManager.Uri.Length - NavigationManager.BaseUri.Length);
        var rootUrl = skipBaseUri.Split("/").First();
        if (rootUrl.Trim().Equals(""))
        {
            rootUrl = "home";
        }

        DataCollectionService.SendEvent(DataCollectionKeys.FirstPage,
            new Dictionary<string, string> { { "page", rootUrl } });
    }

    protected override async Task OnInitializedAsync()
    {
        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()
    {
    }

    void HasChanged()
    {
        StateHasChanged();
    }

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

}