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 MASSplitView. It centralizes pane text hygiene,
    ''' bounded ratio math, RTL detection, theme color resolution, and explicit
    ''' ownership boundaries. It deliberately does not own public/native child hosting,
    ''' dock layout, shell routing, persistence, native panel composition, or mouse-click
    ''' focus rings. Friend-only pane hosting is governed separately.
    ''' </summary>
    Friend NotInheritable Class MASSplitViewPolicy
        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 SplitViewTokens.DefaultTitle
            Return ClampText(text, 64)
        End Function

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

        Friend Shared Function NormalizePaneText(value As String,
                                                  fallback As String) As String
            Dim text As String = NormalizeText(value)
            If text.Length = 0 Then Return fallback
            Return ClampText(text, 130)
        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 ClampRatio(value As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) Then Return SplitViewTokens.DefaultLeftRatio
            If value < SplitViewTokens.MinPaneRatio Then Return SplitViewTokens.MinPaneRatio
            If value > SplitViewTokens.MaxPaneRatio Then Return SplitViewTokens.MaxPaneRatio
            Return value
        End Function

        Friend Shared Sub NormalizeSideRatios(ByRef leftRatio As Single,
                                              ByRef rightRatio As Single)
            leftRatio = ClampRatio(leftRatio)
            rightRatio = ClampRatio(rightRatio)
            Dim total As Single = leftRatio + rightRatio
            If total <= SplitViewTokens.MaxCombinedSideRatio Then Return
            Dim scale As Single = SplitViewTokens.MaxCombinedSideRatio / total
            leftRatio = ClampRatio(leftRatio * scale)
            rightRatio = ClampRatio(rightRatio * scale)
        End Sub

        Friend Shared Function BuildSummary(leftCollapsed As Boolean,
                                            rightCollapsed As Boolean,
                                            leftRatio As Single,
                                            rightRatio As Single) As String
            Dim leftState As String = If(leftCollapsed, "left collapsed", "left " & Math.Round(leftRatio * 100.0F).ToString(CultureInfo.CurrentCulture) & "%")
            Dim rightState As String = If(rightCollapsed, "right collapsed", "right " & Math.Round(rightRatio * 100.0F).ToString(CultureInfo.CurrentCulture) & "%")
            Return leftState & " / " & rightState
        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, "MASSplitViewPolicy.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(SplitViewTokens.TextAlpha)
            Return Nexamas.UI.Values.Color.Text.Primary.WithAlpha(SplitViewTokens.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(SplitViewTokens.MutedTextAlpha)
            Return Nexamas.UI.Values.Color.Text.Secondary.WithAlpha(SplitViewTokens.MutedTextAlpha)
        End Function

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

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

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

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

        Friend Shared Function HasShellIntegrationBoundaryPath() As Boolean
            Dim snapshot As MASRouteShellBoundarySnapshot = MASRouteShellBoundaryPolicy.CreateSplitViewBoundary()
            Return MASRouteShellBoundaryPolicy.IsSafeClosedBoundary(snapshot, "splitview", "SplitView.ShellIntegration", MASRouteShellBoundaryKind.SplitViewShellIntegrationBoundary)
        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 HasNoNativePanelPath() As Boolean
            Return True
        End Function

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


        Friend Shared Function HasHostAdoptionProofPath() As Boolean
            Dim boundary As MASRouteShellBoundarySnapshot = MASRouteShellBoundaryPolicy.CreateSplitViewBoundary()
            Dim snapshot As MASRouteShellHostAdoptionSnapshot = MASRouteShellHostAdoptionPolicy.CreateSplitViewAdoption(boundary,
                                                                                                                        New MASSplitViewLayoutSnapshot(1, "Split host", "Left", "Content", "Right", "Left", "Content", "Right", SplitViewTokens.DefaultLeftRatio, SplitViewTokens.DefaultRightRatio, False, False),
                                                                                                                        New MASHostedContentContractSnapshot("splitview", 1, New MASHostedContentSlotSnapshot() {
                                                                                                                            New MASHostedContentSlotSnapshot("splitview", "content", MASHostedContentSlotKind.SplitViewContentPane, "MASButton", "HostAdoptionProbe", True, True, True, New SkiaSharp.SKRect(0.0F, 0.0F, 320.0F, 180.0F), Nothing, 1)
                                                                                                                        }),
                                                                                                                        New MASSplitViewKeyboardSplitterSnapshot(1, MASSplitViewKeyboardSplitterTarget.LeftDivider, SplitViewTokens.DefaultLeftRatio, SplitViewTokens.DefaultRightRatio, False, False, SplitViewTokens.KeyboardRatioStep, SplitViewTokens.KeyboardLargeRatioStep))
            Return MASRouteShellHostAdoptionPolicy.IsSafeHostAdoption(snapshot, "splitview", "SplitView.HostAdoptionProof", MASRouteShellBoundaryKind.SplitViewShellIntegrationBoundary)
        End Function



        Friend Shared Function HasRealHostAdoptionSamplePath() As Boolean
            Dim boundary As MASRouteShellBoundarySnapshot = MASRouteShellBoundaryPolicy.CreateSplitViewBoundary()
            Dim hosted As New MASHostedContentContractSnapshot("splitview", 1, New MASHostedContentSlotSnapshot() {
                New MASHostedContentSlotSnapshot("splitview", "left", MASHostedContentSlotKind.SplitViewLeftPane, "MASButton", "RealHostAdoptionSampleProbeLeft", True, True, True, New SkiaSharp.SKRect(0.0F, 0.0F, 160.0F, 180.0F), Nothing, 1),
                New MASHostedContentSlotSnapshot("splitview", "content", MASHostedContentSlotKind.SplitViewContentPane, "MASButton", "RealHostAdoptionSampleProbeContent", True, True, True, New SkiaSharp.SKRect(168.0F, 0.0F, 420.0F, 180.0F), Nothing, 1)
            })
            Dim splitter As New MASSplitViewKeyboardSplitterSnapshot(1, MASSplitViewKeyboardSplitterTarget.LeftDivider, SplitViewTokens.DefaultLeftRatio, SplitViewTokens.DefaultRightRatio, False, False, SplitViewTokens.KeyboardRatioStep, SplitViewTokens.KeyboardLargeRatioStep)
            Dim adoption As MASRouteShellHostAdoptionSnapshot = MASRouteShellHostAdoptionPolicy.CreateSplitViewAdoption(boundary,
                                                                                                                        New MASSplitViewLayoutSnapshot(1, "Split host sample", "Left", "Content", "Right", "Left", "Content", "Right", SplitViewTokens.DefaultLeftRatio, SplitViewTokens.DefaultRightRatio, False, False),
                                                                                                                        hosted,
                                                                                                                        splitter)
            Dim sample As MASRouteShellHostAdoptionSampleSnapshot = MASRouteShellHostAdoptionSamplePolicy.CreateSplitViewSample(boundary,
                                                                                                                                 adoption,
                                                                                                                                 New MASSplitViewLayoutSnapshot(1, "Split host sample", "Left", "Content", "Right", "Left", "Content", "Right", SplitViewTokens.DefaultLeftRatio, SplitViewTokens.DefaultRightRatio, False, False),
                                                                                                                                 hosted,
                                                                                                                                 splitter,
                                                                                                                                 focusScenarioVerified:=True,
                                                                                                                                 announcementScenarioVerified:=True)
            Return MASRouteShellHostAdoptionSamplePolicy.IsSafeRealHostAdoptionSample(sample, "splitview", "SplitView.RealHostAdoptionSample", MASRouteShellBoundaryKind.SplitViewShellIntegrationBoundary)
        End Function

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

End Namespace
