Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.TextRendering
Imports SkiaSharp

Namespace Nexamas.UI.Quality

    ''' <summary>
    ''' Phase-7 proof for DPI-03 and DPI-04.
    ''' Verifies that repeated HarfBuzz drawing uses the owned drawable-run/blob cache
    ''' and that system-font fallback drawing is bounded by the official bitmap/measure
    ''' caches instead of allocating a new GDI bitmap on every draw.
    ''' </summary>
    Friend NotInheritable Class MASDpiTextShapingFallbackPerformanceGate
        Friend Const HarfBuzzCacheRecipeName As String = "DPI-03.HarfBuzzDrawableRunCache"
        Friend Const SystemFallbackCacheRecipeName As String = "DPI-04.SystemFallbackBitmapCache"
        Friend Const RepeatedDrawCount As Integer = 32

        Private Sub New()
        End Sub

        Friend Shared Function Passes() As Boolean
            Return ProbeHarfBuzzDrawableRunCache() AndAlso
                   ProbeSystemFallbackBitmapCache()
        End Function

        Private Shared Function ProbeHarfBuzzDrawableRunCache() As Boolean
            Try
                MASShapedText.ClearCache()

                Using paint As SKPaint = CreateProofPaint()
                    Using bitmap As New SKBitmap(320, 96)
                        Using canvas As New SKCanvas(bitmap)
                            Dim sample As String = "مرحبا بالعالم HarfBuzz repeated text 123"

                            For i As Integer = 0 To RepeatedDrawCount - 1
                                MASShapedText.Draw(canvas, sample, 8.0F, 48.0F, paint)
                            Next
                        End Using
                    End Using
                End Using

                Dim snapshot As MASTextRenderingCacheSnapshot = MASShapedText.CaptureCacheSnapshot()
                If snapshot Is Nothing Then Return False
                If snapshot.HarfBuzzDrawCacheEntries <= 0 Then Return False
                If snapshot.HarfBuzzDrawCacheMisses <= 0 Then Return False
                If snapshot.HarfBuzzDrawCacheHits < RepeatedDrawCount - 2 Then Return False

                Return True
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASDpiTextShapingFallbackPerformanceGate.HarfBuzzDrawableRunCache")
                Return False
            End Try
        End Function

        Private Shared Function ProbeSystemFallbackBitmapCache() As Boolean
            Try
                MASShapedText.ClearCache()

                Using paint As SKPaint = CreateProofPaint()
                    Using bitmap As New SKBitmap(420, 110)
                        Using canvas As New SKCanvas(bitmap)
                            Dim sample As String = "CJK 漢字かなカナ emoji 😀🚀 fallback repeated"

                            For i As Integer = 0 To RepeatedDrawCount - 1
                                MASShapedText.Draw(canvas, sample, 8.0F, 58.0F, paint)
                            Next
                        End Using
                    End Using
                End Using

                Dim snapshot As MASTextRenderingCacheSnapshot = MASShapedText.CaptureCacheSnapshot()
                If snapshot Is Nothing Then Return False
                If snapshot.SystemFallbackBitmapCacheEntries <= 0 Then Return False
                If snapshot.SystemFallbackBitmapCacheMisses <= 0 Then Return False
                If snapshot.SystemFallbackBitmapCacheHits < RepeatedDrawCount - 2 Then Return False
                If snapshot.SystemFallbackMeasureCacheEntries <= 0 Then Return False

                Return True
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDiagnosticOnly(ex, "MASDpiTextShapingFallbackPerformanceGate.SystemFallbackBitmapCache")
                Return False
            End Try
        End Function

        Private Shared Function CreateProofPaint() As SKPaint
            Return New SKPaint With {
                .IsAntialias = True,
                .SubpixelText = True,
                .Typeface = SKTypeface.Default,
                .TextSize = 16.0F,
                .Color = SKColors.Black
            }
        End Function
    End Class

End Namespace
