Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Values
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.TextInput
Imports SkiaSharp

Namespace Nexamas.UI.Components

    Public NotInheritable Class MASTextBox
        Inherits MASInputBoxSkiaBase

        Private Shared ReadOnly _contract As MASResolvedComponentContract =
            MASArchitectureRuntime.ResolveContractOrThrow(GetType(MASTextBox))

#Region "Ctor / Factory"

        Public Sub New()
        End Sub

        Public Sub New(text As String)
            Me.Text = MASTextBoxTextPolicy.NormalizeSingleLineText(text)
        End Sub

        Friend Shared Function [Default]() As MASTextBox
            Return New MASTextBox()
        End Function

        Public Shared Function Create(Optional text As String = Nothing) As MASTextBox
            Return New MASTextBox(MASTextBoxTextPolicy.NormalizeSingleLineText(text))
        End Function

#End Region

#Region "Public Fluent API"

        Friend Overloads Sub SetBounds(x As Single,
                               y As Single,
                               width As Single,
                               height As Single)
            Me.SetLocalLayoutBoundsInternal(New SKRect(x, y, x + width, y + height))
        End Sub

        Friend Function WithBounds(x As Single,
                           y As Single,
                           width As Single,
                           height As Single) As MASTextBox
            SetBounds(x, y, width, height)
            Return Me
        End Function

        Public Function WithText(value As String) As MASTextBox
            Me.Text = MASTextBoxTextPolicy.NormalizeSingleLineText(value)
            Return Me
        End Function

        Public Function WithPlaceholder(value As String) As MASTextBox
            Me.Placeholder = MASTextBoxTextPolicy.NormalizePlaceholder(value)
            Return Me
        End Function

        Public Function WithMaxLength(value As Integer) As MASTextBox
            Me.MaxLength = MASTextBoxTextPolicy.NormalizeMaxLength(value)
            Return Me
        End Function

        Public Function AsReadOnly() As MASTextBox
            Me.ReadOnly = True
            Return Me
        End Function

        Public Function AsEditable() As MASTextBox
            Me.ReadOnly = False
            Return Me
        End Function

        Public Function WithValidation(state As MASTextValidationState,
                                       Optional message As String = Nothing) As MASTextBox
            Me.SetValidation(state, message)
            Return Me
        End Function

        Public Function WithoutValidation() As MASTextBox
            Me.ClearValidation()
            Return Me
        End Function

        Public Function ShowValidation(value As Boolean) As MASTextBox
            Me.ShowValidationIndicator = value
            Return Me
        End Function

        Friend Function OnTextChanged(handler As Action(Of String)) As MASTextBox
            If handler Is Nothing Then Throw New ArgumentNullException(NameOf(handler))

            AddHandler Me.TextChanged,
                Sub(sender As Object, e As EventArgs)
                    handler(Me.Text)
                End Sub

            Return Me
        End Function

        Public Function FocusInput() As MASTextBox
            Me.FocusText()
            Return Me
        End Function

        Public Function SelectTextAll() As MASTextBox
            Me.SelectAll()
            Return Me
        End Function

#End Region

#Region "Overrides"

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

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

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

        Protected Overrides Function FilterIncomingText(value As String) As String
            Return MASTextBoxTextPolicy.NormalizeSingleLineText(value)
        End Function

#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 MASTextBox
            MyBase.SetSize(sizeIntent)
            Return Me
        End Function






#End Region
    End Class

End Namespace
