Option Strict On
Option Explicit On

Imports System
Imports System.Globalization
Imports Nexamas.UI.Values

Namespace Nexamas.UI.Controls

    ''' <summary>
    ''' Friend-owned content and row policy for MASNotificationPanel. The policy centralizes
    ''' title/message/action hygiene, tone normalization, visible-row limits, read-state facts,
    ''' RTL detection, and service-free notification boundaries without adding a delivery queue,
    ''' persistence layer, push service, adapter, wrapper, or parallel notification route.
    ''' </summary>
    Friend NotInheritable Class MASNotificationPanelContentPolicy
        Private Sub New()
        End Sub

        Friend Shared Function NormalizeTitle(value As String,
                                              fallback As String) As String
            Return NormalizeText(value, fallback, 96)
        End Function

        Friend Shared Function NormalizeMessage(value As String,
                                                fallback As String) As String
            Return NormalizeText(value, fallback, 180)
        End Function

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

        Friend Shared Function NormalizeActionText(value As String) As String
            Return NormalizeText(value, String.Empty, 32)
        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 NormalizeVisibleRows(itemCount As Integer,
                                                    availableRows As Integer) As Integer
            Dim safeAvailable As Integer = Math.Max(1, availableRows)
            If itemCount <= 0 Then Return 0
            Return Math.Min(Math.Min(itemCount, NotificationPanelTokens.DefaultVisibleRows), safeAvailable)
        End Function

        Friend Shared Function ResolveCountText(itemCount As Integer,
                                                unreadCount As Integer) As String
            If unreadCount > 0 Then Return unreadCount.ToString(CultureInfo.InvariantCulture) & " unread"
            If itemCount > 0 Then Return itemCount.ToString(CultureInfo.InvariantCulture) & " total"
            Return String.Empty
        End Function

        Friend Shared Function ResolveTextIsRtl(text As String) As Boolean
            If Nexamas.UI.Localization.MASLocalization.IsCurrentRightToLeft() Then Return True

            Dim value As String = If(text, String.Empty)
            For Each ch As Char In value
                Dim code As Integer = AscW(ch)
                If code >= &H590 AndAlso code <= &H8FF Then Return True
            Next
            Return False
        End Function

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

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

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

        Private Shared Function NormalizeText(value As String,
                                              fallback As String,
                                              maxLength As Integer) As String
            Dim text As String = If(value, String.Empty).Trim()
            If text.Length = 0 Then text = If(fallback, String.Empty)
            Dim limit As Integer = Math.Max(0, maxLength)
            If limit > 0 AndAlso text.Length > limit Then text = text.Substring(0, limit)
            Return text
        End Function
    End Class

End Namespace
