Option Strict On
Option Explicit On

Imports System.Collections.Generic

Namespace Nexamas.UI.FloatRuntime

    ' ============================================================
    ' MASFloatRuntime Contract
    ' Class: MASFloatSlotState
    '
    ' Purpose:
    '   Stores current and queued Float state for one MASFloatSlot.
    '
    ' Responsibility:
    '   Holds the current lifecycle record and the per-slot queue.
    '
    ' Owns:
    '   Current record reference for one slot.
    '   Queue items for one slot.
    '
    ' Does Not Own:
    '   Policy decisions.
    '   Lifecycle transitions by itself.
    '   Host state.
    '   Presentation.
    '   Input routing.
    '   Focus ownership.
    '
    ' Allowed Dependencies:
    '   MASFloatSlot.
    '   MASFloatLifecycleRecord.
    '   MASFloatQueueItem.
    '   Queue(Of T).
    '
    ' Forbidden Dependencies:
    '   Legacy Overlay classes.
    '   MASFloatCoordinator concrete execution rules.
    '   MASFloatHost.
    '   Feature-owned lifecycle services.
    '
    ' Lifecycle Role:
    '   Runtime state container used by PolicyEngine and Coordinator.
    '
    ' Threading:
    '   Mutated only through runtime-owned state flow.
    '   External synchronization is owned by MASFloatRuntimeState.
    '
    ' Mutation Rules:
    '   Friend/internal mutable state only.
    '   Current must be changed only by Coordinator.
    '   Queue must be changed only after policy approval.
    '
    ' Invariants:
    '   Queue is per Slot.
    '   Current may be Nothing when slot is empty.
    '   Queue is not automatic for every Float.
    '
    ' Failure Rules:
    '   Invalid queued items are rejected before enqueue.
    ' ============================================================
    Friend NotInheritable Class MASFloatSlotState

        Private ReadOnly _queue As New Queue(Of MASFloatQueueItem)()

        Friend ReadOnly Property Slot As MASFloatSlot

        Friend Property Current As MASFloatLifecycleRecord

        Friend ReadOnly Property Queue As Queue(Of MASFloatQueueItem)
            Get
                Return _queue
            End Get
        End Property

        Friend ReadOnly Property HasCurrent As Boolean
            Get
                Return Current IsNot Nothing AndAlso
                       Not Current.State = MASFloatLifecycleState.Closed AndAlso
                       Not Current.State = MASFloatLifecycleState.Disposed
            End Get
        End Property

        Friend ReadOnly Property QueueCount As Integer
            Get
                Return _queue.Count
            End Get
        End Property

        Friend Sub New(slot As MASFloatSlot)
            Me.Slot = slot
        End Sub

        Friend Sub Enqueue(item As MASFloatQueueItem)
            If item Is Nothing Then
                Throw New ArgumentNullException(NameOf(item))
            End If

            _queue.Enqueue(item)
        End Sub

        Friend Function TryDequeue(ByRef item As MASFloatQueueItem) As Boolean
            item = Nothing

            If _queue.Count = 0 Then
                Return False
            End If

            item = _queue.Dequeue()
            Return True
        End Function

        Friend Sub ClearQueue()
            _queue.Clear()
        End Sub

    End Class

End Namespace