Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Controls

    ''' <summary>
    ''' Friend-only visual/lifecycle policy for MASTooltip Round 17 refinement. It owns
    ''' tooltip text hygiene, delay/grace clamping, hover-rect inflation, radius/text
    ''' choices, RTL text detection, and the public-boundary rule that tooltip content
    ''' stays owned by the application tooltip facade and FloatRuntime path.
    ''' </summary>
    Friend NotInheritable Class MASTooltipVisualPolicy

        Friend Const DefaultHoverDelayMs As Integer = 300
        Friend Const DefaultHideGraceMs As Integer = 110
        Friend Const MaxHoverDelayMs As Integer = 2000
        Friend Const MaxHideGraceMs As Integer = 1000

        Private Sub New()
        End Sub

        Friend Shared Function NormalizeText(value As String) As String
            Dim text As String = If(value, String.Empty).Trim()
            If text.Length = 0 Then Return String.Empty

            Dim chars As Char() = text.ToCharArray()
            For i As Integer = 0 To chars.Length - 1
                If Char.IsControl(chars(i)) AndAlso chars(i) <> ChrW(9) AndAlso chars(i) <> ChrW(10) AndAlso chars(i) <> ChrW(13) Then
                    chars(i) = " "c
                End If
            Next

            Return New String(chars).Trim()
        End Function

        Friend Shared Function NormalizeHoverDelayMs(value As Integer) As Integer
            Return Math.Max(0, Math.Min(MaxHoverDelayMs, value))
        End Function

        Friend Shared Function NormalizeHideGraceMs(value As Integer) As Integer
            Return Math.Max(0, Math.Min(MaxHideGraceMs, value))
        End Function

        Friend Shared Function ResolveHoverRectInflatePx(value As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) Then Return 0.0F
            Return Math.Max(0.0F, Math.Min(12.0F, value))
        End Function

        Friend Shared Function ResolvePanelRadiusPx(panelRect As SKRect,
                                                    dpi As Single) As Single
            Dim safeDpi As Single = Math.Max(0.5F, Math.Min(4.0F, dpi))
            Dim tokenRadius As Single = TooltipTokens.CornerRadius * safeDpi
            Dim geometricRadius As Single = Math.Min(panelRect.Width, panelRect.Height) * 0.34F
            Return Math.Max(6.0F * safeDpi, Math.Min(Math.Min(tokenRadius, geometricRadius), 14.0F * safeDpi))
        End Function

        Friend Shared Function ResolveTextColor(tooltipTheme As IMASTooltipTheme) As SKColor
            If tooltipTheme IsNot Nothing AndAlso tooltipTheme.Text IsNot Nothing AndAlso tooltipTheme.Text.Text.Alpha > 0 Then
                Return tooltipTheme.Text.Text
            End If

            Return New SKColor(246, 248, 252, 255)
        End Function

        Friend Shared Function ResolveTextIsRtl(lines As IList(Of String)) As Boolean
            If lines Is Nothing Then Return False
            For Each line As String In lines
                If ContainsRtlText(line) Then Return True
            Next
            Return False
        End Function

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

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

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

        Private Shared Function ContainsRtlText(value As String) As Boolean
            If String.IsNullOrEmpty(value) Then Return False

            For Each ch As Char In value
                Dim code As Integer = AscW(ch)
                If (code >= &H590 AndAlso code <= &H8FF) OrElse
                   (code >= &HFB1D AndAlso code <= &HFDFF) OrElse
                   (code >= &HFE70 AndAlso code <= &HFEFF) Then
                    Return True
                End If
            Next

            Return False
        End Function

    End Class

End Namespace
