Option Strict On
Option Explicit On

Imports System
Imports System.Text
Imports Nexamas.UI.Rendering

Namespace Nexamas.UI.Controls

    ''' <summary>
    ''' Friend-only policy for the Round 17 toast refinement. It centralizes toast
    ''' text hygiene, tone normalization, duration and queue limits, continuous
    ''' lifetime progression, RTL text detection, and the FloatRuntime/service boundary.
    ''' Public SDK code still enters only through the application toast facade.
    ''' </summary>
    Friend NotInheritable Class MASToastVisualLifecyclePolicy

        Friend Const DefaultMessageText As String = "Toast"
        Friend Const MaxVisibleUpperBound As Integer = 8
        Friend Const MaxQueueUpperBound As Integer = 64
        Friend Const MaxDurationMs As Integer = 60000

        Private Sub New()
        End Sub

        Friend Shared Function NormalizeTitle(value As String) As String
            Return NormalizeText(value, String.Empty)
        End Function

        Friend Shared Function NormalizeMessage(value As String) As String
            Return NormalizeText(value, String.Empty)
        End Function

        Friend Shared Function ResolveMessageText(title As String,
                                                  message As String) As String
            Dim normalizedTitle As String = NormalizeTitle(title)
            Dim normalizedMessage As String = NormalizeMessage(message)
            If normalizedTitle.Length = 0 AndAlso normalizedMessage.Length = 0 Then Return DefaultMessageText
            Return normalizedMessage
        End Function

        Friend Shared Function NormalizeActionText(value As String) As String
            Return NormalizeText(value, String.Empty)
        End Function

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

            Dim sb As New StringBuilder(source.Length)
            Dim previousWasWhiteSpace As Boolean = False

            For Each ch As Char In source
                If Char.IsControl(ch) Then
                    If ch = ChrW(9) OrElse ch = ChrW(10) OrElse ch = ChrW(13) Then
                        If Not previousWasWhiteSpace Then
                            sb.Append(" "c)
                            previousWasWhiteSpace = True
                        End If
                    End If
                    Continue For
                End If

                If Char.IsWhiteSpace(ch) Then
                    If Not previousWasWhiteSpace Then
                        sb.Append(" "c)
                        previousWasWhiteSpace = True
                    End If
                Else
                    sb.Append(ch)
                    previousWasWhiteSpace = False
                End If
            Next

            Dim normalized As String = sb.ToString().Trim()
            If normalized.Length = 0 Then Return If(fallback, String.Empty)
            Return normalized
        End Function

        Friend Shared Function NormalizeTone(value As MASToastTone) As MASToastTone
            Select Case value
                Case MASToastTone.Neutral, MASToastTone.Success, MASToastTone.Info, MASToastTone.Warning, MASToastTone.Danger
                    Return value
                Case Else
                    Return MASToastTone.Info
            End Select
        End Function

        Friend Shared Function NormalizeDurationMs(value As Integer) As Integer
            If value <= 0 Then Return 0
            Return Math.Min(MaxDurationMs, value)
        End Function

        Friend Shared Function NormalizeMaxVisible(value As Integer) As Integer
            Return Math.Max(1, Math.Min(MaxVisibleUpperBound, value))
        End Function

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

        Friend Shared Function NormalizeSurfaceMaterial(value As String) As String
            Return MASSurfaceTreatmentMaterialKeyNormalizer.NormalizeForStorage(value)
        End Function

        Friend Shared Function ShouldPauseLifetime(item As MASToastItem) As Boolean
            ' Round17 FIX1: hover and pointer press are visual interaction states only.
            ' Toast lifetime must continue to progress while the user hovers or clicks.
            Return False
        End Function

        Friend Shared Function ShouldAdvanceLifetime(item As MASToastItem) As Boolean
            If item Is Nothing Then Return False
            If item.IsClosing Then Return False
            Return Not ShouldPauseLifetime(item)
        End Function

        Friend Shared Function ResolveLifetimeFraction(item As MASToastItem) As Single
            If item Is Nothing Then Return 0.0F
            If item.DurationMs <= 0 Then Return 0.0F
            Dim fraction As Double = item.RemainingMs / Math.Max(1.0R, CDbl(item.DurationMs))
            If Double.IsNaN(fraction) OrElse Double.IsInfinity(fraction) Then Return 0.0F
            Return CSng(Math.Max(0.0R, Math.Min(1.0R, fraction)))
        End Function

        Friend Shared Function ResolveTextIsRtl(title As String,
                                                message As String) As Boolean
            Return ContainsRtlText(title) OrElse ContainsRtlText(message)
        End Function

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

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

        Friend Shared Function HasNoParallelToastServiceRoute() 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
