Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Services

Namespace Nexamas.UI.TextInput

    ''' <summary>
    ''' Owns clipboard behavior for MAS text-input controls.
    ''' </summary>
    ''' <remarks>
    ''' MASInputBoxSkiaBase exposes the public ClipboardService property, but this
    ''' collaborator owns copy, cut, and paste semantics so that clipboard access,
    ''' read-only checks, MaxLength capacity checks, and input normalization are not
    ''' embedded in the visual control base class.
    ''' </remarks>
    Friend NotInheritable Class MASTextInputClipboardController

#Region "Fields"

        Private _service As IMASClipboardService

#End Region

#Region "Constructor"

        Friend Sub New()
            _service = New MASWinFormsClipboardService()
        End Sub

#End Region

#Region "Properties"

        Friend Property Service As IMASClipboardService
            Get
                Return _service
            End Get
            Set(value As IMASClipboardService)
                _service = If(value, New MASWinFormsClipboardService())
            End Set
        End Property

#End Region

#Region "Operations"

        Friend Function CopySelection(state As MASTextInputState) As Boolean
            If state Is Nothing Then Return False

            Dim selected As String = If(state.SelectedText, String.Empty)
            If String.IsNullOrEmpty(selected) Then Return False

            If _service IsNot Nothing Then
                _service.SetText(selected)
            End If

            ' Copy is a consumed command, not a text mutation.
            Return False
        End Function

        Friend Function CutSelection(state As MASTextInputState,
                                     controller As MASTextInputController,
                                     isReadOnly As Boolean) As Boolean
            If state Is Nothing OrElse controller Is Nothing Then Return False
            If isReadOnly Then Return False

            Dim selected As String = If(state.SelectedText, String.Empty)
            If String.IsNullOrEmpty(selected) Then Return False

            If _service IsNot Nothing Then
                _service.SetText(selected)
            End If

            Return controller.DeleteSelection()
        End Function

        Friend Function PasteClipboard(controller As MASTextInputController,
                                       isReadOnly As Boolean,
                                       getRemainingCapacity As Func(Of Integer),
                                       normalizeIncomingText As Func(Of String, Integer, String)) As Boolean
            If controller Is Nothing Then Return False
            If isReadOnly Then Return False
            If _service Is Nothing OrElse Not _service.HasText() Then Return False

            Dim allowedSpace As Integer = If(getRemainingCapacity Is Nothing, 0, getRemainingCapacity.Invoke())
            If allowedSpace = 0 Then Return False

            Dim raw As String = If(_service.GetText(), String.Empty)
            Dim normalized As String

            If normalizeIncomingText Is Nothing Then
                normalized = If(raw, String.Empty)
            Else
                normalized = If(normalizeIncomingText.Invoke(raw, allowedSpace), String.Empty)
            End If

            If normalized.Length = 0 Then Return False

            Return controller.PasteText(normalized)
        End Function

#End Region

    End Class

End Namespace
