@page "/webspace/{Id:int}/{Route?}"
@using Moonlight.App.Database.Entities
@using Moonlight.App.Repositories
@using Moonlight.App.Services
@using Moonlight.Shared.Components.WebsiteControl
@using Microsoft.EntityFrameworkCore
@using Moonlight.App.Helpers
@using Moonlight.App.Plugin.UI.Webspaces
@using Moonlight.App.Services.Plugins
@using Moonlight.App.Services.Sessions

@inject Repository<WebSpace> WebSpaceRepository
@inject WebSpaceService WebSpaceService
@inject PluginService PluginService
@inject IdentityService IdentityService

<LazyLoader Load="Load">
    @if (CurrentWebspace == null)
    {
        <div class="d-flex justify-content-center flex-center">
            <div class="card">
                <img src="/assets/media/svg/nodata.svg" class="card-img-top w-50 mx-auto pt-5" alt="Not found image"/>
                <div class="card-body text-center">
                    <h1 class="card-title">
                        <TL>Webspace not found</TL>
                    </h1>
                    <p class="card-text fs-4">
                        <TL>A webspace with that id cannot be found or you have no access for this webspace</TL>
                    </p>
                </div>
            </div>
        </div>
    }
    else
    {
        if (HostOnline)
        {
            <CascadingValue Value="CurrentWebspace">
                <CascadingValue Value="Context">
                    <SmartRouter Route="@(Route)">
                        @foreach (var tab in Context.Tabs)
                        {
                            <Route Path="@(tab.Route)">
                                <WebSpaceNavigation Route="@(tab.Route)" WebSpace="CurrentWebspace"/>
                                @(tab.Component)
                            </Route>
                        }
                    </SmartRouter>
                </CascadingValue>
            </CascadingValue>
        }
        else
        {
            <div class="d-flex justify-content-center flex-center">
                <div class="card">
                    <img src="/assets/media/svg/serverdown.svg" class="card-img-top w-50 mx-auto pt-5" alt="Not found image"/>
                    <div class="card-body text-center">
                        <h1 class="card-title">
                            <TL>Host system offline</TL>
                        </h1>
                        <p class="card-text fs-4">
                            <TL>The host system the website is running on is currently offline</TL>
                        </p>
                    </div>
                </div>
            </div>
        }
    }
</LazyLoader>

@code
{
    [Parameter]
    public int Id { get; set; }

    [Parameter]
    public string? Route { get; set; }

    private WebSpace? CurrentWebspace;
    private bool HostOnline = false;

    private WebspacePageContext Context;

    private async Task Load(LazyLoader lazyLoader)
    {
        CurrentWebspace = WebSpaceRepository
            .Get()
            .Include(x => x.CloudPanel)
            .Include(x => x.Owner)
            .FirstOrDefault(x => x.Id == Id);

        if (CurrentWebspace != null)
        {
            if (CurrentWebspace.Owner.Id != IdentityService.User.Id && !IdentityService.User.Admin)
            {
                CurrentWebspace = null;
                return;
            }

            await lazyLoader.SetText("Checking host system online status");

            HostOnline = await WebSpaceService.IsHostUp(CurrentWebspace);

            if (!HostOnline)
                return;

            Context = new WebspacePageContext()
            {
                WebSpace = CurrentWebspace,
                User = IdentityService.User
            };

            Context.Tabs.Add(new()
            {
                Route = "/",
                Name = "Dashboard",
                Component = ComponentHelper.FromType(typeof(WebSpaceDashboard))
            });

            Context.Tabs.Add(new()
            {
                Route = "/files",
                Name = "Files",
                Component = ComponentHelper.FromType(typeof(WebSpaceFiles))
            });

            Context.Tabs.Add(new()
            {
                Route = "/sftp",
                Name = "SFTP",
                Component = ComponentHelper.FromType(typeof(WebSpaceSftp))
            });

            Context.Tabs.Add(new()
            {
                Route = "/databases",
                Name = "Databases",
                Component = ComponentHelper.FromType(typeof(WebSpaceDatabases))
            });

            Context = await PluginService.BuildWebspacePage(Context);
        }
    }
}