Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Theming
Imports SkiaSharp

Namespace Nexamas.UI.TextInput

    ''' <summary>
    ''' Owns Win32 IME bridging and IME candidate-position resolution for MAS text-input controls.
    ''' </summary>
    ''' <remarks>
    ''' The visual control supplies the current layout primitives, while this collaborator
    ''' owns the IME bridge call, command dispatch boundary, and caret-based candidate
    ''' anchor calculation. This keeps MASInputBoxSkiaBase from depending directly on
    ''' Win32 IME message translation policy.
    ''' </remarks>
    Friend NotInheritable Class MASTextInputImeController

#Region "Win32 bridge"

        Friend Function ProcessWin32Message(hwnd As IntPtr,
                                            message As Integer,
                                            wParam As IntPtr,
                                            lParam As IntPtr,
                                            isEnabled As Boolean,
                                            dispatch As Func(Of MASTextCompositionCommand, Boolean),
                                            candidatePointProvider As Func(Of SKPoint)) As Boolean
            If Not isEnabled Then Return False
            If dispatch Is Nothing Then Return False
            If candidatePointProvider Is Nothing Then Return False

            Return MASWin32IMEBridge.ProcessMessage(
                hwnd:=hwnd,
                message:=message,
                wParam:=wParam,
                lParam:=lParam,
                dispatch:=dispatch,
                candidatePointProvider:=candidatePointProvider)
        End Function

#End Region

#Region "Candidate point"

        Friend Function ResolveCandidatePoint(ctx As MASThemeContext,
                                               contentR As SKRect,
                                               paint As SKPaint,
                                               layout As MASTextInputLayoutEngine,
                                               state As MASTextInputState,
                                               scrollOffsetPx As Single,
                                               displayText As String,
                                               isRtl As Boolean) As SKPoint
            If ctx Is Nothing Then Return New SKPoint(0.0F, 0.0F)

            If contentR.Width <= 2.0F OrElse contentR.Height <= 2.0F Then
                Return New SKPoint(0.0F, 0.0F)
            End If

            If paint Is Nothing OrElse layout Is Nothing OrElse state Is Nothing Then
                Return New SKPoint(contentR.Left, contentR.Bottom)
            End If

            Dim caretX As Single =
                layout.GetCaretX(
                    contentR:=contentR,
                    paint:=paint,
                    scrollOffsetPx:=scrollOffsetPx,
                    caretIndex:=state.CaretIndex,
                    isRtl:=isRtl,
                    displayText:=If(displayText, String.Empty))

            caretX = Math.Max(contentR.Left, Math.Min(contentR.Right, caretX))

            Return New SKPoint(caretX, contentR.Bottom)
        End Function

#End Region

    End Class

End Namespace
