' SSOT-V2 Signature: This class/file follows the SSOT-V2 architecture.
Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Calm crystal-ice material renderer.
    ''' Previously consumed by crystal-ice surface consumers through the shared visual primitives path.
    ''' Visual output is intentionally preserved 1:1; this refactor only extracts and
    ''' renames the material layer away from element-oriented naming.
    ''' </summary>
    Friend NotInheritable Class MASCrystalIceSurfacePainter
        Implements IDisposable

#Region "Fields"

        Private _disposed As Boolean

#End Region

#Region "Dispose"

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

#End Region

#Region "Public API"

        Friend Sub DrawCrystalIceBody(
            canvas As SKCanvas,
            baseRect As SKRect,
            radius As Single,
            dpi As Single,
            fam As CommonEnums.SurfaceFamily,
            topCol As SKColor,
            midCol As SKColor,
            botCol As SKColor,
            wash As SKColor)

            If _disposed Then Return

            DrawCrystalIceMaterialBody(
                canvas:=canvas,
                baseRect:=baseRect,
                radius:=radius,
                dpi:=dpi,
                topCol:=topCol,
                midCol:=midCol,
                botCol:=botCol,
                wash:=wash)
        End Sub

        Friend Shared Sub DrawCrystalIceMaterialBody(
            canvas As SKCanvas,
            baseRect As SKRect,
            radius As Single,
            dpi As Single,
            topCol As SKColor,
            midCol As SKColor,
            botCol As SKColor,
            wash As SKColor)

            If canvas Is Nothing Then Return
            If baseRect.Width <= 1 OrElse baseRect.Height <= 1 Then Return

            dpi = PixelSnap.SafeDpi(dpi)
            radius = PixelSnap.SafeRadius(radius)

            '===========================================================
            ' 1) Calm body gradient
            '===========================================================
            Dim topLift As SKColor =
                ColorMath.MixLinear(topCol, midCol, 0.55F)

            Dim midBlend As SKColor =
                ColorMath.MixLinear(midCol, botCol, 0.1F)

            Dim botShade As SKColor =
                ColorMath.MixLinear(botCol, midCol, 0.12F)

            Using grad As SKShader = SKShader.CreateLinearGradient(
                New SKPoint(baseRect.MidX, baseRect.Top),
                New SKPoint(baseRect.MidX, baseRect.Bottom),
                {
                    topLift,
                    midBlend,
                    botShade
                },
                {0.0F, 0.46F, 1.0F},
                SKShaderTileMode.Clamp
            )
                Using p As New SKPaint With {
                    .IsAntialias = True,
                    .IsDither = True,
                    .Shader = grad,
                    .Style = SKPaintStyle.Fill
                }
                    canvas.DrawRoundRect(baseRect, radius, radius, p)
                End Using
            End Using

            '===========================================================
            ' 2) Subtle wash
            '===========================================================
            Dim washCol As SKColor =
                ColorMath.MixLinear(wash, midCol, 0.34F).WithAlpha(5)

            MASVisualPrimitivesPainter.WithClipRoundRect(canvas, baseRect, radius,
                Sub()
                    Using p As New SKPaint With {
                        .IsAntialias = True,
                        .IsDither = True,
                        .Color = washCol,
                        .Style = SKPaintStyle.Fill,
                        .BlendMode = SKBlendMode.SrcOver
                    }
                        canvas.DrawRect(baseRect, p)
                    End Using
                End Sub)

            '===========================================================
            ' 3) Ultra-soft top veil
            '===========================================================
            Dim topBandH As Single = Math.Max(3.0F * dpi, baseRect.Height * 0.22F)

            Dim veilRect As New SKRect(
                baseRect.Left,
                baseRect.Top,
                baseRect.Right,
                Math.Min(baseRect.Bottom, baseRect.Top + topBandH)
            )

            If veilRect.Height > 1.0F Then
                Dim veilTop As SKColor =
                    ColorMath.MixLinear(topCol, midCol, 0.42F).WithAlpha(5)

                Dim veilBot As SKColor =
                    ColorMath.WithAlpha(veilTop, 0)

                MASVisualPrimitivesPainter.WithClipRoundRect(canvas, baseRect, radius,
                    Sub()
                        Using sh As SKShader = SKShader.CreateLinearGradient(
                            New SKPoint(veilRect.Left, veilRect.Top),
                            New SKPoint(veilRect.Left, veilRect.Bottom),
                            {veilTop, veilBot},
                            {0.0F, 1.0F},
                            SKShaderTileMode.Clamp
                        )
                            Using p As New SKPaint With {
                                .IsAntialias = True,
                                .IsDither = True,
                                .Shader = sh,
                                .Style = SKPaintStyle.Fill,
                                .BlendMode = SKBlendMode.SrcOver
                            }
                                canvas.DrawRect(veilRect, p)
                            End Using
                        End Using
                    End Sub)
            End If
        End Sub


#End Region

    End Class

End Namespace