Option Strict On
Option Explicit On

Imports System
Imports System.Linq

Namespace Nexamas.UI.DiagnosticsDashboard

    ''' <summary>
    ''' Immutable read-only dashboard snapshot that joins existing render, layout, element, and virtualization facts.
    ''' </summary>
    Friend NotInheritable Class MASDiagnosticsDashboardSnapshot

        Private ReadOnly _metrics As MASDiagnosticsDashboardMetric()

        Friend Sub New(metrics As MASDiagnosticsDashboardMetric(),
                       sourceName As String)
            _metrics = If(metrics, New MASDiagnosticsDashboardMetric() {}).Where(Function(metric) metric IsNot Nothing).ToArray()
            Me.SourceName = If(sourceName, String.Empty)
        End Sub

        Friend ReadOnly Property SourceName As String

        Friend ReadOnly Property Metrics As MASDiagnosticsDashboardMetric()
            Get
                Return DirectCast(_metrics.Clone(), MASDiagnosticsDashboardMetric())
            End Get
        End Property

        Friend ReadOnly Property MetricCount As Integer
            Get
                Return _metrics.Length
            End Get
        End Property

        Friend ReadOnly Property IsReadOnlyFoundation As Boolean
            Get
                Return True
            End Get
        End Property

        Friend ReadOnly Property HasHeavyUiSurface As Boolean
            Get
                Return False
            End Get
        End Property

        Friend ReadOnly Property HasRenderMetrics As Boolean
            Get
                Return HasAvailableMetric(MASDiagnosticsDashboardMetricKind.RenderTime) AndAlso
                       HasAvailableMetric(MASDiagnosticsDashboardMetricKind.RenderStageTime)
            End Get
        End Property

        Friend ReadOnly Property HasLayoutMetrics As Boolean
            Get
                Return HasAvailableMetric(MASDiagnosticsDashboardMetricKind.LayoutPasses)
            End Get
        End Property

        Friend ReadOnly Property HasElementMetrics As Boolean
            Get
                Return HasAvailableMetric(MASDiagnosticsDashboardMetricKind.ElementCount)
            End Get
        End Property

        Friend ReadOnly Property HasVirtualizationMetrics As Boolean
            Get
                Return HasAvailableMetric(MASDiagnosticsDashboardMetricKind.VirtualizedItemCount) AndAlso
                       HasAvailableMetric(MASDiagnosticsDashboardMetricKind.VirtualizedRealizedCount) AndAlso
                       HasAvailableMetric(MASDiagnosticsDashboardMetricKind.VirtualizationRealizationRatio)
            End Get
        End Property

        Friend ReadOnly Property HasBaselineBenchmarkMetrics As Boolean
            Get
                Return HasAvailableMetric(MASDiagnosticsDashboardMetricKind.BaselineBenchmarkReadiness) AndAlso
                       HasAvailableMetric(MASDiagnosticsDashboardMetricKind.BaselineBenchmarkEntryCount) AndAlso
                       HasAvailableMetric(MASDiagnosticsDashboardMetricKind.BaselineBenchmarkReadyEntryCount) AndAlso
                       HasAvailableMetric(MASDiagnosticsDashboardMetricKind.BaselineBenchmarkWarningCount) AndAlso
                       HasAvailableMetric(MASDiagnosticsDashboardMetricKind.BaselineBenchmarkFailedCount)
            End Get
        End Property

        Friend ReadOnly Property WarningMetricCount As Integer
            Get
                Return _metrics.Where(Function(metric) metric.IsWarning).Count()
            End Get
        End Property

        Friend ReadOnly Property HasWarnings As Boolean
            Get
                Return WarningMetricCount > 0
            End Get
        End Property

        Friend ReadOnly Property ReadyForBaselineAwareDisplay As Boolean
            Get
                Return ReadyForReadOnlyDisplay AndAlso HasBaselineBenchmarkMetrics
            End Get
        End Property

        Friend ReadOnly Property ReadyForReadOnlyDisplay As Boolean
            Get
                Return IsReadOnlyFoundation AndAlso
                       Not HasHeavyUiSurface AndAlso
                       HasRenderMetrics AndAlso
                       HasLayoutMetrics AndAlso
                       HasElementMetrics AndAlso
                       HasVirtualizationMetrics
            End Get
        End Property

        Friend Function GetMetric(kind As MASDiagnosticsDashboardMetricKind) As MASDiagnosticsDashboardMetric
            Return _metrics.FirstOrDefault(Function(metric) metric.Kind = kind)
        End Function

        Private Function HasAvailableMetric(kind As MASDiagnosticsDashboardMetricKind) As Boolean
            Dim metric As MASDiagnosticsDashboardMetric = GetMetric(kind)
            Return metric IsNot Nothing AndAlso metric.IsAvailable
        End Function

    End Class

End Namespace
