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 "Shared acrylic layers"

        Private Sub DrawCoolAcrylicTopEdgeMicroHighlight(canvas As SKCanvas,
                                                         lr As SKRect,
                                                         radius As Single,
                                                         dpi As Single,
                                                         midCol As SKColor,
                                                         intensity As MASVisualEffectIntensityProfiles.AcrylicSurfaceIntensityProfile)

            If canvas Is Nothing Then Return
            If lr.Width <= 1 OrElse lr.Height <= 1 Then Return
            If intensity.TopBandEmphasis <= 0.0F Then Return

            Dim y As Single = Math.Max(0.5F, dpi)
            Dim strokeW As Single =
                Math.Max(SurfaceTokens.FxRingMinStrokePx, 0.95F * dpi)

            Dim alpha As Byte =
                CByte(Math.Max(0, Math.Min(255, CInt(Math.Round(12.0F + intensity.TopBandEmphasis * 18.0F)))))

            Dim hi As SKColor =
                ColorMath.WithAlpha(ColorMath.Mix(SKColors.White, midCol, 0.06F), alpha)

            _hairline.StrokeWidth = strokeW
            _hairline.Color = hi
            _hairline.Shader = Nothing

            canvas.DrawLine(
                lr.Left + radius * 0.5F,
                y,
                lr.Right - radius * 0.5F,
                y,
                _hairline)

        End Sub

        Private Sub DrawCoolAcrylicInnerBottomVignette(canvas As SKCanvas,
                                                       lr As SKRect,
                                                       radius As Single,
                                                       botCol As SKColor,
                                                       intensity As MASVisualEffectIntensityProfiles.AcrylicSurfaceIntensityProfile)

            If canvas Is Nothing Then Return
            If lr.Width <= 1 OrElse lr.Height <= 1 Then Return
            If intensity.BottomAnchorStrength <= 0.0F Then Return

            Dim alpha As Byte =
                CByte(Math.Max(0, Math.Min(255, CInt(Math.Round(10.0F + intensity.BottomAnchorStrength * 16.0F)))))

            Using sh As SKShader = SKShader.CreateRadialGradient(
                New SKPoint(lr.MidX, lr.Top + lr.Height * 0.96F),
                Math.Max(lr.Width, lr.Height) * 0.9F,
                New SKColor() {
                    SKColors.Transparent,
                    ColorMath.WithAlpha(ColorMath.Mix(botCol, SKColors.Black, 0.12F), alpha)
                },
                New Single() {0.64F, 1.0F},
                SKShaderTileMode.Clamp)

                Using p As New SKPaint With {
                    .IsAntialias = True,
                    .IsDither = True,
                    .Style = SKPaintStyle.Fill,
                    .BlendMode = SKBlendMode.Multiply,
                    .Shader = sh
                }
                    canvas.DrawRoundRect(lr, radius, radius, p)
                End Using
            End Using

        End Sub

        Private Sub DrawCoolAcrylicBodyGlow(canvas As SKCanvas,
                                            lr As SKRect,
                                            radius As Single,
                                            intensity As MASVisualEffectIntensityProfiles.AcrylicSurfaceIntensityProfile)

            Dim cx As Single = lr.MidX
            Dim cy As Single =
                lr.Top + lr.Height * (0.24F + 0.12F * (1.0F - intensity.TopBandEmphasis))

            Dim rad As Single =
                Math.Max(lr.Width, lr.Height) *
                (0.88F + 0.14F * intensity.BodyGlowStrength)

            Dim glowA0 As Byte =
                CByte(Math.Max(0, Math.Min(255, CInt(Math.Round(8.0F + 18.0F * intensity.BodyGlowStrength)))))

            Dim glowA1 As Byte =
                CByte(Math.Max(0, Math.Min(255, CInt(Math.Round(3.0F + 7.0F * intensity.BodyGlowStrength)))))

            Using shGlow As SKShader = SKShader.CreateRadialGradient(
                New SKPoint(cx, cy),
                rad,
                New SKColor() {
                    New SKColor(255, 255, 255, glowA0),
                    New SKColor(255, 255, 255, glowA1),
                    SKColors.Transparent
                },
                New Single() {0.0F, 0.46F, 1.0F},
                SKShaderTileMode.Clamp)

                _glow.Shader = shGlow
                _glow.BlendMode = SKBlendMode.Screen
                canvas.DrawRoundRect(lr, radius, radius, _glow)
                _glow.Shader = Nothing
                _glow.BlendMode = SKBlendMode.SrcOver
            End Using

        End Sub

        Private Sub DrawCoolAcrylicTopSheen(canvas As SKCanvas,
                                            lr As SKRect,
                                            radius As Single,
                                            dpi As Single,
                                            intensity As MASVisualEffectIntensityProfiles.AcrylicSurfaceIntensityProfile)

            Dim shineInset As Single = SurfaceTokens.AcrylicShineInsetPx * dpi

            Dim shineBaseH As Single =
                Math.Max(
                    lr.Height * SurfaceTokens.AcrylicShineHeightRatio,
                    12.0F * dpi)

            Dim shineH As Single = shineBaseH * intensity.SheenHeightMul

            Dim shineRect As New SKRect(
                lr.Left + shineInset,
                lr.Top + shineInset,
                lr.Right - shineInset,
                Math.Min(lr.Bottom, lr.Top + shineH))

            _shine.Shader = _shineShader
            _shine.BlendMode = SKBlendMode.Screen
            _shine.Color = New SKColor(
                255,
                255,
                255,
                CByte(Math.Max(0, Math.Min(255, CInt(Math.Round(255.0F * intensity.SheenStrength))))))

            Dim shrink As Single = SurfaceTokens.AcrylicShineCornerShrinkPx * dpi
            Dim rrRad As Single = Math.Max(2.0F, radius - shrink)

            canvas.DrawRoundRect(shineRect, rrRad, rrRad, _shine)

            _shine.Shader = Nothing
            _shine.BlendMode = SKBlendMode.SrcOver
            _shine.Color = SKColors.White

        End Sub

        Private Sub DrawCoolAcrylicBorderHierarchy(canvas As SKCanvas,
                                                   baseRect As SKRect,
                                                   radius As Single,
                                                   dpi As Single,
                                                   borderOuter As SKColor,
                                                   borderInner As SKColor,
                                                   outerStrength As Single,
                                                   innerStrength As Single)

            If canvas Is Nothing Then Return
            If baseRect.Width <= 1 OrElse baseRect.Height <= 1 Then Return

            Dim strokePx As Single =
                Math.Max(
                    SurfaceTokens.FxRingStroke * dpi,
                    SurfaceTokens.FxRingMinStrokePx)

            Dim outerStroke As MASVisualPrimitivesPainter.StrokeRoundRectInfo =
                MASVisualPrimitivesPainter.BuildStrokeRoundRectInfo(
                    baseRect:=baseRect,
                    baseRadius:=radius,
                    dpi:=dpi,
                    edgeInsetPx:=0.0F,
                    strokeWidthPx:=strokePx)

            If outerStroke.HasValue Then
                _strokeOuter.IsAntialias = True
                _strokeOuter.StrokeWidth = outerStroke.StrokeWidth
                _strokeOuter.Color = ColorMath.WithAlpha(
                    borderOuter,
                    CByte(Math.Max(0, Math.Min(255, CInt(Math.Round(borderOuter.Alpha * (0.72F + outerStrength * 0.56F)))))))

                canvas.DrawRoundRect(
                    outerStroke.Rect,
                    outerStroke.Radius,
                    outerStroke.Radius,
                    _strokeOuter)
            End If

            If borderInner.Alpha = 0 Then Return

            Dim innerStroke As MASVisualPrimitivesPainter.StrokeRoundRectInfo =
                MASVisualPrimitivesPainter.BuildStrokeRoundRectInfo(
                    baseRect:=baseRect,
                    baseRadius:=radius,
                    dpi:=dpi,
                    edgeInsetPx:=SurfaceTokens.AcrylicInnerBorderInsetDip * dpi,
                    strokeWidthPx:=strokePx)

            If innerStroke.HasValue Then
                _strokeInner.IsAntialias = True
                _strokeInner.StrokeWidth = innerStroke.StrokeWidth
                _strokeInner.Color = ColorMath.WithAlpha(
                    borderInner,
                    CByte(Math.Max(0, Math.Min(255, CInt(Math.Round(borderInner.Alpha * (0.72F + innerStrength * 0.56F)))))))

                canvas.DrawRoundRect(
                    innerStroke.Rect,
                    innerStroke.Radius,
                    innerStroke.Radius,
                    _strokeInner)
            End If

        End Sub

#End Region

End Class

End Namespace
