@using Moonlight.Core.Services
@using Moonlight.Core.UI.Components.Auth
@using MoonCore.Services
@using Moonlight.Core.Configuration
@using Moonlight.Core.Events

@inherits LayoutComponentBase

@inject IdentityService IdentityService
@inject NavigationManager Navigation
@inject IServiceProvider ServiceProvider
@inject FeatureService FeatureService
@inject UnloadService UnloadService
@inject ConfigService<CoreConfiguration> ConfigService
@inject ScopedStorageService ScopedStorageService

@implements IDisposable

@{
    var url = new Uri(Navigation.Uri);
}

<div class="d-flex flex-column flex-root app-root">
    <div class="app-page flex-column flex-column-fluid">
        <AppHeader/>

        <div class="app-wrapper flex-column flex-row-fluid">
            @if (IdentityService.IsLoggedIn)
            {
                <AppSidebar/>
            }

            <div class="app-main flex-column flex-row-fluid ">
                <div class="d-flex flex-column flex-column-fluid">
                    <div class="app-content flex-column-fluid ">
                        <div class="app-container container-fluid">
                            <div class="row">
                                <div class="col-md-1 col-12"></div>

                                <div class="col-md-10 col-12">
                                    <SoftErrorHandler>
                                        @if (IsRestarting)
                                        {
                                            <IconAlert Title="Moonlight is restarting" Color="primary" Icon="bx-refresh">
                                                Moonlight is restarting and will be available again soon. Please be patient
                                                <div class="text-center mt-3">
                                                    <a href="javascript:location.reload()" class="btn btn-primary">Reconnect</a>
                                                </div>
                                            </IconAlert>
                                        }
                                        else
                                        {
                                            if (IsInitialized)
                                            {
                                                if (IdentityService.IsLoggedIn)
                                                {
                                                    <PermissionChecker>
                                                        @Body
                                                    </PermissionChecker>
                                                }
                                                else
                                                {
                                                    if (url.LocalPath == "/register")
                                                    {
                                                        <Register/>
                                                    }
                                                    else
                                                    {
                                                        <Login/>
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                <div class="d-flex justify-content-center">
                                                    <div class="d-flex align-items-center">
                                                        <div class="card card-body m-15 p-15">
                                                            <LazyLoader Load="Load">
                                                                <span></span>
                                                            </LazyLoader>
                                                        </div>
                                                    </div>
                                                </div>
                                            }
                                        }
                                    </SoftErrorHandler>
                                </div>

                                <div class="col-md-1 col-12"></div>
                            </div>
                        </div>
                    </div>
                </div>

                <AppFooter/>
            </div>
        </div>
    </div>
</div>

@code
{
    private bool IsInitialized = false;
    private bool IsUnloaded = false;
    private bool IsRestarting = false;

    private async Task Load(LazyLoader lazyLoader)
    {
        // Base init
        await lazyLoader.SetText("Initializing");
        
        IdentityService.OnAuthenticationStateChanged += OnAuthenticationStateChanged;
        
        CoreEvents.OnMoonlightRestart += async () =>
        {
            IsRestarting = true;
            await InvokeAsync(StateHasChanged);
        };
        
        UnloadService.OnUnloaded += OnUnloaded;
        await UnloadService.Initialize();

        // Feature hook
        await FeatureService.SessionInit(ServiceProvider, lazyLoader);

        // Complete
        IsInitialized = true;
        await InvokeAsync(StateHasChanged);
    }
    
    private async Task OnAuthenticationStateChanged()
    {
        await InvokeAsync(StateHasChanged);
    }

    private async Task OnUnloaded()
    {
        if(ConfigService.Get().Security.DisableClientSideUnload)
            return;
        
        if(IsUnloaded)
            return;

        IsUnloaded = true;
        await FeatureService.SessionDispose(ScopedStorageService);
    }

    public async void Dispose()
    {
        if(IsUnloaded)
            return;

        IsUnloaded = true;
        await FeatureService.SessionDispose(ScopedStorageService);
    }
}