Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Cool dialog shell surface renderer (MASGlassPrime – Flat Polished Edition).
    ''' نسخة زجاجية فلات، مصقولة، باردة جداً.
    ''' </summary>
    Friend NotInheritable Class MASDialogShellSurfacePainter
        Implements IDisposable

        Private _disposed As Boolean
        Private ReadOnly _shadow As New MASShadowPainter()

        Friend Sub Draw(canvas As SKCanvas,
                        rectPx As SKRect,
                        dpi As Single,
                        Optional cornerRadiusDip As Single = 11.0F)

            If _disposed Then Return
            If canvas Is Nothing Then Return
            If rectPx.IsEmpty OrElse rectPx.Width <= 1.0F OrElse rectPx.Height <= 1.0F Then Return

            dpi = PixelSnap.SafeDpi(dpi)
            rectPx = PixelSnap.SnapRectHalf(rectPx)

            Dim rr As Single = Math.Max(4.0F, cornerRadiusDip * dpi)

            Dim shadowRect As New SKRect(
                rectPx.Left,
                rectPx.Top + (1.0F * dpi),
                rectPx.Right,
                rectPx.Bottom + (2.5F * dpi))

            _shadow.DrawShadow(
                canvas:=canvas,
                r:=shadowRect,
                radiusPx:=rr + (1.0F * dpi),
                dpi:=dpi,
                shadowBase:=New SKColor(10, 20, 32),
                spec:=MASAmbientSurfaceShadowProfiles.ResolveCoolDialogShell())
            DrawBaseBody(canvas, rectPx, rr)
            DrawTopBloom(canvas, rectPx, rr, dpi)
            DrawInnerWash(canvas, rectPx, rr)
            DrawBottomSettle(canvas, rectPx, rr)
            DrawBorders(canvas, rectPx, rr, dpi)
        End Sub

