using Insight.Web.Constants;
using Microsoft.AspNetCore.Components;
using Vaitr.MemoryBus;

namespace Insight.Web.Components.Navbars;

public partial class Main : IDisposable
{
    [Inject] private IMemoryBus Bus { get; init; } = default!;
    [Inject] private NavigationManager NavigationManager { get; init; } = default!;

    private string Uri { get; set; } = string.Empty;
    private IDisposable? Token { get; set; }
    public bool Disposed { get; set; } = false;

    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());
    }

    private bool ExpandMonitoringGroup
    {
        get
        {
            if (Uri.StartsWith(Navigation.Monitoring.Index)) return true;
            return false;
        }
    }

    private bool ExpandManagementGroup
    {
        get
        {
            if (Uri.StartsWith(Navigation.Management.Overview.Index)) return true;
            if (Uri.StartsWith(Navigation.Management.Accounts.Index)) return true;
            if (Uri.StartsWith(Navigation.Management.Customers.Index)) return true;
            if (Uri.StartsWith(Navigation.Management.Hosts.Index)) return true;
            if (Uri.StartsWith(Navigation.Management.HostGroups.Index)) return true;
            if (Uri.StartsWith(Navigation.Management.Agents.Index)) return true;
            return false;
        }
    }

    private bool ExpandInventoryGroup
    {
        get
        {
            if (ExpandInventorySystem) return true;
            if (ExpandInventoryNetwork) return true;
            if (ExpandInventoryHardware) return true;
            return false;
        }
    }

    private bool ExpandInventorySystem
    {
        get
        {
            if (Uri.StartsWith(Navigation.Inventory.Systems.Os.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Os.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Os.Guests)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Updates.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Updates.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Software.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Software.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Services.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Services.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Sessions.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Printers.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Volumes.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Users.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Users.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Groups.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.Groups.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.StoragePools.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Systems.VirtualMaschines.Index)) return true;
            return false;
        }
    }

    private bool ExpandInventoryNetwork
    {
        get
        {
            if (Uri.StartsWith(Navigation.Inventory.Network.Interfaces.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Network.Addresses.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Network.Addresses.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Network.Nameservers.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Network.Nameservers.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Network.Gateways.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Network.Gateways.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Network.Routes.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Network.Routes.Hosts)) return true;
            return false;
        }
    }

    private bool ExpandInventoryHardware
    {
        get
        {
            if (Uri.StartsWith(Navigation.Inventory.Hardware.Mainboards.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Hardware.Mainboards.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Hardware.Processors.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Hardware.Processors.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Hardware.Memory.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Hardware.Memory.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Hardware.Drives.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Hardware.Drives.Hosts)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Hardware.Videocards.Index)) return true;
            if (Uri.StartsWith(Navigation.Inventory.Hardware.Videocards.Hosts)) return true;
            return false;
        }
    }

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

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

        Token?.Dispose();
    }
}