@inherits ComponentBase
@using Insight.Web.Pages.Management.Hosts.Systems.VirtualMaschines.Snapshots
@using Microsoft.AspNetCore.Components.Rendering
@using static Insight.Agent.Messages.VirtualMaschine;

<BaseContainer Title="@Title" Breadcrumbs="@Breadcrumbs" LoadData="LoadDataAsync">
    <Content>
        @if (HostVirtualMaschine is not null)
        {
            <MudGrid>
                <MudItem xs="12" sm="6" md="6" lg="3">
                    <KeyValueCard T="string" Key="Id" Value="@HostVirtualMaschine?.UniqueId" />
                </MudItem>

                <MudItem xs="12" sm="6" md="6" lg="3">
                    <KeyValueCard T="string" Key="Name" Value="@HostVirtualMaschine?.Name" />
                </MudItem>

                <MudItem xs="12" sm="6" md="6" lg="3">
                    <KeyValueCard T="string" Key="Configuration Version" Value="@HostVirtualMaschine?.ConfigurationVersion" />
                </MudItem>

                <MudItem xs="12" sm="6" md="6" lg="3">
                    <KeyValueCard T="string" Key="Default State" Value="@HostVirtualMaschine?.EnabledDefault" />
                </MudItem>

                <MudItem xs="12" sm="6" md="6" lg="3">
                    @{
                        var color = Enum.Parse<EnabledEnum>(HostVirtualMaschine?.Enabled, true) switch
                        {
                            EnabledEnum.Aktiviert => Color.Success,
                            EnabledEnum.Deaktiviert => Color.Error,
                            _ => Color.Inherit
                        };

                        var text = Enum.Parse<EnabledEnum>(HostVirtualMaschine?.Enabled, true) switch
                        {
                            EnabledEnum.Aktiviert => "Running",
                            EnabledEnum.Deaktiviert => "Stopped",
                            _ => "Unknown"
                        };

                        <KeyValueCard T="string" Key="State" ValueColor="@color" Value="@text" />
                    }
                </MudItem>

                <MudItem xs="12" sm="6" md="6" lg="3">
                    @if (HostVirtualMaschine?.Health is not null)
                    {
                        var color = Enum.Parse<HealthStatusEnum>(HostVirtualMaschine.Health, true) switch
                        {
                            HealthStatusEnum.OK => Color.Success,
                            HealthStatusEnum.Hauptfehler => Color.Warning,
                            HealthStatusEnum.Kritischer_Fehler => Color.Error,
                            _ => Color.Error
                        };

                        <KeyValueCard T="string" Key="Health" ValueColor="color" Value="@HostVirtualMaschine?.Health" />
                    }
                </MudItem>

                <MudItem xs="12" sm="6" md="6" lg="3">
                    <KeyValueCard T="uint?" Key="Processors" Value="@HostVirtualMaschine?.NumberOfProcessors" />
                </MudItem>   
                
                @if (Enum.TryParse<EnabledEnum>(HostVirtualMaschine?.Enabled, true, out var enabled) && enabled == EnabledEnum.Aktiviert)
                {
                    @if (enabled == EnabledEnum.Aktiviert)
                    {
                        <MudItem xs="12" sm="6" md="6" lg="3">
                            <KeyValueCard T="uint?" Key="Processor Usage %" Value="@HostVirtualMaschine?.ProcessorLoad" />
                        </MudItem>
                    }

                    @if (HostVirtualMaschine?.MemoryUsage is not null)
                    {
                        <MudItem xs="12" sm="6" md="6" lg="3">
                            @{
                                var value = "-";

                                value = $"{Math.Round((HostVirtualMaschine.MemoryUsage.Value / Math.Pow(1024, 1)), 2)} GB";

                                <KeyValueCard T="string" Key="Memory" Value="@value" />
                            }
                        </MudItem>
                    }

                    @if (HostVirtualMaschine?.MemoryAvailable is not null)
                    {
                        <MudItem xs="12" sm="6" md="6" lg="3">
                            <KeyValueCard T="int?" Key="Available Memory %" Value="@HostVirtualMaschine?.MemoryAvailable" />
                        </MudItem>
                    }

                    <MudItem xs="12" sm="6" md="6" lg="3">
                        <KeyValueCard T="uint?" Key="Process" Value="@HostVirtualMaschine?.ProcessId" />
                    </MudItem>

                    @if (HostVirtualMaschine?.OnTime is not null)
                    {
                        var ontime = TimeSpan.FromMilliseconds((double)HostVirtualMaschine.OnTime);
                        string label = string.Empty;
                        string value = string.Empty;

                        if (ontime.TotalDays > 1)
                        {
                            label = "Days";
                            value = Math.Round(ontime.TotalDays).ToString();
                        }
                        else if (ontime.TotalHours > 1)
                        {
                            label = "Hours";
                            value = Math.Round(ontime.TotalHours).ToString();
                        }
                        else if (ontime.TotalMinutes > 1)
                        {
                            label = "Minutes";
                            value = Math.Round(ontime.TotalMinutes).ToString();
                        }
                        else
                        {
                            label = "Seconds";
                            value = Math.Round(ontime.TotalSeconds, 0).ToString();
                        }

                        <MudItem xs="12" sm="6" md="6" lg="3">
                            <KeyValueCard T="string" Key="@($"Uptime ({label})")" Value="@value" />
                        </MudItem>
                    }
                }

                <MudItem xs="12" sm="6" md="6" lg="3">
                    <KeyValueCard T="string" Key="Created" Value="@HostVirtualMaschine?.InstallDate?.ToString("dd/MM/yyyy HH:mm:ss")" />
                </MudItem>
                @*<MudItem xs="12" sm="6" md="6" lg="3">
                    <KeyValueCard T="string" Key="Modified" Value="@HostVirtualMaschine?.TimeOfLastConfigurationChange?.ToString("dd/MM/yyyy HH:mm:ss")" />
                </MudItem>*@
                <MudItem xs="12" sm="6" md="6" lg="3">
                    <KeyValueCard T="string" Key="Last State Change" Value="@HostVirtualMaschine?.TimeOfLastStateChange?.ToString("dd/MM/yyyy HH:mm:ss")" />
                </MudItem>
                <MudItem xs="12" sm="6" md="6" lg="3">
                    @{
                        var value = "-";

                        @if (HostVirtualMaschine?.LastReplicationTime is not null && HostVirtualMaschine.LastReplicationTime.Value.Year >= 2000)
                        {
                            value = @HostVirtualMaschine?.LastReplicationTime?.ToString("dd/MM/yyyy HH:mm:ss");
                        }

                        <KeyValueCard T="string" Key="Last Replication" Value="@value" />
                    }
                </MudItem>

                @*<MudItem xs="12" md="6" lg="3">
                    <MudTextField Label="Snapshots" Value="@(VirtualMaschineEntity?.VirtualMaschine?.Configurations?.Count)" Variant="Variant.Outlined" Margin="Margin.Dense" ReadOnly
                                  Adornment="Adornment.End" AdornmentIcon="@Icons.Filled.CenterFocusWeak" AdornmentColor="Color.Primary" OnAdornmentClick="@(() => OnSnapshots())" />
                </MudItem>*@
            </MudGrid>

            @if (string.IsNullOrWhiteSpace(HostVirtualMaschine?.Notes) is false)
            {
                <div class="mt-7 mb-7">
                    <MudItem xs="12" sm="12" md="12" lg="12">
                        <KeyValueCard T="string" Key="Notes" Value="@HostVirtualMaschine?.Notes" />
                    </MudItem>
                </div>
            }

            @if (HostVirtualMaschine?.Configs is not null)
            {
                <div class="mt-7 mb-7">
                    <MudExpansionPanels>
                        <MudExpansionPanel Text="Snapshots" IsInitiallyExpanded="true" Icon="@Icons.Material.Filled.SettingsBackupRestore">
                            <MudTreeView Items="@TreeItems" Dense="false" Hover="true" ExpandOnClick="false">
                                <ItemTemplate>
                                    <MudTreeViewItem @bind-Expanded="@context.IsExpanded" Items="@context.TreeItems">
                                        <Content>
                                            <MudTreeViewItemToggleButton @bind-Expanded="@context.IsExpanded" Visible="@context.HasChild" />
                                            @if (context.IsCurrentConfig)
                                            {
                                                <MudIcon Icon="Icons.Filled.Check" Class="ml-3" Color="Color.Success" />
                                                <MudLink Href="@Navigation.Management.Hosts.Systems.VirtualMaschines.Snapshots.DetailsHref(HostId, VirtualMaschineId, context.Entity?.Id)" Typo="Typo.body1" Underline="Underline.None" Color="Color.Success">
                                                    <b>Active</b>
                                                </MudLink>
                                            }
                                            else
                                            {
                                                <MudLink Href="@Navigation.Management.Hosts.Systems.VirtualMaschines.Snapshots.DetailsHref(HostId, VirtualMaschineId, context.Entity?.Id)" Typo="Typo.body1" Underline="Underline.None">
                                                    <b>@context?.Entity?.Name</b>
                                                </MudLink>

                                                @if (string.IsNullOrWhiteSpace(context?.Entity?.Notes) is false)
                                                {
                                                    <MudText Class="ml-7" Align="Align.Right">
                                                        @($"{context?.Entity?.Notes}")
                                                    </MudText>
                                                }

                                                <MudSpacer />

                                                <MudText Class="ml-7">
                                                    @($"{context?.Entity?.CreationTime?.ToLocalTime()}")
                                                </MudText>
                                            }
                                        </Content>
                                    </MudTreeViewItem>
                                </ItemTemplate>
                            </MudTreeView>
                        </MudExpansionPanel>
                    </MudExpansionPanels>
                </div>
            }
        }
    </Content>
</BaseContainer>