Option Strict On
Option Explicit On

Imports Nexamas.UI.Theming
Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Reusable chrome rendering context for titled platform surfaces.
    ''' It carries the consolidated surface theme profile and the material identity needed
    ''' to derive header/separator chrome without letting component renderers read
    ''' component-specific theme contracts directly.
    ''' </summary>
    Friend NotInheritable Class MASSurfaceChromeContext
        Friend ReadOnly Property ThemeContext As MASThemeContext
        Friend ReadOnly Property SurfaceTheme As MASSurfaceThemeProfile
        Friend ReadOnly Property MaterialId As String
        Friend ReadOnly Property BoundsPx As SKRect
        Friend ReadOnly Property BaseBoundsPx As SKRect
        Friend ReadOnly Property CornerRadiusPx As Single
        Friend ReadOnly Property BorderStrengthLevel As Integer
        Friend ReadOnly Property SurfaceStrengthLevel As Integer
        Friend ReadOnly Property Dpi As Single
        Friend ReadOnly Property SurfaceRole As MASSurfaceRole
        Friend ReadOnly Property VisualState As MASSurfaceVisualState

        Friend Sub New(themeContext As MASThemeContext,
                       surfaceTheme As MASSurfaceThemeProfile,
                       materialId As String,
                       boundsPx As SKRect,
                       baseBoundsPx As SKRect,
                       cornerRadiusPx As Single,
                       borderStrengthLevel As Integer,
                       surfaceStrengthLevel As Integer,
                       dpi As Single,
                       Optional surfaceRole As MASSurfaceRole = MASSurfaceRole.Unknown,
                       Optional visualState As MASSurfaceVisualState = MASSurfaceVisualState.Normal)

            Me.ThemeContext = themeContext
            Me.SurfaceTheme = If(surfaceTheme, MASSurfaceThemeProfileResolver.Resolve(themeContext))
            Me.MaterialId = NormalizeMaterialId(materialId)
            Me.BoundsPx = boundsPx
            Me.BaseBoundsPx = baseBoundsPx
            Me.CornerRadiusPx = SafeRadius(cornerRadiusPx)
            Me.BorderStrengthLevel = ClampLevel(borderStrengthLevel)
            Me.SurfaceStrengthLevel = ClampLevel(surfaceStrengthLevel)
            Me.Dpi = SafeDpi(dpi, themeContext)
            Me.SurfaceRole = surfaceRole
            Me.VisualState = visualState
        End Sub


        Private Shared Function NormalizeMaterialId(value As String) As String
            Dim key As String = MASSurfaceTreatmentMaterialKeyNormalizer.NormalizeForStorage(value)
            If key.Length = 0 Then Return MASSurfaceMaterialIds.Auto
            Return key
        End Function

        Private Shared Function ClampLevel(value As Integer) As Integer
            If value < 1 Then Return 1
            If value > 6 Then Return 6
            Return value
        End Function

        Private Shared Function SafeRadius(value As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) OrElse value < 0.0F Then Return 0.0F
            Return value
        End Function

        Private Shared Function SafeDpi(value As Single,
                                        themeContext As MASThemeContext) As Single
            Dim resolved As Single = value
            If themeContext IsNot Nothing AndAlso themeContext.Dpi > 0.0F Then resolved = themeContext.Dpi
            If resolved <= 0.0F OrElse Single.IsNaN(resolved) OrElse Single.IsInfinity(resolved) Then Return 1.0F
            Return resolved
        End Function
    End Class

End Namespace
