Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-owned chrome policy for MASTopBarComponent. It owns title/material hygiene,
    ''' DPI and menu-input bounds normalization, standard window-command mapping, and the
    ''' boundary between the public top-bar gateway and native window adapter plumbing.
    ''' </summary>
    Friend NotInheritable Class MASTopBarChromePolicy
        Private Sub New()
        End Sub

        Friend Const MenuOwnerKey As String = "MAS.TopBar.Menu"

        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 NormalizeSurfaceMaterial(value As String) As String
            Return MASSurfaceTreatmentMaterialKeyNormalizer.NormalizeForStorage(value)
        End Function

        Friend Shared Function NormalizeDpi(value As Single) As Single
            Return PixelSnap.SafeDpi(value)
        End Function

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

        Friend Shared Function BuildSafeFloatInputBounds(barRectPx As SKRect,
                                                         dpi As Single) As SKRect
            Dim safeDpi As Single = NormalizeDpi(dpi)
            If barRectPx.Width <= TopBarTokens.MinimumUsablePx OrElse barRectPx.Height <= TopBarTokens.MinimumUsablePx Then Return SKRect.Empty

            Dim left As Single = Math.Min(0.0F, barRectPx.Left)
            Dim top As Single = Math.Min(0.0F, barRectPx.Top)
            Dim right As Single = Math.Max(barRectPx.Right, barRectPx.Left + (TopBarTokens.FloatBackdropExtentDip * safeDpi))
            Dim bottom As Single = Math.Max(barRectPx.Bottom, barRectPx.Top + (TopBarTokens.FloatBackdropExtentDip * safeDpi))
            Return New SKRect(left, top, right, bottom)
        End Function

        Friend Shared Function ResolveStandardWindowCommand(commandId As String) As Nullable(Of MASTopBarWindowCommand)
            Dim key As String = If(commandId, String.Empty).Trim()
            If key.Length = 0 Then Return Nothing

            If String.Equals(key, "window.minimize", StringComparison.OrdinalIgnoreCase) Then
                Return MASTopBarWindowCommand.Minimize
            End If

            If String.Equals(key, "window.toggle_max_restore", StringComparison.OrdinalIgnoreCase) Then
                Return MASTopBarWindowCommand.ToggleMaximizeRestore
            End If

            If String.Equals(key, "window.close", StringComparison.OrdinalIgnoreCase) Then
                Return MASTopBarWindowCommand.CloseWindow
            End If

            Return Nothing
        End Function

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

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

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

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

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

End Namespace
