Option Strict On
Option Explicit On

Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    Friend NotInheritable Class MASFrostedGlassSurfaceMaterial
        Implements IMASSurfaceMaterial

        Public ReadOnly Property Id As String Implements IMASSurfaceMaterial.Id
            Get
                Return MASSurfaceMaterialIds.FrostedGlass
            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.Right, 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

            Dim bandTop As Single = rectPx.Top + rectPx.Height * 0.12F
            Dim bandBottom As Single = rectPx.Top + rectPx.Height * 0.34F

            Using bandShader As SKShader = SKShader.CreateLinearGradient(
                    New SKPoint(rectPx.Left, bandTop),
                    New SKPoint(rectPx.Right, bandBottom),
                    New SKColor() {palette.Glow, MASSurfaceMaterialPaintUtils.WithAlpha(palette.Glow, 0)},
                    Nothing,
                    SKShaderTileMode.Clamp),
                  bandPaint As New SKPaint With {.IsAntialias = True, .IsDither = True, .Style = SKPaintStyle.Fill, .Shader = bandShader}

                canvas.Save()
                canvas.ClipRoundRect(rr, SKClipOperation.Intersect, True)
                canvas.DrawOval(
                    New SKRect(rectPx.Left - rectPx.Width * 0.2F,
                               bandTop - rectPx.Height * 0.25F,
                               rectPx.Right + rectPx.Width * 0.3F,
                               bandBottom + rectPx.Height * 0.3F),
                    bandPaint)
                canvas.Restore()
            End Using
        End Sub

    End Class

End Namespace