-- =============================================================================
-- Dialogue: Event Subscription & Choice Handling
-- =============================================================================
-- This example demonstrates how to subscribe to dialogue-related events
-- and handle player choices from Lua.
--
-- The DialogueSystem fires events when the player interacts with the
-- dialogue box. You can subscribe to these events to drive game logic.
--
-- Dialogue-related events you can subscribe to:
--   "dialogue_show"     - Fired when dialogue should be displayed
--   "dialogue_hide"     - Fired when dialogue is hidden
--
-- Note: choice and dismiss callbacks are handled via the singleton's
-- onChoiceSelected / onDismissed callbacks (C++ side). From Lua, you
-- can poll is_active() or subscribe to your own custom events.
-- =============================================================================

-- ---------------------------------------------------------------------------
-- 1. Subscribe to dialogue show events (for logging/tracking)
-- ---------------------------------------------------------------------------

local show_sub = ecs.subscribe_event("dialogue_show", function(event, params)
    local text = params.text or ""
    local speaker = params.speaker or "Unknown"

    print("[Dialogue Log] " .. speaker .. ": \"" .. text .. "\"")
end)

print("Subscribed to dialogue_show events for logging (ID: " .. show_sub .. ")")

-- ---------------------------------------------------------------------------
-- 2. Subscribe to dialogue hide events
-- ---------------------------------------------------------------------------

local hide_sub = ecs.subscribe_event("dialogue_hide", function(event, params)
    print("[Dialogue Log] Dialogue was hidden")
end)

print("Subscribed to dialogue_hide events (ID: " .. hide_sub .. ")")

-- ---------------------------------------------------------------------------
-- 3. Example: Branching dialogue with choice handling
-- ---------------------------------------------------------------------------
-- This shows a complete flow: show dialogue -> handle choice -> react.
-- Since choice selection happens via C++ callbacks, from Lua we can
-- use a custom event pattern or poll is_active().

function show_branching_dialogue()
    -- Step 1: Show the dialogue with choices
    ecs.dialogue.show(
        "You see a dark cave entrance. What do you do?",
        { "Enter the cave", "Look around first", "Leave" },
        "Narrator"
    )

    print("Branching dialogue shown. Waiting for player choice...")

    -- Step 2: In a real game, you'd check the result asynchronously.
    -- For this example, we demonstrate the pattern.
    -- (Use ecs.dialogue.is_active() to poll, or wire C++ callbacks
    --  to send custom events when choices are made.)
end

show_branching_dialogue()

-- ---------------------------------------------------------------------------
-- 4. Example: NPC greeting with follow-up
-- ---------------------------------------------------------------------------

function npc_greeting(npc_name, greeting_text)
    -- Show initial greeting
    ecs.dialogue.show(
        greeting_text,
        { "Who are you?", "Tell me about this place", "Goodbye" },
        npc_name
    )

    -- The choice handling would be done via C++ callback wiring or
    -- by polling is_active() in your game loop.
end

npc_greeting("Elder Marcus", "Ah, a new face in our village! Welcome, traveler.")

-- ---------------------------------------------------------------------------
-- 5. Example: Chain multiple dialogue lines
-- ---------------------------------------------------------------------------

function show_dialogue_sequence(lines)
    for i, line in ipairs(lines) do
        ecs.dialogue.show(line.text, line.choices or {}, line.speaker or "")
        print("  [Line " .. i .. "] " .. (line.speaker or "") .. ": \"" .. line.text .. "\"")
    end
end

local intro_sequence = {
    { text = "The storm rages outside.", speaker = "Narrator" },
    { text = "You find shelter in an abandoned tower.", speaker = "Narrator" },
    { text = "A voice calls from the shadows...", speaker = "Narrator" },
    { text = "Who dares enter my sanctuary?", speaker = "Mysterious Voice",
      choices = { "I seek shelter from the storm", "I mean no harm", "I was sent here" } }
}

show_dialogue_sequence(intro_sequence)

-- =============================================================================
-- Summary
-- =============================================================================
-- To handle dialogue events from Lua:
--   1. Subscribe to "dialogue_show" events for logging or side effects
--   2. Subscribe to "dialogue_hide" events for cleanup
--   3. Use ecs.dialogue.show() / ecs.dialogue.hide() for direct control
--   4. For choice reactions, wire C++ callbacks to Lua events, or
--      poll ecs.dialogue.is_active() in your update loop
--
-- EventParams uses flat key-value pairs. Type metadata is available
-- via params._types table (e.g., params._types.text = "string").
-- =============================================================================

print("Dialogue event subscription examples completed!")
