@inherits LayoutComponentBase
@implements IDisposable
@inject GlobalLoadingService GlobalLoadingService

<CascadingAuthenticationState>
    <AuthorizeView>
        <Authorized>
            <ConfirmModal />
            <FullScreenLoadingIndicator @ref="LoadingIndicator" />
            <ClipboardCountdownBar />
            <TopMenu />
            <div class="flex pt-16 mb-4 lg:mb-16 overflow-x-hidden bg-gray-100 dark:bg-gray-900 relative">
                <div id="main-content" class="relative w-full max-w-screen-2xl mx-auto h-full overflow-y-auto bg-gray-100 dark:bg-gray-900 min-h-[300px]">
                    <main>
                        <GlobalNotificationDisplay />
                        @Body
                    </main>
                </div>
            </div>
            <Footer />
        </Authorized>
        <NotAuthorized>
            <main>
                @Body
            </main>
        </NotAuthorized>
    </AuthorizeView>
</CascadingAuthenticationState>

@code {
    [CascadingParameter]
    private Task<AuthenticationState>? AuthState { get; set; }

    private FullScreenLoadingIndicator LoadingIndicator = new();

    /// <inheritdoc />
    protected override void OnInitialized()
    {
        GlobalLoadingService.OnChange += OnChange;
    }

    /// <inheritdoc />
    public void Dispose()
    {
        GlobalLoadingService.OnChange -= OnChange;
    }

    private void OnChange()
    {
        if (GlobalLoadingService.IsLoading)
        {
            LoadingIndicator.Show(GlobalLoadingService.LoadingMessage);
        }
        else
        {
            LoadingIndicator.Hide();
        }
        StateHasChanged();
    }
}
