Option Strict On
Option Explicit On

Imports System
Imports System.Globalization
Imports System.Windows.Forms
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Values
Imports Nexamas.UI.General
Imports SkiaSharp

Namespace Nexamas.UI.Controls

    ''' <summary>
    ''' Friend-owned disclosure policy for MASExpander. It owns title hygiene, RTL header
    ''' layout facts, keyboard expand/collapse semantics, child bounds hygiene, header hit
    ''' geometry, and no-local-clock disclosure boundaries while keeping section orchestration in
    ''' MASAccordion and avoiding any native Panel/UserControl wrapper path. Official
    ''' MASMotionSystem consumption is the only permitted future disclosure motion route.
    ''' </summary>
    Friend NotInheritable Class MASExpanderDisclosurePolicy
        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 = 0 Then Return String.Empty
            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 ExpanderTokens.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 ResolveKeyboardExpandedState(keyCode As Keys,
                                                            currentExpanded As Boolean,
                                                            isRtl As Boolean) As Nullable(Of Boolean)
            Select Case keyCode
                Case Keys.Space, Keys.Enter
                    Return Not currentExpanded
                Case Keys.Up
                    Return False
                Case Keys.Down
                    Return True
                Case Keys.Left
                    Return isRtl
                Case Keys.Right
                    Return Not isRtl
                Case Else
                    Return Nothing
            End Select
        End Function

        Friend Shared Function ResolveRightToLeft() As Boolean
            Return Nexamas.UI.Localization.MASLocalization.IsCurrentRightToLeft()
        End Function

        Friend Shared Function ResolveHeaderBoundsPx(bounds As SKRect,
                                                     dpi As Single) As SKRect
            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)
            Return New SKRect(bounds.Left,
                              bounds.Top,
                              bounds.Right,
                              Math.Min(bounds.Bottom, bounds.Top + ExpanderTokens.HeaderHeightDip * safeDpi))
        End Function

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

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

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

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

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

End Namespace
