Option Strict On
Option Explicit On

Imports System

Namespace Nexamas.UI.TextInput

    ''' <summary>
    ''' Owns multiline mode, wrapping, and vertical scroll state for MAS text-input controls.
    ''' </summary>
    ''' <remarks>
    ''' Multiline mode affects input commands, layout snapshot construction, viewport
    ''' clamping, and rendering. This collaborator centralizes the mode/scroll state so
    ''' MASInputBoxSkiaBase does not store low-level multiline viewport fields directly.
    ''' The owning control still performs layout because it owns ThemeContext and paint creation.
    ''' </remarks>
    Friend NotInheritable Class MASTextInputMultilineController

#Region "Properties"

        Friend Property Enabled As Boolean
        Friend Property WordWrap As Boolean = True
        Friend Property VerticalScrollOffsetPx As Single

#End Region

#Region "Mode"

        Friend Function SetEnabled(value As Boolean) As Boolean
            If Enabled = value Then Return False

            Enabled = value
            VerticalScrollOffsetPx = 0.0F
            Return True
        End Function

        Friend Function SetWordWrap(value As Boolean) As Boolean
            If WordWrap = value Then Return False

            WordWrap = value
            Return True
        End Function

#End Region

#Region "Scroll"

        Friend Sub ScrollVerticalBy(deltaPx As Single)
            If Single.IsNaN(deltaPx) OrElse Single.IsInfinity(deltaPx) Then Return

            VerticalScrollOffsetPx += deltaPx
            ClampVerticalToZero()
        End Sub

        Friend Function SetVerticalScrollOffset(value As Single) As Boolean
            Dim normalized As Single = If(Single.IsNaN(value) OrElse Single.IsInfinity(value), 0.0F, value)
            If normalized < 0.0F Then normalized = 0.0F

            If Math.Abs(VerticalScrollOffsetPx - normalized) <= 0.01F Then Return False

            VerticalScrollOffsetPx = normalized
            Return True
        End Function

        Friend Sub ClampVerticalToZero()
            If VerticalScrollOffsetPx < 0.0F Then VerticalScrollOffsetPx = 0.0F
        End Sub

        Friend Function ResolveHorizontalScrollOffset(singleLineScrollOffsetPx As Single) As Single
            If WordWrap Then Return 0.0F
            Return singleLineScrollOffsetPx
        End Function

#End Region

    End Class

End Namespace
