Option Strict On
Option Explicit On

Imports System
Imports System.Globalization
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-owned policy for MASAppLayout. It centralizes text hygiene,
    ''' responsive breakpoint selection, RTL detection, theme color resolution, and
    ''' explicit ownership boundaries. It deliberately does not own routing, PageHost
    ''' state, public/native child hosting, persistence, native shell composition,
    ''' or mouse-click focus rings. Friend-only slot hosting is governed separately.
    ''' </summary>
    Friend NotInheritable Class MASAppLayoutPolicy
        Private Sub New()
        End Sub

        Friend Shared Function NormalizeTitle(value As String) As String
            Dim text As String = NormalizeText(value)
            If text.Length = 0 Then Return AppLayoutTokens.DefaultTitle
            Return ClampText(text, 64)
        End Function

        Friend Shared Function NormalizeSectionTitle(value As String,
                                                     fallback As String) As String
            Dim text As String = NormalizeText(value)
            If text.Length = 0 Then Return fallback
            Return ClampText(text, 56)
        End Function

        Friend Shared Function NormalizeSectionText(value As String,
                                                    fallback As String) As String
            Dim text As String = NormalizeText(value)
            If text.Length = 0 Then Return fallback
            Return ClampText(text, 140)
        End Function

        Friend Shared Function NormalizeStatusText(value As String) As String
            Dim text As String = NormalizeText(value)
            If text.Length = 0 Then Return AppLayoutTokens.DefaultStatusText
            Return ClampText(text, 110)
        End Function

        Friend Shared Function NormalizeText(value As String) As String
            If value Is Nothing Then Return String.Empty
            Dim text As String = value.Trim()
            If text.Length = 0 Then Return String.Empty
            Do While text.Contains("  ")
                text = text.Replace("  ", " ")
            Loop
            Return text
        End Function

        Friend Shared Function ClampText(value As String,
                                         maxLength As Integer) As String
            Dim text As String = If(value, String.Empty)
            If maxLength <= 0 OrElse text.Length <= maxLength Then Return text
            Return text.Substring(0, Math.Max(0, maxLength - 1)) & "…"
        End Function

        Friend Shared Function ResolveMode(widthDip As Single) As String
            If widthDip < AppLayoutTokens.MediumBreakpointDip Then Return "Compact"
            If widthDip < AppLayoutTokens.WideBreakpointDip Then Return "Balanced"
            Return "Wide"
        End Function

        Friend Shared Function BuildSummary(widthDip As Single,
                                            showNavigation As Boolean,
                                            showDetails As Boolean,
                                            showStatus As Boolean) As String
            Dim mode As String = ResolveMode(widthDip)
            Dim nav As String = If(showNavigation, "nav", "no nav")
            Dim details As String = If(showDetails, "details", "no details")
            Dim status As String = If(showStatus, "status", "no status")
            Return mode & " • " & nav & " • " & details & " • " & status
        End Function

        Friend Shared Function ResolveRightToLeft() As Boolean
            Try
                Return Nexamas.UI.Localization.MASLocalization.IsCurrentRightToLeft()
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowInputBoundary(ex, "MASAppLayoutPolicy.ResolveRightToLeft")
                Return False
            End Try
        End Function

        Friend Shared Function ResolveAccentColor(ctx As MASThemeContext) As SKColor
            If ctx IsNot Nothing AndAlso ctx.BrandTheme IsNot Nothing Then Return ctx.BrandTheme.BrandPrimary
            If ctx IsNot Nothing AndAlso ctx.SemanticTheme IsNot Nothing Then Return ctx.SemanticTheme.Info
            Return New SKColor(64, 132, 246)
        End Function

        Friend Shared Function ResolvePrimaryTextColor(ctx As MASThemeContext) As SKColor
            If ctx IsNot Nothing AndAlso ctx.TextColorTheme IsNot Nothing Then Return ctx.TextColorTheme.PrimaryText.WithAlpha(AppLayoutTokens.TextAlpha)
            Return Nexamas.UI.Values.Color.Text.Primary.WithAlpha(AppLayoutTokens.TextAlpha)
        End Function

        Friend Shared Function ResolveMutedTextColor(ctx As MASThemeContext) As SKColor
            If ctx IsNot Nothing AndAlso ctx.TextColorTheme IsNot Nothing Then Return ctx.TextColorTheme.SecondaryText.WithAlpha(AppLayoutTokens.MutedTextAlpha)
            Return Nexamas.UI.Values.Color.Text.Secondary.WithAlpha(AppLayoutTokens.MutedTextAlpha)
        End Function

        Friend Shared Function HasNoRouterPath() As Boolean
            Return True
        End Function

        Friend Shared Function HasNoPageHostOwnershipPath() As Boolean
            Return True
        End Function

        Friend Shared Function HasRouteOwnershipBoundaryPath() As Boolean
            Dim snapshot As MASRouteShellBoundarySnapshot = MASRouteShellBoundaryPolicy.CreateAppLayoutBoundary()
            Return MASRouteShellBoundaryPolicy.IsSafeClosedBoundary(snapshot, "applayout", "AppLayout.RoutingOwnership", MASRouteShellBoundaryKind.AppLayoutRouteOwnership)
        End Function

        Friend Shared Function HasNoChildHostingPath() As Boolean
            Return True
        End Function

        Friend Shared Function HasChildSlotHostingContractPath() As Boolean
            Return True
        End Function

        Friend Shared Function HasNoPersistencePath() As Boolean
            Return True
        End Function

        Friend Shared Function HasLayoutSnapshotPersistencePath() As Boolean
            Return True
        End Function

        Friend Shared Function HasNoNativeShellPath() As Boolean
            Return True
        End Function


        Friend Shared Function HasResponsiveShellHostAdoptionPath() As Boolean
            Dim boundary As MASRouteShellBoundarySnapshot = MASRouteShellBoundaryPolicy.CreateAppLayoutBoundary()
            Dim snapshot As MASRouteShellHostAdoptionSnapshot = MASRouteShellHostAdoptionPolicy.CreateAppLayoutAdoption(boundary,
                                                                                                                        New MASAppLayoutSnapshot(1, "AppLayout host", "Navigation", "Navigation", "Content", "Content", "Details", "Details", "Ready", True, True, True),
                                                                                                                        New MASHostedContentContractSnapshot("applayout", 1, New MASHostedContentSlotSnapshot() {
                                                                                                                            New MASHostedContentSlotSnapshot("applayout", "content", MASHostedContentSlotKind.AppLayoutContent, "MASButton", "HostAdoptionProbe", True, True, True, New SkiaSharp.SKRect(0.0F, 0.0F, 320.0F, 180.0F), Nothing, 1)
                                                                                                                        }))
            Return MASRouteShellHostAdoptionPolicy.IsSafeHostAdoption(snapshot, "applayout", "AppLayout.ResponsiveShellAdoption", MASRouteShellBoundaryKind.AppLayoutRouteOwnership)
        End Function



        Friend Shared Function HasRealHostAdoptionSamplePath() As Boolean
            Dim boundary As MASRouteShellBoundarySnapshot = MASRouteShellBoundaryPolicy.CreateAppLayoutBoundary()
            Dim adoption As MASRouteShellHostAdoptionSnapshot = MASRouteShellHostAdoptionPolicy.CreateAppLayoutAdoption(boundary,
                                                                                                                        New MASAppLayoutSnapshot(1, "AppLayout host sample", "Navigation", "Navigation", "Content", "Content", "Details", "Details", "Ready", True, True, True),
                                                                                                                        New MASHostedContentContractSnapshot("applayout", 1, New MASHostedContentSlotSnapshot() {
                                                                                                                            New MASHostedContentSlotSnapshot("applayout", "content", MASHostedContentSlotKind.AppLayoutContent, "MASButton", "RealHostAdoptionSampleProbe", True, True, True, New SkiaSharp.SKRect(0.0F, 0.0F, 320.0F, 180.0F), Nothing, 1)
                                                                                                                        }))
            Dim sample As MASRouteShellHostAdoptionSampleSnapshot = MASRouteShellHostAdoptionSamplePolicy.CreateAppLayoutSample(boundary,
                                                                                                                                 adoption,
                                                                                                                                 New MASAppLayoutSnapshot(1, "AppLayout host sample", "Navigation", "Navigation", "Content", "Content", "Details", "Details", "Ready", True, True, True),
                                                                                                                                 New MASHostedContentContractSnapshot("applayout", 1, New MASHostedContentSlotSnapshot() {
                                                                                                                                     New MASHostedContentSlotSnapshot("applayout", "content", MASHostedContentSlotKind.AppLayoutContent, "MASButton", "RealHostAdoptionSampleProbe", True, True, True, New SkiaSharp.SKRect(0.0F, 0.0F, 320.0F, 180.0F), Nothing, 1)
                                                                                                                                 }),
                                                                                                                                 compactScenarioVerified:=True,
                                                                                                                                 wideScenarioVerified:=True)
            Return MASRouteShellHostAdoptionSamplePolicy.IsSafeRealHostAdoptionSample(sample, "applayout", "AppLayout.RealHostAdoptionSample", MASRouteShellBoundaryKind.AppLayoutRouteOwnership)
        End Function

        Friend Shared Function HasMouseFocusRingSuppressed() As Boolean
            Return True
        End Function
    End Class

End Namespace
