using Insight.Infrastructure;
using Insight.Infrastructure.Entities;
using Insight.Web.Constants;
using Microsoft.AspNetCore.Components;
using MongoDB.Bson;
using MongoDB.Driver;
using MudBlazor;

namespace Insight.Web.Pages.Management.Hosts.Network.Interfaces;

[Route(Navigation.Management.Hosts.Network.Interfaces.Details)]
public partial class Details
{
    [Parameter] public string? HostId { get; set; }
    [Parameter] public string? InterfaceId { get; set; }

    [Inject] private IMongoDatabase Database { get; init; } = default!;
    [Inject] private ISnackbar Snackbar { get; init; } = default!;
    [Inject] private NavigationManager NavigationManager { get; init; } = default!;

    private string Title { get; set; } = Global.Name;
    private List<BreadcrumbItem> Breadcrumbs { get; } = new();
    private HostInterfaceEntity? HostInterface { get; set; }

    private async Task LoadDataAsync()
    {
        Breadcrumbs.Clear();
        HostInterface = null;

        if (string.IsNullOrWhiteSpace(HostId) || ObjectId.TryParse(HostId, out var hostId) is false)
        {
            Notification.Error(Snackbar, "Not Found");
            NavigationManager.NavigateTo(Navigation.Management.Hosts.Index);
            return;
        }

        Breadcrumbs.Add(new BreadcrumbItem("Home", href: Navigation.Home));
        Breadcrumbs.Add(new BreadcrumbItem("Hosts", href: Navigation.Management.Hosts.Index));

        StateHasChanged();

        var host = await Database.Host()
            .Find(Builders<HostEntity>
            .Filter
            .Eq(p => p.Id, HostId))
            .FirstOrDefaultAsync(default);

        if (host is null)
        {
            Notification.Error(Snackbar, "Not Found");
            NavigationManager.NavigateTo(Navigation.Management.Hosts.Index);
            return;
        }

        Breadcrumbs.Add(new BreadcrumbItem(host.Name, href: Navigation.Management.Hosts.DetailsHref(HostId)));

        StateHasChanged();

        if (string.IsNullOrWhiteSpace(InterfaceId) || ObjectId.TryParse(InterfaceId, out var interfaceId) is false)
        {
            Notification.Error(Snackbar, "Not Found");
            NavigationManager.NavigateTo(Navigation.Management.Hosts.Network.Interfaces.IndexHref(HostId));
            return;
        }

        HostInterface = await Database.HostInterface()
                .Find(p => p.Host == HostId && p.Id == InterfaceId)
            .FirstOrDefaultAsync();

        if (HostInterface is null)
        {
            Notification.Error(Snackbar, "Not Found");
            NavigationManager.NavigateTo(Navigation.Management.Hosts.Network.Interfaces.IndexHref(HostId));
            return;
        }

        Title = $"Hosts » {host.Name} » Interface » {HostInterface.Name ?? HostInterface.Description}｜Insight";


        Breadcrumbs.Add(new BreadcrumbItem("Interfaces", href: Navigation.Management.Hosts.Network.Interfaces.IndexHref(HostId)));
        Breadcrumbs.Add(new BreadcrumbItem(HostInterface.Name ?? HostInterface.Description, href: "#", true));

        StateHasChanged();
    }
}