@page "/admin/servers/view/{Id:int}/{Route?}"

@using Moonlight.App.Repositories
@using Moonlight.App.Database.Entities
@using Microsoft.EntityFrameworkCore
@using Moonlight.Shared.Components.Navigations

@inject Repository<Server> ServerRepository

<OnlyAdmin>
    <LazyLoader @ref="LazyLoader" Load="Load">
        @if (Server == null)
        {
            <div class="alert alert-danger">
                <TL>No server with this id found</TL>
            </div>
        }
        else
        {
            <CascadingValue TValue="Server" Value="Server">
                <SmartRouter Route="@Route">
                    <Route Path="/">
                        <AdminServersViewNavigation Index="0" Server="Server"/>
                        <Overview/>
                    </Route>
                    <Route Path="/image">
                        <AdminServersViewNavigation Index="1" Server="Server"/>
                        <Image/>
                    </Route>
                    <Route Path="/resources">
                        <AdminServersViewNavigation Index="2" Server="Server"/>
                        <Resources/>
                    </Route>
                    <Route Path="/allocations">
                        <AdminServersViewNavigation Index="3" Server="Server"/>
                        <Allocations />
                    </Route>
                    <Route Path="/archive">
                        <AdminServersViewNavigation Index="4" Server="Server"/>
                        <Archive/>
                    </Route>
                    <Route Path="/debug">
                        <AdminServersViewNavigation Index="5" Server="Server"/>
                        <Debug/>
                    </Route>
                    <Route Path="/delete">
                        <AdminServersViewNavigation Index="6" Server="Server"/>
                    </Route>
                </SmartRouter>
            </CascadingValue>
        }
    </LazyLoader>
</OnlyAdmin>

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

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

    private LazyLoader LazyLoader;
    private Server? Server;

    private Task Load(LazyLoader arg)
    {
        Server = ServerRepository
            .Get()
            .Include(x => x.Image)
            .Include(x => x.Owner)
            .Include(x => x.Archive)
            .Include(x => x.Allocations)
            .Include(x => x.MainAllocation)
            .Include(x => x.Variables)
            .Include(x => x.Node)
            .FirstOrDefault(x => x.Id == Id);

        return Task.CompletedTask;
    }
}