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 MASContentCarousel. It centralizes content text hygiene,
    ''' key hygiene, selection/index clamping, RTL detection, theme color resolution, and
    ''' boundary evidence. It deliberately does not own galleries, image loading, slide
    ''' decks, onboarding flows, content providers, auto-rotation clocks, or transition engines.
    ''' </summary>
    Friend NotInheritable Class MASContentCarouselPolicy
        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 ContentCarouselTokens.DefaultTitle
            Return ClampText(text, 72)
        End Function

        Friend Shared Function NormalizeItemTitle(value As String) As String
            Dim text As String = NormalizeText(value)
            If text.Length = 0 Then Return ContentCarouselTokens.DefaultItemTitle
            Return ClampText(text, 86)
        End Function

        Friend Shared Function NormalizeSubtitle(value As String) As String
            Return ClampText(NormalizeText(value), 96)
        End Function

        Friend Shared Function NormalizeDetail(value As String) As String
            Return ClampText(NormalizeText(value), 180)
        End Function

        Friend Shared Function NormalizeKey(value As String,
                                            fallbackPrefix As String,
                                            ordinal As Integer) As String
            Dim text As String = NormalizeText(value)
            If text.Length = 0 Then text = fallbackPrefix & Math.Max(1, ordinal).ToString(CultureInfo.InvariantCulture)
            text = text.Replace(" "c, "-"c)
            Return ClampText(text, 64)
        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 ClampIndex(index As Integer,
                                           count As Integer) As Integer
            If count <= 0 Then Return -1
            If index < 0 Then Return 0
            If index >= count Then Return count - 1
            Return index
        End Function

        Friend Shared Function MoveIndex(currentIndex As Integer,
                                          delta As Integer,
                                          count As Integer,
                                          isLooping As Boolean) As Integer
            If count <= 0 Then Return -1
            Dim current As Integer = ClampIndex(currentIndex, count)
            Dim candidate As Integer = current + delta
            If isLooping Then
                Do While candidate < 0
                    candidate += count
                Loop
                Do While candidate >= count
                    candidate -= count
                Loop
                Return candidate
            End If
            Return ClampIndex(candidate, count)
        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, "MASContentCarouselPolicy.ResolveRightToLeft")
                Return False
            End Try
        End Function

        Friend Shared Function BuildSummary(itemCount As Integer,
                                            selectedIndex As Integer) As String
            If itemCount <= 0 Then Return "0 items"
            Dim indexText As String = (Math.Max(0, selectedIndex) + 1).ToString(CultureInfo.CurrentCulture)
            Dim totalText As String = itemCount.ToString(CultureInfo.CurrentCulture)
            Dim noun As String = If(itemCount = 1, "item", "items")
            Return indexText & " / " & totalText & " " & noun
        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(ContentCarouselTokens.TextAlpha)
            Return Nexamas.UI.Values.Color.Text.Primary.WithAlpha(ContentCarouselTokens.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(ContentCarouselTokens.MutedTextAlpha)
            Return Nexamas.UI.Values.Color.Text.Secondary.WithAlpha(ContentCarouselTokens.MutedTextAlpha)
        End Function

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

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

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

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

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

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

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

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

End Namespace
