Option Strict On
Option Explicit On

Imports System
Imports System.Windows.Forms

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only interaction authority for MASToggleSwitch. It keeps toggle input,
    ''' keyboard state decisions, lifecycle cancellation, and text normalization out
    ''' of the public control surface so the visual control remains a small gateway.
    ''' </summary>
    Friend NotInheritable Class MASToggleSwitchInteractionPolicy

        Private Sub New()
        End Sub

        Friend Shared Function NormalizeText(value As String) As String
            Dim s As String = If(value, String.Empty)
            s = s.Replace(vbCrLf, " ")
            s = s.Replace(vbCr, " ")
            s = s.Replace(vbLf, " ")
            s = s.Replace(vbTab, " ")
            Return CollapseSpaces(s).Trim()
        End Function

        Friend Shared Function CanInteract(isEnabled As Boolean, isVisible As Boolean) As Boolean
            Return isEnabled AndAlso isVisible
        End Function

        Friend Shared Function ResolveNextChecked(current As Boolean) As Boolean
            Return Not current
        End Function

        Friend Shared Function ShouldToggleFromKey(keyCode As Keys) As Boolean
            Return keyCode = Keys.Space OrElse keyCode = Keys.Enter
        End Function

        Friend Shared Function ShouldCancelFromKey(keyCode As Keys) As Boolean
            Return keyCode = Keys.Escape
        End Function

        Friend Shared Function TryResolveKeyboardState(keyCode As Keys,
                                                       current As Boolean,
                                                       ByRef nextChecked As Boolean) As Boolean
            Select Case keyCode
                Case Keys.Left, Keys.Home
                    nextChecked = False
                    Return True

                Case Keys.Right, Keys.End
                    nextChecked = True
                    Return True

                Case Else
                    nextChecked = current
                    Return False
            End Select
        End Function

        Friend Shared Function ShouldResetTransientState(isEnabled As Boolean,
                                                         isVisible As Boolean) As Boolean
            Return Not isEnabled OrElse Not isVisible
        End Function

        Private Shared Function CollapseSpaces(value As String) As String
            If String.IsNullOrEmpty(value) Then Return String.Empty

            Dim result As New System.Text.StringBuilder(value.Length)
            Dim previousSpace As Boolean = False

            For Each ch As Char In value
                Dim isSpace As Boolean = Char.IsWhiteSpace(ch)
                If isSpace Then
                    If previousSpace Then Continue For
                    result.Append(" "c)
                    previousSpace = True
                Else
                    result.Append(ch)
                    previousSpace = False
                End If
            Next

            Return result.ToString()
        End Function

    End Class

End Namespace
