Option Strict On
Option Explicit On

Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Platform default material body. It draws only the themed surface fill and subtle internal wash.
    ''' Outer visual composition is handled by its owning systems.
    ''' </summary>
    Friend NotInheritable Class NexamasUIDefaultSurfaceMaterial
        Implements IMASSurfaceMaterial

        Public ReadOnly Property Id As String Implements IMASSurfaceMaterial.Id
            Get
                Return MASSurfaceMaterialIds.PlatformDefault
            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 palette As MASSurfaceMaterialPalette =
                MASSurfaceMaterialThemeResolver.Resolve(Me.Id, context)

            Dim rr As SKRoundRect = MASSurfaceMaterialPaintUtils.RoundRect(rectPx, radiusPx)

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

                canvas.DrawRoundRect(rr, p)
            End Using

            If palette.Glow.Alpha > 0 Then
                Using washShader As SKShader = SKShader.CreateRadialGradient(
                        New SKPoint(rectPx.Left + rectPx.Width * 0.16F, rectPx.Top + rectPx.Height * 0.08F),
                        Math.Max(rectPx.Width, rectPx.Height) * 0.7F,
                        New SKColor() {palette.Glow, MASSurfaceMaterialPaintUtils.WithAlpha(palette.Glow, 0)},
                        New Single() {0.0F, 1.0F},
                        SKShaderTileMode.Clamp),
                      wash As New SKPaint With {
                        .IsAntialias = True,
                        .IsDither = True,
                        .Style = SKPaintStyle.Fill,
                        .Shader = washShader,
                        .BlendMode = SKBlendMode.SoftLight
                      }

                    canvas.DrawRoundRect(rr, wash)
                End Using
            End If
        End Sub
    End Class

End Namespace
