@using Rms.Service.Bootstrap.Security

@inherits LayoutComponentBase
@implements IDisposable

@inject IJSRuntime          JS
@inject LogoutHandler       Handler
@inject IConnectedUserList  ConnectedUserList
@inject NavigationManager   NavigationManager
@inject UserService         UserService
@inject IConnectedUser      ConnectedUser
@inject ILogger<MainLayout> Log

@*
 *                                dbMango
 *
 * Copyright 2025 Deutsche Bank AG
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *@

<div style="height:100vh">
    <NavMenu />

    <div class="main">
        <div class="content px-4">
            @Body
        </div>
    </div>
</div>

@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender )
            await Handler.OnAfterRenderAsync(JS);
        await base.OnAfterRenderAsync(firstRender);
    }

    protected override void OnInitialized()
    {
        NavigationManager.LocationChanged += OnLocationChanged;
        var userName = UserService.GetEmail();
        ConnectedUser.Name = userName;
        ConnectedUserList.Add(ConnectedUser);
    }

    private void OnLocationChanged(object? sender, LocationChangedEventArgs args)
    {
        try
        {
            Log.LogDebug($"Navigation: User=\"{ConnectedUser.Name}\" Url=\"{args.Location}\"");
        }
        catch (Exception e)
        {
            Log.LogError(e.Message, e);
        }
    }

    public void Dispose()
    {
        NavigationManager.LocationChanged -= OnLocationChanged;
        ConnectedUserList.Remove(ConnectedUser);
    }

}

