Option Strict On
Option Explicit On

Imports System.Collections.Generic

Namespace Nexamas.UI.FloatRuntime

    ' ============================================================
    ' MASFloatRuntime Contract
    ' Class: MASFloatRuntimeState
    '
    ' Purpose:
    '   Stores internal state for MASFloatRuntime across all slots.
    '
    ' Responsibility:
    '   Provides slot state lookup, handle id generation, generation
    '   generation, and queue sequence generation for runtime internals.
    '
    ' Owns:
    '   Slot states.
    '   Runtime counters.
    '
    ' Does Not Own:
    '   Registered descriptors.
    '   Policy decisions.
    '   Host visual tree.
    '   Input routing.
    '   Focus ownership.
    '   Presentation calculation.
    '   Transition execution.
    '
    ' Allowed Dependencies:
    '   MASFloatSlot.
    '   MASFloatSlotState.
    '   Dictionary.
    '   Enum.
    '
    ' Forbidden Dependencies:
    '   Legacy Overlay classes.
    '   OverlayHost.
    '   OverlayManager.
    '   OverlayContent.
    '   OverlayLayer.
    '   OverlayService.
    '   Feature-owned lifecycle services.
    '
    ' Lifecycle Role:
    '   Internal state owned by MASFloatRuntime/Coordinator.
    '   Used as the state source for policy and lifecycle orchestration.
    '
    ' Threading:
    '   Provides a SyncRoot for runtime-owned synchronization.
    '   Lifecycle mutation should still happen on UI thread after marshaling.
    '
    ' Mutation Rules:
    '   Friend/internal only.
    '   Slot state mutation must happen through Coordinator-controlled flow.
    '
    ' Invariants:
    '   Every MASFloatSlot has exactly one MASFloatSlotState.
    '   Generated ids, generations, and queue sequences are positive.
    '   No public consumer can access this state.
    '
    ' Failure Rules:
    '   Missing slot state is created during construction and should not occur.
    ' ============================================================
    Friend NotInheritable Class MASFloatRuntimeState

        Private ReadOnly _slotStates As New Dictionary(Of MASFloatSlot, MASFloatSlotState)()

        Private _nextHandleId As Long = 0L

        Private _nextGeneration As Long = 0L

        Private _nextQueueSequence As Long = 0L

        Friend ReadOnly Property SyncRoot As Object

        Friend Sub New()
            Me.SyncRoot = New Object()

            For Each slot As MASFloatSlot In DirectCast(
                [Enum].GetValues(GetType(MASFloatSlot)),
                MASFloatSlot()
            )
                _slotStates(slot) = New MASFloatSlotState(slot)
            Next
        End Sub

        Friend Function GetSlotState(slot As MASFloatSlot) As MASFloatSlotState
            Dim state As MASFloatSlotState = Nothing

            If Not _slotStates.TryGetValue(slot, state) Then
                state = New MASFloatSlotState(slot)
                _slotStates(slot) = state
            End If

            Return state
        End Function

        Friend Function GetAllSlotStates() As IReadOnlyList(Of MASFloatSlotState)
            Dim result As New List(Of MASFloatSlotState)()

            For Each pair As KeyValuePair(Of MASFloatSlot, MASFloatSlotState) In _slotStates
                result.Add(pair.Value)
            Next

            Return result.AsReadOnly()
        End Function

        Friend Function NextHandleId() As Long
            _nextHandleId += 1L
            Return _nextHandleId
        End Function

        Friend Function NextGeneration() As Long
            _nextGeneration += 1L
            Return _nextGeneration
        End Function

        Friend Function NextQueueSequence() As Long
            _nextQueueSequence += 1L
            Return _nextQueueSequence
        End Function

    End Class

End Namespace