Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.Windows.Forms
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-owned policy for MASTransferList. It centralizes item hygiene,
    ''' bounded item counts, list-selection math, RTL visual order, and explicit
    ''' architecture boundaries so the control does not become a binder, provider,
    ''' permission engine, drag/drop runtime, or virtualization subsystem.
    ''' </summary>
    Friend NotInheritable Class MASTransferListPolicy
        Private Sub New()
        End Sub

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

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

        Friend Shared Function NormalizeItems(values As IEnumerable(Of String)) As List(Of String)
            Dim result As New List(Of String)()
            If values Is Nothing Then Return result
            For Each value As String In values
                Dim normalized As String = NormalizeItem(value)
                If normalized.Length = 0 Then Continue For
                If result.Count >= TransferListTokens.MaxStoredItems Then Exit For
                result.Add(normalized)
            Next
            Return result
        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 -1
            If index >= count Then Return count - 1
            Return index
        End Function

        Friend Shared Function RemoveFirstByText(source As List(Of String), text As String) As Boolean
            If source Is Nothing OrElse String.IsNullOrEmpty(text) Then Return False
            For i As Integer = 0 To source.Count - 1
                If String.Equals(source(i), text, StringComparison.CurrentCultureIgnoreCase) Then
                    source.RemoveAt(i)
                    Return True
                End If
            Next
            Return False
        End Function

        Friend Shared Function EnsureVisibleTopIndex(selectedIndex As Integer,
                                                     currentTopIndex As Integer,
                                                     itemCount As Integer) As Integer
            If itemCount <= 0 Then Return 0
            Dim maxTop As Integer = Math.Max(0, itemCount - TransferListTokens.MaxVisibleRows)
            Dim top As Integer = Math.Max(0, Math.Min(currentTopIndex, maxTop))
            If selectedIndex < 0 Then Return top
            If selectedIndex < top Then Return Math.Max(0, selectedIndex)
            If selectedIndex >= top + TransferListTokens.MaxVisibleRows Then
                Return Math.Max(0, Math.Min(maxTop, selectedIndex - TransferListTokens.MaxVisibleRows + 1))
            End If
            Return top
        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, "MASTransferListPolicy.ResolveRightToLeft")
                Return False
            End Try
        End Function

        Friend Shared Function BuildCountText(count As Integer) As String
            If count = 1 Then Return "1 item"
            Return count.ToString(CultureInfo.CurrentCulture) & " items"
        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(TransferListTokens.TextAlpha)
            Return Nexamas.UI.Values.Color.Text.Primary.WithAlpha(TransferListTokens.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(TransferListTokens.MutedTextAlpha)
            Return Nexamas.UI.Values.Color.Text.Secondary.WithAlpha(TransferListTokens.MutedTextAlpha)
        End Function

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

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

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

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

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

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

End Namespace
