@using Moonlight.Shared.Components.ErrorBoundaries
@using Moonlight.App.Database.Entities
@using Moonlight.App.Extensions
@using Moonlight.App.Services.Sessions

@layout ThemeInit
@implements IDisposable
@inherits LayoutComponentBase

@inject IJSRuntime JsRuntime
@inject IdentityService IdentityService
@inject SessionService SessionService
@inject NavigationManager NavigationManager

<GlobalErrorBoundary>
    @{
        var uri = new Uri(NavigationManager.Uri);
        var pathParts = uri.LocalPath.Split("/");

        var title = "";

        foreach (var pathPart in pathParts)
        {
            if (!string.IsNullOrEmpty(pathPart))
                title += $"{pathPart.FirstCharToUpper()} ";
        }
    }

    <PageTitle>@(string.IsNullOrEmpty(title) ? "Dashboard " : title)- Moonlight</PageTitle>

    <div class="d-flex flex-column flex-root app-root" id="kt_app_root">
        <div class="app-page flex-column flex-column-fluid" id="kt_app_page">
            <canvas id="snow" class="snow-canvas"></canvas>

            @{
                //TODO: Add a way to disable the snow
            }

            <PageHeader></PageHeader>
            <div class="app-wrapper flex-column flex-row-fluid" id="kt_app_wrapper">
                <Sidebar></Sidebar>
                <div class="app-main flex-column flex-row-fluid" id="kt_app_main">
                    <div class="d-flex flex-column flex-column-fluid">
                        <div id="kt_app_content" class="app-content flex-column-fluid">
                            <div id="kt_app_content_container" class="app-container container-fluid">
                                <div class="mt-10">
                                    <PageErrorBoundary>
                                        @Body
                                    </PageErrorBoundary>
                                </div>
                            </div>
                        </div>
                    </div>
                    <Footer></Footer>
                </div>
            </div>
        </div>
    </div>
</GlobalErrorBoundary>

@code
{
    private User? User;

    protected override void OnInitialized()
    {
        AddBodyAttribute("data-kt-app-page-loading", "on");
    }

    protected override void OnAfterRender(bool firstRender)
    {
    //Initialize classes and attributes for layout with dark sidebar
        AddBodyAttribute("data-kt-app-reset-transition", "true");

        AddBodyAttribute("data-kt-app-layout", "dark-sidebar");
        AddBodyAttribute("data-kt-app-header-fixed", "true");
        AddBodyAttribute("data-kt-app-sidebar-fixed", "true");
        AddBodyAttribute("data-kt-app-sidebar-hoverable", "true");
        AddBodyAttribute("data-kt-app-sidebar-push-header", "true");
        AddBodyAttribute("data-kt-app-sidebar-push-toolbar", "true");
        AddBodyAttribute("data-kt-app-sidebar-push-footer", "true");
        AddBodyAttribute("data-kt-app-toolbar-enabled", "true");

        AddBodyClass("app-default");

        JsRuntime.InvokeVoidAsync("KTModalUpgradePlan.init");
        JsRuntime.InvokeVoidAsync("KTCreateApp.init");
        JsRuntime.InvokeVoidAsync("KTModalUserSearch.init");
        JsRuntime.InvokeVoidAsync("KTModalNewTarget.init");
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                User = await IdentityService.Get();

                await InvokeAsync(StateHasChanged);
                await Task.Delay(300);

                await JsRuntime.InvokeVoidAsync("document.body.removeAttribute", "data-kt-app-reset-transition");
                await JsRuntime.InvokeVoidAsync("document.body.removeAttribute", "data-kt-app-page-loading");
                await JsRuntime.InvokeVoidAsync("KTMenu.createInstances");
                await JsRuntime.InvokeVoidAsync("KTDrawer.createInstances");
                await JsRuntime.InvokeVoidAsync("createSnow");

                await SessionService.Register();

                NavigationManager.LocationChanged += (sender, args) => { SessionService.Refresh(); };
            }
            catch (Exception)
            {
    // ignored
            }
        }
    }

    public void Dispose()
    {
        SessionService.Close();
    }

    private void AddBodyAttribute(string attribute, string value)
    {
        JsRuntime.InvokeVoidAsync("document.body.setAttribute", attribute, value);
    }

    private void AddBodyClass(string className)
    {
        JsRuntime.InvokeVoidAsync("document.body.classList.add", className);
    }
}