-- =============================================================================
-- Game Mode Lua API Examples
-- =============================================================================
-- This file demonstrates how to query the editor/game mode state from Lua
-- using the ecs.* Lua API.
--
-- The application can be in one of two modes:
--   - Editor mode: the scene editor is active
--   - Game mode: the game is running (with sub-states: menu, playing, paused)
--
-- These functions allow Lua scripts to adapt their behavior based on the
-- current application mode.
-- =============================================================================

-- =============================================================================
-- Querying the Current Mode
-- =============================================================================

-- Check if we are in editor mode:
if ecs.is_editor_mode() then
    print("Application is in EDITOR mode")
end

-- Check if we are in game mode (any play state):
if ecs.is_game_mode() then
    print("Application is in GAME mode")
end

-- Get the mode as a string:
local mode = ecs.get_game_mode()
print("Current mode: " .. mode)  -- "editor" or "game"

-- =============================================================================
-- Querying the Gameplay State (only meaningful in game mode)
-- =============================================================================

-- Check specific gameplay states:
if ecs.is_game_playing() then
    print("Game is PLAYING")
end

if ecs.is_game_menu() then
    print("Game is in MENU")
end

if ecs.is_game_paused() then
    print("Game is PAUSED")
end

-- Get the play state as a string:
local state = ecs.get_game_play_state()
print("Game play state: " .. state)  -- "menu", "playing", or "paused"

-- =============================================================================
-- Practical Examples
-- =============================================================================

-- Example 1: Only run editor-specific logic in editor mode
function update_editor_ui()
    if ecs.is_editor_mode() then
        -- Show editor UI elements
        print("Updating editor UI...")
    end
end

-- Example 2: Only process game input when game is playing
function process_game_input()
    if ecs.is_game_playing() then
        -- Process player input
        print("Processing game input...")
    end
end

-- Example 3: Show/hide pause menu
function toggle_pause_menu()
    if ecs.is_game_paused() then
        -- Show pause menu overlay
        print("Showing pause menu...")
    else
        -- Hide pause menu
        print("Hiding pause menu...")
    end
end

-- Example 4: Conditional behavior based on mode
function on_entity_clicked(entity_id)
    if ecs.is_editor_mode() then
        -- In editor: select the entity
        print("Selected entity " .. entity_id .. " in editor")
    elseif ecs.is_game_playing() then
        -- In game: interact with the entity
        print("Interacting with entity " .. entity_id)
    end
end

-- =============================================================================
-- Using Mode Queries in Event Handlers
-- =============================================================================

-- Register an event handler that checks mode:
function on_frame_update()
    if ecs.is_game_playing() then
        -- Update game logic
    elseif ecs.is_editor_mode() then
        -- Update editor logic
    end
end

-- =============================================================================
-- Error Handling
-- =============================================================================

-- All functions return valid values even if called at unexpected times:
local m = ecs.get_game_mode()
assert(type(m) == "string", "get_game_mode should return a string")

local s = ecs.get_game_play_state()
assert(type(s) == "string", "get_game_play_state should return a string")

local b1 = ecs.is_editor_mode()
assert(type(b1) == "boolean", "is_editor_mode should return a boolean")

local b2 = ecs.is_game_mode()
assert(type(b2) == "boolean", "is_game_mode should return a boolean")

local b3 = ecs.is_game_playing()
assert(type(b3) == "boolean", "is_game_playing should return a boolean")

local b4 = ecs.is_game_menu()
assert(type(b4) == "boolean", "is_game_menu should return a boolean")

local b5 = ecs.is_game_paused()
assert(type(b5) == "boolean", "is_game_paused should return a boolean")

print("Game mode API examples completed successfully!")
