using Insight.Agent.Messages;
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.Systems.VirtualMaschines.Snapshots;

[Route(Navigation.Management.Hosts.Systems.VirtualMaschines.Snapshots.Details)]
public partial class Details
{
    [Parameter] public string? HostId { get; set; }
    [Parameter] public string? VirtualMaschineId { get; set; }
    [Parameter] public string? SnapshotId { 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 HostHypervisorVirtualMaschineEntity? VirtualMaschine { get; set; }
    private HostHypervisorVirtualMaschineConfigEntity? Snapshot { get; set; }
    private HostHypervisorVirtualMaschineConfigEntity? ParentSnapshot { get; set; }


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

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

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

        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)));
        Breadcrumbs.Add(new BreadcrumbItem("Virtual Maschines", href: Navigation.Management.Hosts.Systems.VirtualMaschines.IndexHref(HostId)));

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

        VirtualMaschine = await Database.HostHypervisorVirtualMaschine()
            .Find(p => p.Host == HostId && p.Id == VirtualMaschineId)
            .FirstOrDefaultAsync();

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

        Breadcrumbs.Add(new BreadcrumbItem(VirtualMaschine.Name, href: Navigation.Management.Hosts.Systems.VirtualMaschines.DetailsHref(HostId, VirtualMaschineId)));

        if (string.IsNullOrWhiteSpace(SnapshotId) || ObjectId.TryParse(SnapshotId, out var snapshotId) is false)
        {
            Notification.Error(Snackbar, "Not Found");
            NavigationManager.NavigateTo(Navigation.Management.Hosts.Systems.VirtualMaschines.DetailsHref(HostId, VirtualMaschineId));
            return;
        }

        // get snapshot
        Snapshot = await Database.HostVirtualMaschineConfig()
            .Find(p => p.Host == HostId && p.VirtualMaschine == VirtualMaschineId && p.Id == SnapshotId)
            .FirstOrDefaultAsync();

        if (Snapshot is null)
        {
            Notification.Error(Snackbar, "Not Found");
            NavigationManager.NavigateTo(Navigation.Management.Hosts.Systems.VirtualMaschines.DetailsHref(HostId, VirtualMaschineId));
            return;
        }

        // get parent snapshot
        if (Snapshot.ParentId is not null)
        {
            ParentSnapshot = await Database.HostVirtualMaschineConfig()
                .Find(p => p.Host == HostId && p.VirtualMaschine == VirtualMaschineId && p.UniqueId == Snapshot.ParentId)
                .FirstOrDefaultAsync();
        }

        Breadcrumbs.Add(new BreadcrumbItem(Snapshot.Name, href: "#", true));
        Title = $"Hosts » {host.Name} » Virtual Maschine » {VirtualMaschine.Name} » Snapshot » {Snapshot.Name}｜Insight";

        StateHasChanged();
    }
}