Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Controls

    ''' <summary>
    ''' Friend-owned surface policy for MASGroupBox. It centralizes title hygiene,
    ''' surface/material normalization, title-plate lift bounds, header/content geometry,
    ''' child slot bounds, and the surface-only boundary without opening a wrapper or a
    ''' second container surface route.
    ''' </summary>
    Friend NotInheritable Class MASGroupBoxSurfacePolicy
        Private Sub New()
        End Sub

        Friend Shared Function NormalizeTitle(value As String) As String
            Dim text As String = If(value, String.Empty).Trim()
            If text.Length > 96 Then text = text.Substring(0, 96)
            Return text
        End Function

        Friend Shared Function ResolveRenderTitle(value As String) As String
            Return NormalizeTitle(value)
        End Function

        Friend Shared Function NormalizeSurfaceMaterial(value As String) As String
            Return MASSurfaceTreatmentMaterialKeyNormalizer.NormalizeForStorage(value)
        End Function

        Friend Shared Function NormalizeSurfaceSource(value As MASGroupBoxSurfaceSource) As MASGroupBoxSurfaceSource
            Select Case value
                Case MASGroupBoxSurfaceSource.Plain, MASGroupBoxSurfaceSource.TransparentFrame
                    Return value
                Case Else
                    Return MASGroupBoxSurfaceSource.Acrylic
            End Select
        End Function

        Friend Shared Function NormalizeBorderRecipe(value As MASGroupBoxBorderRecipe) As MASGroupBoxBorderRecipe
            Select Case value
                Case MASGroupBoxBorderRecipe.AcrylicSurface
                    Return MASGroupBoxBorderRecipe.AcrylicSurface
                Case Else
                    Return MASGroupBoxBorderRecipe.ListSurface
            End Select
        End Function

        Friend Shared Function NormalizeBorderStrength(value As MASGroupBoxBorderStrength) As MASGroupBoxBorderStrength
            Select Case value
                Case MASGroupBoxBorderStrength.Level1, MASGroupBoxBorderStrength.Level2, MASGroupBoxBorderStrength.Level3,
                     MASGroupBoxBorderStrength.Level4, MASGroupBoxBorderStrength.Level5, MASGroupBoxBorderStrength.Level6
                    Return value
                Case Else
                    Return MASGroupBoxBorderStrength.Level5
            End Select
        End Function

        Friend Shared Function NormalizePadding(value As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) OrElse value < 0.0F Then Return 0.0F
            If value > 96.0F Then Return 96.0F
            Return value
        End Function

        Friend Shared Function NormalizeTitleLift(value As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) Then Return GroupBoxTokens.FloatingTitleDefaultLift
            If value < 0.0F Then Return 0.0F
            If value > 1.0F Then Return 1.0F
            Return value
        End Function

        Friend Shared Function NormalizeRelativeBounds(value As SKRect) As SKRect
            If Single.IsNaN(value.Left) OrElse Single.IsNaN(value.Top) OrElse
               Single.IsNaN(value.Right) OrElse Single.IsNaN(value.Bottom) OrElse
               Single.IsInfinity(value.Left) OrElse Single.IsInfinity(value.Top) OrElse
               Single.IsInfinity(value.Right) OrElse Single.IsInfinity(value.Bottom) Then
                Return SKRect.Empty
            End If

            Dim left As Single = Math.Min(value.Left, value.Right)
            Dim top As Single = Math.Min(value.Top, value.Bottom)
            Dim right As Single = Math.Max(value.Left, value.Right)
            Dim bottom As Single = Math.Max(value.Top, value.Bottom)
            If right <= left OrElse bottom <= top Then Return SKRect.Empty
            Return New SKRect(left, top, right, bottom)
        End Function

        Friend Shared Function ResolveHeaderHeight(title As String,
                                                   reserveHeaderWhenEmpty As Boolean) As Single
            If reserveHeaderWhenEmpty Then Return GroupBoxTokens.HeaderHeight
            If NormalizeTitle(title).Length = 0 Then Return 0.0F
            Return GroupBoxTokens.HeaderHeight
        End Function

        Friend Shared Function ResolveContentBounds(bounds As SKRect,
                                                    headerHeight As Single,
                                                    paddingLeft As Single,
                                                    paddingTop As Single,
                                                    paddingRight As Single,
                                                    paddingBottom As Single) As SKRect
            If bounds.Width <= 1.0F OrElse bounds.Height <= 1.0F Then Return SKRect.Empty

            Dim r As New SKRect(
                bounds.Left + NormalizePadding(paddingLeft),
                bounds.Top + Math.Max(0.0F, headerHeight) + NormalizePadding(paddingTop),
                bounds.Right - NormalizePadding(paddingRight),
                bounds.Bottom - NormalizePadding(paddingBottom))

            If r.Width <= 1.0F OrElse r.Height <= 1.0F Then Return SKRect.Empty
            Return r
        End Function

        Friend Shared ReadOnly Property HasSurfaceMaterialRoute As Boolean
            Get
                Return True
            End Get
        End Property

        Friend Shared ReadOnly Property HasChildSlotPolicy As Boolean
            Get
                Return True
            End Get
        End Property

        Friend Shared ReadOnly Property HasTitlePlatePolicy As Boolean
            Get
                Return True
            End Get
        End Property

        Friend Shared ReadOnly Property HasNoNativeGroupBoxWrapper As Boolean
            Get
                Return True
            End Get
        End Property

        Friend Shared ReadOnly Property HasNoParallelSurfaceRoute As Boolean
            Get
                Return True
            End Get
        End Property
    End Class

End Namespace