#Region "Layers"

        Private Shared Sub DrawBaseBody(canvas As SKCanvas,
                                        rectPx As SKRect,
                                        radiusPx As Single)

            ' فلات زجاجي: شبه موحّد، بارد، مصقول
            Dim cTop As New SKColor(245, 250, 255, 210)
            Dim cBot As New SKColor(230, 240, 250, 200)

            Using sh As SKShader = SKShader.CreateLinearGradient(
                New SKPoint(rectPx.Left, rectPx.Top),
                New SKPoint(rectPx.Left, rectPx.Bottom),
                New SKColor() {cTop, cBot},
                New Single() {0.0F, 1.0F},
                SKShaderTileMode.Clamp
            )
                Using p As New SKPaint With {
                    .IsAntialias = True,
                    .IsDither = True,
                    .Style = SKPaintStyle.Fill,
                    .Shader = sh
                }
                    canvas.DrawRoundRect(rectPx, radiusPx, radiusPx, p)
                End Using
            End Using
        End Sub

        Private Shared Sub DrawTopBloom(canvas As SKCanvas,
                                        rectPx As SKRect,
                                        radiusPx As Single,
                                        dpi As Single)

            ' Bloom خفيف جداً – فقط لمعان مصقول
            Dim bloomHeight As Single = Math.Max(10.0F * dpi, rectPx.Height * 0.18F)

            Dim bloomRect As New SKRect(
                rectPx.Left,
                rectPx.Top,
                rectPx.Right,
                rectPx.Top + bloomHeight
            )

            Using sh As SKShader = SKShader.CreateLinearGradient(
                New SKPoint(bloomRect.Left, bloomRect.Top),
                New SKPoint(bloomRect.Left, bloomRect.Bottom),
                New SKColor() {
                    New SKColor(255, 255, 255, 70),
                    New SKColor(255, 255, 255, 12),
                    SKColors.Transparent
                },
                New Single() {0.0F, 0.35F, 1.0F},
                SKShaderTileMode.Clamp
            )
                Using p As New SKPaint With {
                    .IsAntialias = True,
                    .IsDither = True,
                    .Style = SKPaintStyle.Fill,
                    .Shader = sh,
                    .BlendMode = SKBlendMode.Screen
                }
                    canvas.Save()
                    canvas.ClipRoundRect(New SKRoundRect(rectPx, radiusPx, radiusPx), antialias:=True)
                    canvas.DrawRect(bloomRect, p)
                    canvas.Restore()
                End Using
            End Using
        End Sub

        Private Shared Sub DrawInnerWash(canvas As SKCanvas,
                                         rectPx As SKRect,
                                         radiusPx As Single)

            ' Wash خفيف جداً – فقط لإعطاء إحساس الزجاج وليس البلاستيك
            Dim inner As SKRect = InsetRect(rectPx, 1.0F, 1.0F)

            Using sh As SKShader = SKShader.CreateLinearGradient(
                New SKPoint(inner.Left, inner.Top),
                New SKPoint(inner.Left, inner.Bottom),
                New SKColor() {
                    New SKColor(250, 252, 255, 40),
                    New SKColor(230, 240, 250, 20),
                    New SKColor(215, 228, 240, 12)
                },
                New Single() {0.0F, 0.55F, 1.0F},
                SKShaderTileMode.Clamp
            )
                Using p As New SKPaint With {
                    .IsAntialias = True,
                    .IsDither = True,
                    .Style = SKPaintStyle.Fill,
                    .Shader = sh,
                    .BlendMode = SKBlendMode.SoftLight
                }
                    canvas.DrawRoundRect(inner,
                                         Math.Max(0.0F, radiusPx - 1.0F),
                                         Math.Max(0.0F, radiusPx - 1.0F),
                                         p)
                End Using
            End Using
        End Sub

        Private Shared Sub DrawBottomSettle(canvas As SKCanvas,
                                            rectPx As SKRect,
                                            radiusPx As Single)

            ' Depth خفيف جداً – فقط لإعطاء قاعدة زجاجية
            Dim settleRect As New SKRect(
                rectPx.Left,
                rectPx.Top + (rectPx.Height * 0.6F),
                rectPx.Right,
                rectPx.Bottom
            )

            Using sh As SKShader = SKShader.CreateLinearGradient(
                New SKPoint(settleRect.Left, settleRect.Top),
                New SKPoint(settleRect.Left, settleRect.Bottom),
                New SKColor() {
                    SKColors.Transparent,
                    New SKColor(160, 180, 200, 18),
                    New SKColor(130, 150, 170, 22)
                },
                New Single() {0.0F, 0.55F, 1.0F},
                SKShaderTileMode.Clamp
            )
                Using p As New SKPaint With {
                    .IsAntialias = True,
                    .IsDither = True,
                    .Style = SKPaintStyle.Fill,
                    .Shader = sh,
                    .BlendMode = SKBlendMode.Multiply
                }
                    canvas.Save()
                    canvas.ClipRoundRect(New SKRoundRect(rectPx, radiusPx, radiusPx), antialias:=True)
                    canvas.DrawRect(settleRect, p)
                    canvas.Restore()
                End Using
            End Using
        End Sub

        Private Shared Sub DrawBorders(canvas As SKCanvas,
                                       rectPx As SKRect,
                                       radiusPx As Single,
                                       dpi As Single)

            Dim outerStroke As Single = Math.Max(1.0F, 1.0F * dpi)
            Dim innerStroke As Single = Math.Max(1.0F, 1.0F * dpi)

            ' إطار خارجي فلات، بارد، نظيف
            Using p As New SKPaint With {
                .IsAntialias = True,
                .IsDither = True,
                .Style = SKPaintStyle.Stroke,
                .StrokeWidth = outerStroke,
                .Color = New SKColor(150, 165, 185, 130)
            }
                canvas.DrawRoundRect(rectPx, radiusPx, radiusPx, p)
            End Using

            ' إطار داخلي مصقول – يعطي edge زجاجي
            Dim innerRect As SKRect = InsetRect(rectPx, 1.0F, 1.0F)
            Using p As New SKPaint With {
                .IsAntialias = True,
                .IsDither = True,
                .Style = SKPaintStyle.Stroke,
                .StrokeWidth = innerStroke,
                .Color = New SKColor(255, 255, 255, 90)
            }
                canvas.DrawRoundRect(innerRect,
                                     Math.Max(0.0F, radiusPx - 1.0F),
                                     Math.Max(0.0F, radiusPx - 1.0F),
                                     p)
            End Using

            ' Rim line علوي – فلات جداً
            Dim topLineRect As New SKRect(
                rectPx.Left + 1.0F,
                rectPx.Top + 1.0F,
                rectPx.Right - 1.0F,
                rectPx.Top + Math.Max(2.0F, 2.0F * dpi)
            )

            Using sh As SKShader = SKShader.CreateLinearGradient(
                New SKPoint(topLineRect.Left, topLineRect.Top),
                New SKPoint(topLineRect.Left, topLineRect.Bottom),
                New SKColor() {
                    New SKColor(255, 255, 255, 80),
                    New SKColor(255, 255, 255, 18)
                },
                New Single() {0.0F, 1.0F},
                SKShaderTileMode.Clamp
            )
                Using p As New SKPaint With {
                    .IsAntialias = True,
                    .IsDither = True,
                    .Style = SKPaintStyle.Fill,
                    .Shader = sh
                }
                    canvas.Save()
                    canvas.ClipRoundRect(New SKRoundRect(rectPx, radiusPx, radiusPx), antialias:=True)
                    canvas.DrawRect(topLineRect, p)
                    canvas.Restore()
                End Using
            End Using
        End Sub

#End Region

#Region "Helpers"

        Private Shared Function InsetRect(r As SKRect, dx As Single, dy As Single) As SKRect
            Return New SKRect(r.Left + dx, r.Top + dy, r.Right - dx, r.Bottom - dy)
        End Function

#End Region

        Public Sub Dispose() Implements IDisposable.Dispose
            If _disposed Then Return
            _disposed = True
            SafeDisposeShadow(_shadow)
        End Sub


        Private Shared Sub SafeDisposeShadow(shadow As MASShadowPainter)
            If shadow Is Nothing Then Return

            Try
                shadow.Dispose()
            Catch masCaughtException1 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException1)
            End Try
        End Sub

    End Class

End Namespace
