Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Windows.Forms
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Components.ListPopup
Imports Nexamas.UI.Components.ListRendering
Imports Nexamas.UI.Composition
Imports Nexamas.UI.Controls
Imports Nexamas.UI.FloatRuntime
Imports Nexamas.UI.General
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Motion
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.TextRendering
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Public Nexamas UI single-selection dropdown input.
    ''' The control owns selection, keyboard behavior, and the public item mutation gateway;
    ''' its dropdown float content remains an internal FloatRuntime detail.
    ''' </summary>
    Partial Public NotInheritable Class MASComboBox
        Inherits MASControlBase
        Implements IMASControlRuntimeServicesConsumer
        Implements IMASLayoutParticipant
        Implements IMASIntrinsicSizeContract
        Implements IMASLayoutSemanticBoundsProvider


#Region "Fields"

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

        Private ReadOnly _items As New List(Of String)()
        Private ReadOnly _itemsView As ReadOnlyCollection(Of String)
        Private _selectedIndex As Integer = -1

        Private _hover As Boolean
        Private _pressed As Boolean
        Private _isDropdownOpen As Boolean
        Private _lastPixelBounds As SKRect
        Private _keyboardSearchPrefix As String = String.Empty
        Private _keyboardSearchLastUtc As DateTime = DateTime.MinValue

        Private _floatRuntime As MASFloatRuntime
        Private _comboRuntimeServices As MASControlRuntimeServices
        Private _pressPulseRunner As MASMotionTimelineRunner

        Private ReadOnly _ddService As DropDownService
        Private ReadOnly _shadowPainter As New MASShadowPainter()
        Private ReadOnly _primitives As New MASVisualPrimitivesPainter()

#End Region

#Region "Events"

        ''' <summary>
        ''' Raised when the effective selected index/text changes through the public selection gateway.
        ''' Handler exceptions are contained at the Nexamas UI boundary and routed to diagnostics.
        ''' </summary>
        Public Event SelectionChanged As EventHandler

#End Region

#Region "Constructor"

        Public Sub New()
            MyBase.New()

            _itemsView = _items.AsReadOnly()

            _ddService =
                New DropDownService(
                    hasOwnedFloat:=
                        Function(handle As MASFloatHandle) As Boolean
                            Dim runtime As MASFloatRuntime = ResolveFloatRuntime()
                            If runtime Is Nothing Then Return False
                            If handle Is Nothing Then Return False

                            Try
                                Return runtime.IsOpen(handle)
                            Catch masCaughtException15 As Exception
                                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowInputBoundary(masCaughtException15)
                                Return False
                            End Try
                        End Function,
                    closeOwnedFloat:=
                        Function(handle As MASFloatHandle) As Boolean
                            Dim runtime As MASFloatRuntime = ResolveFloatRuntime()
                            If runtime Is Nothing Then Return False
                            If handle Is Nothing Then Return False

                            Try
                                Return runtime.Close(
                                    handle,
                                    MASFloatCloseReason.Programmatic)
                            Catch masCaughtException1 As Exception
                                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowInputBoundary(masCaughtException1)
                                Return False
                            End Try
                        End Function,
                    showFloat:=
                        Function(ownerKey As String,
                                 content As IMASFloatContent,
                                 boundsPx As SKRect) As MASFloatHandle

                            Return ShowDropDownFloat(
                                ownerKey:=ownerKey,
                                content:=content,
                                boundsPx:=boundsPx)
                        End Function)
        End Sub

#End Region












    End Class

End Namespace
