Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.General
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Reusable low-level painter for titled surface header chrome.
    ''' It paints only the header material band, gloss, and separator. Component shells
    ''' keep ownership of layout, title strings, icon kind, hit-testing, and buttons.
    ''' </summary>
    Friend NotInheritable Class MASSurfaceHeaderChromePainter

        Private Sub New()
        End Sub

        Friend Shared Sub DrawBackground(canvas As SKCanvas,
                                 headerRectPx As SKRect,
                                 baseRectPx As SKRect,
                                 cornerRadiusPx As Single,
                                 plan As MASSurfaceHeaderChromePlan,
                                 shouldDrawGloss As Boolean)

            If canvas Is Nothing OrElse headerRectPx.IsEmpty Then Return

            Using sh As SKShader = SKShader.CreateLinearGradient(
                New SKPoint(headerRectPx.Left, headerRectPx.Top),
                New SKPoint(headerRectPx.Left, headerRectPx.Bottom),
                New SKColor() {plan.HeaderTop, plan.HeaderMid, plan.HeaderBottom},
                New Single() {0.0F, 0.5F, 1.0F},
                SKShaderTileMode.Clamp),
                p As New SKPaint With {
                    .IsAntialias = True,
                    .IsDither = True,
                    .Style = SKPaintStyle.Fill,
                    .Shader = sh
                }

                MASVisualPrimitivesPainter.WithClipRoundRect(canvas, baseRectPx, cornerRadiusPx,
                    Sub()
                        canvas.DrawRect(headerRectPx, p)
                    End Sub)
            End Using

            If shouldDrawGloss Then
                DrawGloss(canvas, headerRectPx, baseRectPx, cornerRadiusPx, plan.HeaderSheen)
            End If
        End Sub

        Friend Shared Sub DrawSeparator(canvas As SKCanvas,
                                        headerRectPx As SKRect,
                                        baseRectPx As SKRect,
                                        cornerRadiusPx As Single,
                                        dpi As Single,
                                        plan As MASSurfaceHeaderChromePlan)

            If canvas Is Nothing OrElse headerRectPx.IsEmpty Then Return

            Dim y As Single = PixelSnap.Snap(headerRectPx.Bottom - 0.5F, 0.5F)

            Using p As New SKPaint With {
                .IsAntialias = True,
                .IsDither = True,
                .Style = SKPaintStyle.Stroke,
                .StrokeWidth = Math.Max(1.0F, PixelSnap.SafeDpi(dpi)),
                .StrokeCap = SKStrokeCap.Square,
                .Color = plan.HeaderSeparator
            }
                MASVisualPrimitivesPainter.WithClipRoundRect(canvas, baseRectPx, cornerRadiusPx,
                    Sub()
                        canvas.DrawLine(headerRectPx.Left, y, headerRectPx.Right, y, p)
                    End Sub)
            End Using
        End Sub

        Private Shared Sub DrawGloss(canvas As SKCanvas,
                                     headerRectPx As SKRect,
                                     baseRectPx As SKRect,
                                     cornerRadiusPx As Single,
                                     sheenColor As SKColor)

            Dim h As Single = Math.Max(1.0F, headerRectPx.Height * 0.42F)

            Dim glossRect As SKRect = PixelSnap.SnapRectHalf(
                New SKRect(headerRectPx.Left, headerRectPx.Top, headerRectPx.Right, headerRectPx.Top + h))

            Dim c As SKColor = sheenColor

            Using sh As SKShader = SKShader.CreateLinearGradient(
                New SKPoint(glossRect.Left, glossRect.Top),
                New SKPoint(glossRect.Left, glossRect.Bottom),
                New SKColor() {
                    c,
                    c.WithAlpha(CByte(Math.Max(0, CInt(c.Alpha * 0.35F)))),
                    SKColors.Transparent
                },
                New Single() {0.0F, 0.55F, 1.0F},
                SKShaderTileMode.Clamp),
                p As New SKPaint With {
                    .IsAntialias = True,
                    .IsDither = True,
                    .Style = SKPaintStyle.Fill,
                    .Shader = sh,
                    .BlendMode = SKBlendMode.SoftLight
                }

                MASVisualPrimitivesPainter.WithClipRoundRect(canvas, baseRectPx, cornerRadiusPx,
                    Sub()
                        canvas.DrawRect(glossRect, p)
                    End Sub)
            End Using
        End Sub
    End Class

End Namespace
