@using MongoDB.Driver
@using Vaitr.Bus

@inherits ComponentBase
@implements IDisposable

@inject NavigationManager NavigationManager
@inject IMongoDatabase Database
@inject Bus Bus

<MudNavGroup Title="Monitoring" Expanded="ExpandMonitoring">
    @*<MudNavLink Href=@Navigation.Monitoring.Index Match="NavLinkMatch.Prefix">
        Dashboard
    </MudNavLink>*@
    <MudNavLink Href=@Navigation.Monitoring.Maintenance.Index Match="NavLinkMatch.Prefix">
        Maintenance
    </MudNavLink>
    @*<MudNavLink Href=@Navigation.Monitoring.Problems Match="NavLinkMatch.Prefix">
        Problems
    </MudNavLink>*@
</MudNavGroup>

<MudNavGroup Title="Inventory" Expanded="ExpandInventory">
    <MudNavGroup Title="System" Expanded="ExpandInventorySystem">
        <MudNavLink Href=@Navigation.Inventory.Systems.Os.Index Match="NavLinkMatch.Prefix">
            Os
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Systems.Updates.Index Match="NavLinkMatch.Prefix">
            Updates
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Systems.Software.Index Match="NavLinkMatch.Prefix">
            Software
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Systems.Services.Index Match="NavLinkMatch.Prefix">
            Services
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Systems.Sessions.Index Match="NavLinkMatch.Prefix">
            Sessions
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Systems.Printers.Index Match="NavLinkMatch.Prefix">
            Printers
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Systems.Volumes.Index Match="NavLinkMatch.Prefix">
            Volumes
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Systems.Users.Index Match="NavLinkMatch.Prefix">
            Users
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Systems.Groups.Index Match="NavLinkMatch.Prefix">
            Groups
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Systems.StoragePools.Index Match="NavLinkMatch.Prefix">
            Storage Pools
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Systems.VirtualMaschines.Index Match="NavLinkMatch.Prefix">
            Virtual Maschines
        </MudNavLink>
    </MudNavGroup>
    <MudNavGroup Title="Network" Expanded="ExpandInventoryNetwork">
        <MudNavLink Href=@Navigation.Inventory.Network.Interfaces.Index Match="NavLinkMatch.Prefix">
            Interfaces
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Network.Addresses.Index Match="NavLinkMatch.Prefix">
            Addresses
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Network.Nameservers.Index Match="NavLinkMatch.Prefix">
            Nameservers
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Network.Gateways.Index Match="NavLinkMatch.Prefix">
            Gateways
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Network.Routes.Index Match="NavLinkMatch.Prefix">
            Routes
        </MudNavLink>
    </MudNavGroup>
    <MudNavGroup Title="Hardware" Expanded="ExpandInventoryHardware">
        <MudNavLink Href=@Navigation.Inventory.Hardware.Mainboards.Index Match="NavLinkMatch.Prefix">
            Mainboards
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Hardware.Processors.Index Match="NavLinkMatch.Prefix">
            Processors
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Hardware.Memory.Index Match="NavLinkMatch.Prefix">
            Memory
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Hardware.Drives.Index Match="NavLinkMatch.Prefix">
            Drives
        </MudNavLink>
        <MudNavLink Href=@Navigation.Inventory.Hardware.Videocards.Index Match="NavLinkMatch.Prefix">
            Videocards
        </MudNavLink>
    </MudNavGroup>
</MudNavGroup>

<MudNavGroup Title="Management" Expanded="ExpandManagement">
    <MudNavLink Href=@Navigation.Management.Accounts.Index Match="NavLinkMatch.Prefix">
        Accounts
    </MudNavLink>
    <MudNavLink Href=@Navigation.Management.Customers.Index Match="NavLinkMatch.Prefix">
        Customers
    </MudNavLink>
    <MudNavLink Href=@Navigation.Management.Hosts.Index Match="NavLinkMatch.Prefix">
        Hosts
    </MudNavLink>
    <MudNavLink Href=@Navigation.Management.HostGroups.Index Match="NavLinkMatch.Prefix">
        Host Groups
    </MudNavLink>
    <MudNavLink Href=@Navigation.Management.Agents.Index Match="NavLinkMatch.Prefix">
        Agents
    </MudNavLink>
    <MudNavLink Href=@Navigation.Management.Scheduler.Jobs.Index Match="NavLinkMatch.Prefix" Style="text-decoration: line-through;">
        Scheduler
    </MudNavLink>
</MudNavGroup>

@code{
    private bool ExpandMonitoring => _uri.StartsWith(Navigation.Monitoring.Index);
    private bool ExpandManagement => _uri.StartsWith(Navigation.Management.Index);
    private bool ExpandInventory => _uri.StartsWith(Navigation.Inventory.Index);
    private bool ExpandInventorySystem => _uri.StartsWith(Navigation.Inventory.Systems.Index);
    private bool ExpandInventoryNetwork => _uri.StartsWith(Navigation.Inventory.Network.Index);
    private bool ExpandInventoryHardware => _uri.StartsWith(Navigation.Inventory.Hardware.Index);
    private string _uri = string.Empty;
    private IDisposable? _token;
    private bool _disposed;

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

    public void OnRefresh(string? message = null)
    {
        _uri = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
        InvokeAsync(() => StateHasChanged());
    }

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

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

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