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.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.TextRendering
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASComboBox

#Region "Items API"

        ''' <summary>
        ''' Replaces the combo-box items through the supported mutation gateway.
        ''' The dropdown is closed before mutation so no open float keeps stale layout/selection state.
        ''' </summary>
        Public Sub SetItems(items As IEnumerable(Of String))
            Dim previousIndex As Integer = _selectedIndex
            Dim previousText As String = SelectedText

            CloseDropdownForItemMutation()
            ResetKeyboardSearch()
            _items.Clear()

            If items IsNot Nothing Then
                For Each it As String In items
                    _items.Add(If(it, String.Empty))
                Next
            End If

            _selectedIndex = If(_items.Count > 0, 0, -1)

            InvalidateVisual()
            RaiseSelectionChangedIfEffectiveValueChanged(previousIndex, previousText)
        End Sub

        ''' <summary>
        ''' Appends items through the supported mutation gateway. Direct mutation of the item store
        ''' is intentionally not exposed to consumers.
        ''' </summary>
        Public Sub AddItems(items As IEnumerable(Of String))
            If items Is Nothing Then Return

            Dim previousIndex As Integer = _selectedIndex
            Dim previousText As String = SelectedText
            Dim added As Boolean = False

            CloseDropdownForItemMutation()
            ResetKeyboardSearch()

            For Each it As String In items
                _items.Add(If(it, String.Empty))
                added = True
            Next

            If Not added Then Return

            If _selectedIndex = -1 AndAlso _items.Count > 0 Then
                _selectedIndex = 0
            Else
                _selectedIndex = NormalizeSelectionIndex(_selectedIndex)
            End If

            InvalidateVisual()
            RaiseSelectionChangedIfEffectiveValueChanged(previousIndex, previousText)
        End Sub

        ''' <summary>
        ''' Clears all items and closes any owned dropdown float.
        ''' </summary>
        Public Sub ClearItems()
            Dim previousIndex As Integer = _selectedIndex
            Dim previousText As String = SelectedText

            CloseDropdownForItemMutation()
            ResetKeyboardSearch()
            _items.Clear()
            _selectedIndex = -1

            InvalidateVisual()
            RaiseSelectionChangedIfEffectiveValueChanged(previousIndex, previousText)
        End Sub

        Public Sub SelectFirst()
            If _items.Count > 0 Then
                SelectedIndex = 0
            Else
                SelectedIndex = -1
            End If
        End Sub

#End Region

    End Class

End Namespace
