Option Strict On
Option Explicit On

Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Diagnostics
Imports Nexamas.UI.Controls
Imports Nexamas.UI.General
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.RuntimeEnvironment
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports Nexamas.UI.TextInput
Imports Nexamas.UI.Services
Imports Nexamas.UI.Composition
Imports SkiaSharp

Namespace Nexamas.UI.Components

    Partial Public MustInherit Class MASInputBoxSkiaBase
        Inherits MASControlBase
        Implements IMASTextInputControl
        Implements IMASDefaultFocusProvider
        Implements IMASLayoutParticipant
        Implements IMASIntrinsicSizeContract

#Region "Fields"

        Private ReadOnly _controller As New MASTextInputController()
        Private ReadOnly _layoutEngine As New MASTextInputLayoutEngine()
        Private ReadOnly _multilineViewport As New MASTextMultilineViewport()

        Private ReadOnly _renderResources As New MASTextInputRenderResources()
        Private ReadOnly _clipboardController As New MASTextInputClipboardController()
        Private ReadOnly _selectionController As New MASTextInputSelectionController()
        Private ReadOnly _editingController As New MASTextInputEditingController()
        Private ReadOnly _imeController As New MASTextInputImeController()
        Private _placeholder As String = InputTokens.DefaultPlaceholder

        Private ReadOnly _validationVisuals As New MASTextInputValidationVisualState()

        Private ReadOnly _layoutCache As New MASTextInputLayoutCache()

        Private ReadOnly _multilineController As New MASTextInputMultilineController()

        Private _fontProfile As MASTextInputFontProfile = MASTextInputFontProfile.CreateDefault()
        Private _textFontSizeDip As Single
        Private _textCrispRendering As Boolean = True
        Private _syntaxHighlighter As IMASTextSyntaxHighlighter
        Private _syntaxHighlightingEnabled As Boolean

#End Region


#Region "State"

        <Conditional("DEBUG")>
        Private Sub ValidateUIContract()
            Debug.Assert(_controller IsNot Nothing, "UI contract failed: controller is Nothing.")

            If _controller Is Nothing Then Return

            Debug.Assert(_controller.State IsNot Nothing,
                         "UI contract failed: controller.State is Nothing.")
        End Sub

        Private ReadOnly Property _state As MASTextInputState
            Get
                Return _controller.State
            End Get
        End Property

#End Region
        Protected Sub New()
            _controller.Clipboard = _clipboardController.Service
        End Sub
#Region "Text layout cache"

        Private Sub InvalidateTextLayout()
            _layoutCache.InvalidateLayout()
        End Sub

        Private Sub InvalidateTextTheme()
            _layoutCache.InvalidateTheme()
        End Sub

        Private Function GetOrBuildTextSnapshot(state As MASTextInputState,
                                                layout As MASTextInputLayoutEngine,
                                                paint As SKPaint,
                                                contentR As SKRect,
                                                scrollOffsetPx As Single,
                                                environmentStamp As MASRuntimeEnvironmentStamp) As MASTextInputLayoutSnapshot

            Return _layoutCache.GetOrBuild(
                state:=state,
                layout:=layout,
                paint:=paint,
                contentR:=contentR,
                scrollOffsetPx:=scrollOffsetPx,
                multiline:=_multilineController.Enabled,
                multilineWordWrap:=_multilineController.WordWrap,
                multilineVerticalScrollOffsetPx:=_multilineController.VerticalScrollOffsetPx,
                crispRendering:=_textCrispRendering,
                environmentStamp:=environmentStamp)
        End Function

#End Region

#Region "Abstract / Overridables"

        Protected Overridable ReadOnly Property HasLeftIcon As Boolean
            Get
                Return False
            End Get
        End Property

        Protected Overridable ReadOnly Property RightReservedWidthDip As Single
            Get
                Return 0.0F
            End Get
        End Property

        Protected Overridable Sub DrawLeftIcon(canvas As SKCanvas, iconRect As SKRect, dpi As Single)
        End Sub

        Protected Overridable Sub DrawRightAdornment(canvas As SKCanvas,
                                                     ctx As MASThemeContext,
                                                     rightRect As SKRect,
                                                     dpi As Single)
        End Sub

        Protected Overridable ReadOnly Property InteractionKindValue As Integer
            Get
                Return CInt(CommonEnums.ControlKind.TextBox)
            End Get
        End Property

        Protected Overridable ReadOnly Property IsReadOnlyBox As Boolean
            Get
                Return _state.ReadOnly
            End Get
        End Property

        Protected Overridable Function FilterIncomingText(value As String) As String
            Return If(value, String.Empty)
        End Function

        Protected Overridable ReadOnly Property EffectiveValidationState As MASTextValidationState
            Get
                If Not Me.Enabled Then Return MASTextValidationState.None
                Return _validationVisuals.State
            End Get
        End Property

#End Region

#Region "Public API"

        ' Public API moved to MASInputBoxSkiaBase.PublicAPI.vb.

#End Region

#Region "MASControlBase integration"

        ' Moved to a focused MASInputBoxSkiaBase partial file.

#End Region


#Region "Composition layout participant"

        ' Moved to a focused MASInputBoxSkiaBase partial file.

#End Region

#Region "Default focus"

        ' Moved to a focused MASInputBoxSkiaBase partial file.

#End Region
#Region "Dispose"

        ' Moved to a focused MASInputBoxSkiaBase partial file.

#End Region


#Region "Unified MASSize Fluent API"

        ''' <summary>
        ''' Strongly typed entry into the shared MASSize intent API.
        ''' This method keeps fluent chains on the concrete element while delegating all sizing decisions to MASControlBase/MASSize.
        ''' </summary>
        Public Shadows Function WithSize(sizeIntent As Nexamas.UI.Layout.MASSize) As MASInputBoxSkiaBase
            MyBase.SetSize(sizeIntent)
            Return Me
        End Function






#End Region
    End Class

End Namespace
