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

@using Moonlight.Features.Servers.Entities
@using Moonlight.Features.Servers.UI.Components
@using MoonCore.Abstractions
@using Microsoft.EntityFrameworkCore
@using Moonlight.Features.Servers.UI.NodeComponents

@inject Repository<ServerNode> NodeRepository

@attribute [RequirePermission(5001)]

<LazyLoader Load="Load">
    @if (Node == null)
    {
        <NotFoundAlert/>
    }
    else
    {
        <AdminNodeViewNavigation Index="@GetIndex()" NodeId="@Id"/>

        <SmartRouter Route="@Route">
            <Route Path="/">
                <NodeOverview Node="Node"/>
            </Route>
            <Route Path="/allocations">
                <NodeAllocations Node="Node"/>
            </Route>
            <Route Path="/setup">
                <NodeSetup Node="Node"/>
            </Route>
        </SmartRouter>
    }
</LazyLoader>

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

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

    private ServerNode? Node;

    private async Task Load(LazyLoader lazyLoader)
    {
        await lazyLoader.SetText("Loading node");

        Node = NodeRepository
            .Get()
            .Include(x => x.Allocations)
            .FirstOrDefault(x => x.Id == Id);
    }

    private int GetIndex()
    {
        if (string.IsNullOrEmpty(Route))
            return 0;

        var route = "/" + (Route ?? "");

        switch (route)
        {
            case "/allocations":
                return 1;

            case "/setup":
                return 2;

            default:
                return 0;
        }
    }
}