Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.General
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Extracted responsibility slice for MASAcrylicEffectPainter.
    ''' Kept partial to preserve the binary/public surface while reducing hotspot size.
    ''' </summary>
    Partial Friend NotInheritable Class MASAcrylicEffectPainter

#Region "Grain cache"

        Private Sub EnsureGrainCache(dpi As Single,
                                     grainTileDip As Single)

            Dim dpi100 As Integer = Math.Max(1, MASVisualPrimitivesPainter.ToDpi100(dpi))
            Dim tilePx As Single = grainTileDip * dpi
            Dim tilePx100 As Integer = Math.Max(1, CInt(Math.Round(tilePx * 100.0F)))

            If _grainShader IsNot Nothing AndAlso
               _grainBmp IsNot Nothing AndAlso
               _grainLastDpi100 = dpi100 AndAlso
               _grainLastTilePx100 = tilePx100 Then

                Return
            End If

            Try
                If _grainShader IsNot Nothing Then _grainShader.Dispose()
            Catch masCaughtException1 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException1)
            End Try

            _grainShader = Nothing

            Try
                If _grainBmp IsNot Nothing Then _grainBmp.Dispose()
            Catch masCaughtException2 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException2)
            End Try

            _grainBmp = Nothing

            _grainLastDpi100 = dpi100
            _grainLastTilePx100 = tilePx100

            Dim w As Integer = 64
            Dim h As Integer = 64
            Dim bmp As New SKBitmap(w, h, SKColorType.Bgra8888, SKAlphaType.Premul)

            Dim rnd As New Random(1337)

            For yy As Integer = 0 To h - 1
                For xx As Integer = 0 To w - 1
                    Dim v As Integer = 128 + rnd.Next(-6, 7)
                    If v < 0 Then v = 0
                    If v > 255 Then v = 255
                    bmp.SetPixel(xx, yy, New SKColor(CByte(v), CByte(v), CByte(v), 255))
                Next
            Next

            _grainBmp = bmp

            Dim desiredTilePx As Single = tilePx
            Dim sx As Single = desiredTilePx / CSng(w)
            Dim sy As Single = desiredTilePx / CSng(h)

            If sx < 0.2F Then sx = 0.2F
            If sy < 0.2F Then sy = 0.2F

            Dim mat As SKMatrix = SKMatrix.CreateScale(sx, sy)
            _grainShader = SKShader.CreateBitmap(_grainBmp, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat, mat)

        End Sub

#End Region

#Region "Helpers"

        Private Shared Function Clamp01(v As Single) As Single
            If Single.IsNaN(v) OrElse Single.IsInfinity(v) Then Return 0.0F
            If v < 0.0F Then Return 0.0F
            If v > 1.0F Then Return 1.0F
            Return v
        End Function

        Private Shared Function ScaleByte(v As Byte,
                                          mul As Single) As Byte

            Dim x As Integer = CInt(Math.Round(v * mul))
            If x < 0 Then x = 0
            If x > 255 Then x = 255

            Return CByte(x)

        End Function

        Private Shared Function StrengthToMul(v As Single) As Single
            v = Clamp01(v)
            Return 0.76F + v * 0.42F
        End Function

        Private Shared Sub SafeDisposePaint(p As SKPaint)
            If p Is Nothing Then Return

            Try
                p.Dispose()
            Catch masCaughtException3 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException3)
            End Try
        End Sub

        Private Shared Sub SafeDisposeBitmap(ByRef bmp As SKBitmap)
            If bmp Is Nothing Then Return

            Try
                bmp.Dispose()
            Catch masCaughtException4 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException4)
            Finally
                bmp = Nothing
            End Try
        End Sub

#End Region

    

    End Class

End Namespace
