Option Strict On
Option Explicit On

Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Native WarmPaper surface material. It remains inside the official material registry,
    ''' but preserves its own paper palette instead of being recolored into a generic theme fill.
    ''' </summary>
    Friend NotInheritable Class MASWarmPaperNativeSurfaceMaterial
        Implements IMASSurfaceMaterial

        Public ReadOnly Property Id As String Implements IMASSurfaceMaterial.Id
            Get
                Return MASSurfaceMaterialIds.WarmPaperNative
            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 paperTop As SKColor = New SKColor(255, 251, 240, CByte(242 + CInt(strength * 8)))
            Dim paperMid As SKColor = New SKColor(249, 241, 223, CByte(238 + CInt(strength * 8)))
            Dim paperBottom As SKColor = New SKColor(239, 225, 202, CByte(236 + CInt(strength * 8)))

            Using shader As SKShader = SKShader.CreateLinearGradient(
                    New SKPoint(rectPx.Left, rectPx.Top),
                    New SKPoint(rectPx.Left, rectPx.Bottom),
                    New SKColor() {paperTop, paperMid, paperBottom},
                    New Single() {0.0F, 0.55F, 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 grain As New SKPaint With {
                .IsAntialias = True,
                .Style = SKPaintStyle.Stroke,
                .StrokeWidth = Math.Max(1.0F, context.Dpi),
                .Color = New SKColor(140, 108, 64, CByte(14 + CInt(strength * 8)))
            }
                Dim y As Single = rectPx.Top + 7.0F * context.Dpi
                Dim stepPx As Single = Math.Max(9.0F * context.Dpi, rectPx.Height / 18.0F)

                While y < rectPx.Bottom
                    canvas.DrawLine(
                        rectPx.Left + 12.0F * context.Dpi,
                        y,
                        rectPx.Right - 12.0F * context.Dpi,
                        y + 0.8F * context.Dpi,
                        grain)

                    y += stepPx
                End While
            End Using

            canvas.Restore()
        End Sub

    End Class

End Namespace
