Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Controls

    ''' <summary>
    ''' Friend-owned visual surface policy for MASSidePanel. It owns drawer-panel title
    ''' hygiene, content insets, close-button geometry, relative child placement hygiene,
    ''' and the separation between side-panel rendering and MASDrawer/FloatRuntime lifecycle.
    ''' </summary>
    Friend NotInheritable Class MASSidePanelSurfacePolicy
        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
            Dim normalized As String = NormalizeTitle(value)
            If normalized.Length = 0 Then Return DrawerTokens.DefaultTitle
            Return normalized
        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 ResolveContentInset() As MASLayoutInset
            Return New MASLayoutInset(DrawerTokens.PaddingLeftDip,
                                      DrawerTokens.HeaderHeightDip + DrawerTokens.PaddingTopDip,
                                      DrawerTokens.PaddingRightDip,
                                      DrawerTokens.PaddingBottomDip)
        End Function

        Friend Shared Function ResolveContentBounds(bounds As SKRect) As SKRect
            If bounds.Width <= 1.0F OrElse bounds.Height <= 1.0F Then Return SKRect.Empty
            Dim inset As MASLayoutInset = ResolveContentInset()
            Dim r As New SKRect(bounds.Left + inset.Left,
                                bounds.Top + inset.Top,
                                bounds.Right - inset.Right,
                                bounds.Bottom - inset.Bottom)
            If r.Width <= 1.0F OrElse r.Height <= 1.0F Then Return SKRect.Empty
            Return r
        End Function

        Friend Shared Function ResolveCloseButtonBoundsPx(header As SKRect,
                                                          dpi As Single) As SKRect
            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)
            If header.Width <= 1.0F OrElse header.Height <= 1.0F Then Return SKRect.Empty
            Dim size As Single = DrawerTokens.CloseButtonSizeDip * safeDpi
            Dim right As Single = header.Right - DrawerTokens.CloseButtonRightInsetDip * safeDpi
            Dim top As Single = header.MidY - size / 2.0F
            Return PixelSnap.SnapRect(New SKRect(right - size, top, right, top + size), safeDpi)
        End Function

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

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

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

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

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

End Namespace
