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.StoragePools.PhysicalDisks;

[Route(Navigation.Management.Hosts.Systems.StoragePools.PhysicalDisks.Details)]
public partial class Details
{
    [Parameter] public string? HostId { get; set; }
    [Parameter] public string? StoragePoolId { get; set; }
    [Parameter] public string? PhysicalDiskId { 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 HostStoragePoolPhysicalDiskEntity? HostPhysicalDisk { get; set; }

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

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

        StateHasChanged();

        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("Storage Pools", href: Navigation.Management.Hosts.Systems.StoragePools.IndexHref(HostId)));

        StateHasChanged();

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

        var storagePool = await Database.HostStoragePool()
            .Find(p => p.Host == HostId && p.Id == StoragePoolId)
            .FirstOrDefaultAsync();

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

        Breadcrumbs.Add(new BreadcrumbItem(storagePool.Name, href: Navigation.Management.Hosts.Systems.StoragePools.DetailsHref(HostId, StoragePoolId)));
        Breadcrumbs.Add(new BreadcrumbItem("Physical Disks", href: Navigation.Management.Hosts.Systems.StoragePools.PhysicalDisks.IndexHref(HostId, StoragePoolId)));

        StateHasChanged();

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

        HostPhysicalDisk = await Database.HostStoragePoolPhysicalDisk()
            .Find(p => p.Host == HostId && p.StoragePool == StoragePoolId && p.Id == PhysicalDiskId)
            .FirstOrDefaultAsync();

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

        Title = $"Hosts » {host.Name} » Storage Pool » {storagePool.Name} » Physical Disk » {HostPhysicalDisk.Name}｜Insight";

        Breadcrumbs.Add(new BreadcrumbItem(HostPhysicalDisk.Name, href: "#", true));

        StateHasChanged();
    }
}