Option Strict On
Option Explicit On

Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Native CrystalIce surface material. It is a first-class registered material variant,
    ''' not a registry escape path. The recipe preserves its own crystal palette while
    ''' still participating in frame, shadow, and policy resolution.
    ''' </summary>
    Friend NotInheritable Class MASCrystalIceNativeSurfaceMaterial
        Implements IMASSurfaceMaterial

        Public ReadOnly Property Id As String Implements IMASSurfaceMaterial.Id
            Get
                Return MASSurfaceMaterialIds.CrystalIceNative
            End Get
        End Property

        Friend Sub Draw(canvas As SKCanvas,
                        context As MASSurfaceMaterialContext) Implements IMASSurfaceMaterial.Draw

            If canvas Is Nothing OrElse context Is Nothing Then Return

            Dim rectPx As SKRect = context.BoundsPx
            Dim radiusPx As Single = context.CornerRadiusPx
            If rectPx.Width <= 0.5F OrElse rectPx.Height <= 0.5F Then Return

            Dim rr As SKRoundRect = MASSurfaceMaterialPaintUtils.RoundRect(rectPx, radiusPx)
            Dim strength As Single = MASSurfaceMaterialPaintUtils.LevelFactor(context.SurfaceStrengthLevel, 0.0F, 1.0F)

            Dim top As SKColor = New SKColor(236, 250, 255, CByte(222 + CInt(strength * 12)))
            Dim mid As SKColor = New SKColor(205, 234, 255, CByte(204 + CInt(strength * 8)))
            Dim bottom As SKColor = New SKColor(170, 205, 236, CByte(210 + CInt(strength * 10)))

            Using shader As SKShader = SKShader.CreateLinearGradient(
                    New SKPoint(rectPx.Left, rectPx.Top),
                    New SKPoint(rectPx.Left, rectPx.Bottom),
                    New SKColor() {top, mid, bottom},
                    New Single() {0.0F, 0.42F, 1.0F},
                    SKShaderTileMode.Clamp),
                  p As New SKPaint With {
                    .IsAntialias = True,
                    .IsDither = True,
                    .Style = SKPaintStyle.Fill,
                    .Shader = shader
                  }

                canvas.DrawRoundRect(rr, p)
            End Using

            canvas.Save()
            canvas.ClipRoundRect(rr, SKClipOperation.Intersect, True)

            Using linePaint As New SKPaint With {
                .IsAntialias = True,
                .Style = SKPaintStyle.Stroke,
                .StrokeWidth = Math.Max(1.0F, context.Dpi),
                .Color = New SKColor(255, 255, 255, CByte(34 + CInt(strength * 14)))
            }
                Dim stepPx As Single = Math.Max(10.0F * context.Dpi, rectPx.Width / 12.0F)
                Dim x As Single = rectPx.Left - rectPx.Height

                While x < rectPx.Right + rectPx.Height
                    canvas.DrawLine(x, rectPx.Bottom, x + rectPx.Height, rectPx.Top, linePaint)
                    x += stepPx
                End While
            End Using

            canvas.Restore()
        End Sub

    End Class

End Namespace
