using System;
using System.Collections.Generic;
namespace AlicizaX.UI.Runtime
{
///
/// UI State Machine - Validates state transitions for UI lifecycle.
/// Helps catch bugs early and ensures proper UI lifecycle management.
///
internal static class UIStateMachine
{
private static readonly Dictionary> _validTransitions = new()
{
[UIState.Uninitialized] = new() { UIState.CreatedUI },
[UIState.CreatedUI] = new() { UIState.Loaded },
[UIState.Loaded] = new() { UIState.Initialized },
[UIState.Initialized] = new() { UIState.Opened },
[UIState.Opened] = new() { UIState.Closed, UIState.Destroying },
[UIState.Closed] = new() { UIState.Opened, UIState.Destroying },
[UIState.Destroying] = new() { UIState.Destroyed },
[UIState.Destroyed] = new() { }
};
///
/// Checks if a state transition is valid.
///
/// Current state
/// Target state
/// True if transition is valid
public static bool IsValidTransition(UIState from, UIState to)
{
return _validTransitions.TryGetValue(from, out var validStates) && validStates.Contains(to);
}
///
/// Validates a state transition and logs error if invalid.
///
/// Name of the UI for logging
/// Current state
/// Target state
/// True if transition is valid
public static bool ValidateTransition(string uiName, UIState from, UIState to)
{
if (IsValidTransition(from, to))
return true;
Log.Error($"[UI] Invalid state transition for {uiName}: {from} -> {to}");
return false;
}
///
/// Gets all valid next states from the current state.
///
/// Current state
/// Set of valid next states
public static HashSet GetValidNextStates(UIState currentState)
{
return _validTransitions.TryGetValue(currentState, out var states)
? states
: new HashSet();
}
///
/// Gets a human-readable description of the state.
///
public static string GetStateDescription(UIState state)
{
return state switch
{
UIState.Uninitialized => "Not yet created",
UIState.CreatedUI => "UI logic created, awaiting resource load",
UIState.Loaded => "Resources loaded, awaiting initialization",
UIState.Initialized => "Initialized, ready to open",
UIState.Opened => "Currently visible and active",
UIState.Closed => "Hidden but cached",
UIState.Destroying => "Being destroyed",
UIState.Destroyed => "Fully destroyed",
_ => "Unknown state"
};
}
}
}