@using MongoDB.Driver
@using Vaitr.Bus

@inherits ComponentBase
@implements IDisposable

@inject IMongoDatabase Database
@inject Bus Bus

@*@if (_customer is not null)
{
    <MudNavLink Icon="@Icons.Material.Outlined.Business" Href="@Navigation.Management.Customers.DetailsHref(CustomerEntity.DocumentId.ToString())" Class="pl-2">
        @_customer.Name
    </MudNavLink>
}*@

@if (_host is not null)
{
    <MudNavLink Icon="@Icons.Material.Outlined.Devices" IconColor="Color.Inherit" Href=@Navigation.Management.Hosts.DetailsHref(_id) Match="NavLinkMatch.All" Class="pl-3">
        @_host.Name
    </MudNavLink>
}

<div class="mt-3">
    <MudNavGroup Title="Actions">
        <MudNavLink Href=@Navigation.Management.Hosts.Actions.Console.IndexHref(_id) Match="NavLinkMatch.Prefix">Console</MudNavLink>
    </MudNavGroup>
</div>

<div class="mt-1">
    <MudNavGroup Title="Inventory">
        <MudNavGroup Title="System">
            <MudNavLink Href=@Navigation.Management.Hosts.Systems.Os.DetailsHref(_id) Match="NavLinkMatch.Prefix">Os</MudNavLink>
            <MudNavGroup Title="Updates">
                <MudNavLink Href=@Navigation.Management.Hosts.Systems.Updates.Installed.IndexHref(_id) Match="NavLinkMatch.Prefix">Installed</MudNavLink>
                <MudNavLink Href=@Navigation.Management.Hosts.Systems.Updates.Pending.IndexHref(_id) Match="NavLinkMatch.Prefix">Pending</MudNavLink>
            </MudNavGroup>
            <MudNavLink Href=@Navigation.Management.Hosts.Systems.Sessions.IndexHref(_id) Match="NavLinkMatch.Prefix">Sessions</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Systems.Software.IndexHref(_id) Match="NavLinkMatch.Prefix">Software</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Systems.Services.IndexHref(_id) Match="NavLinkMatch.Prefix">Services</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Systems.Printers.IndexHref(_id) Match="NavLinkMatch.Prefix">Printers</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Systems.Volumes.IndexHref(_id) Match="NavLinkMatch.Prefix">Volumes</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Systems.Users.IndexHref(_id) Match="NavLinkMatch.Prefix">Users</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Systems.Groups.IndexHref(_id) Match="NavLinkMatch.Prefix">Groups</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Systems.StoragePools.IndexHref(_id) Match="NavLinkMatch.Prefix">Storage Pools</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Systems.VirtualMaschines.IndexHref(_id) Match="NavLinkMatch.Prefix">Virtual Maschines</MudNavLink>
        </MudNavGroup>
        <MudNavGroup Title="Network">
            <MudNavLink Href=@Navigation.Management.Hosts.Network.Interfaces.IndexHref(_id) Match="NavLinkMatch.Prefix">Interfaces</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Network.Addresses.IndexHref(_id) Match="NavLinkMatch.Prefix">Addresses</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Network.Nameservers.IndexHref(_id) Match="NavLinkMatch.Prefix">Nameservers</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Network.Gateways.IndexHref(_id) Match="NavLinkMatch.Prefix">Gateways</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Network.Routes.IndexHref(_id) Match="NavLinkMatch.Prefix">Routes</MudNavLink>
        </MudNavGroup>
        <MudNavGroup Title="Hardware">
            <MudNavLink Href=@Navigation.Management.Hosts.Hardware.Mainboard.DetailsHref(_id) Match="NavLinkMatch.Prefix">Mainboard</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Hardware.Processors.DetailsHref(_id) Match="NavLinkMatch.Prefix">Processors</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Hardware.Memory.IndexHref(_id) Match="NavLinkMatch.Prefix">Memory</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Hardware.Drives.IndexHref(_id) Match="NavLinkMatch.Prefix">Drives</MudNavLink>
            <MudNavLink Href=@Navigation.Management.Hosts.Hardware.Videocards.IndexHref(_id) Match="NavLinkMatch.Prefix">Videocards</MudNavLink>
        </MudNavGroup>
    </MudNavGroup>
</div>

<div class="mt-1">
    <MudNavLink Href=@Navigation.Management.Hosts.LogsHref(_id) Match="NavLinkMatch.Prefix">Logs</MudNavLink>
</div>

@code{
    [CascadingParameter] public IReadOnlyDictionary<string, object>? RouteValues { get; set; }

    private string? _id;
    private CustomerEntity? _customer;
    private HostEntity? _host;
    private IDisposable? _token;
    private bool _disposed;

    protected override void OnInitialized()
    {
        _token = Bus?.Subscribe<string>(OnRefresh, p => p == Events.Layout.Rendered);
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (RouteValues is not null && _id is null)
        {
            if (RouteValues.TryGetValue("hostId", out object? rawId) == false) return;
            _id = rawId?.ToString();

            _host = await Database.Host()
                .Find(p => p.Id == _id)
                .FirstOrDefaultAsync(default);

            if (_host is not null)
            {
                _customer = await Database.Customer()
                    .Find(p => p.Id == _host.Customer)
                    .FirstOrDefaultAsync(default);
            }

            await InvokeAsync(() => StateHasChanged());
        }
    }

    public void OnRefresh(string? message) => InvokeAsync(StateHasChanged);

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (_disposed) return;
        if (disposing is false) return;

        try
        {
            _token?.Dispose();
        }
        finally
        {
            _disposed = true;
        }
    }
}