-- =============================================================================
-- Dialogue: Direct Singleton API Control
-- =============================================================================
-- This example demonstrates how to control the DialogueSystem directly
-- via its singleton Lua API, without using the EventBus.
--
-- The DialogueSystem singleton exposes these functions:
--   show(text, choices, speaker)  - Display dialogue
--   hide()                         - Dismiss dialogue
--   progress()                     - Click-to-progress (no-choices mode)
--   select_choice(index)           - Select a choice (1-based)
--   is_active()                    - Check if dialogue is active
--   get_settings()                 - Get visual settings table
--   set_settings(table)            - Set visual settings
--   save_settings(path)            - Save settings to JSON
--   load_settings(path)            - Load settings from JSON
--
-- Settings table fields:
--   font_name, font_size, speaker_font_size,
--   background_opacity, box_height_fraction, box_position_fraction
-- =============================================================================

-- ---------------------------------------------------------------------------
-- 1. Show dialogue directly via the singleton API
-- ---------------------------------------------------------------------------

ecs.dialogue.show("This dialogue was shown directly via the singleton API!",
                  {},
                  "Lua Script")

print("Dialogue shown via direct API. Active: " .. tostring(ecs.dialogue.is_active()))

-- ---------------------------------------------------------------------------
-- 2. Read and modify visual settings
-- ---------------------------------------------------------------------------

local settings = ecs.dialogue.get_settings()
print("Current font: " .. settings.font_name)
print("Current font size: " .. settings.font_size)

-- Change appearance settings
settings.font_name = "Jupiteroid-Regular.ttf"
settings.font_size = 24.0
settings.speaker_font_size = 20.0
settings.background_opacity = 0.85
settings.box_height_fraction = 0.25
settings.box_position_fraction = 0.75

ecs.dialogue.set_settings(settings)
print("Dialogue settings updated")

-- ---------------------------------------------------------------------------
-- 3. Show dialogue with choices
-- ---------------------------------------------------------------------------

ecs.dialogue.show(
    "Where would you like to travel?",
    { "The Forest", "The Village", "The Mountains" },
    "Guide"
)

print("Dialogue with choices shown. Active: " .. tostring(ecs.dialogue.is_active()))

-- Simulate player selecting choice 1
ecs.dialogue.select_choice(1)
print("After choice: Active = " .. tostring(ecs.dialogue.is_active()))

-- ---------------------------------------------------------------------------
-- 4. Show and dismiss (no choices)
-- ---------------------------------------------------------------------------

ecs.dialogue.show("A mysterious voice echoes through the chamber...")
print("Narration shown. Active: " .. tostring(ecs.dialogue.is_active()))

-- Simulate click-to-progress
ecs.dialogue.progress()
print("After progress: Active = " .. tostring(ecs.dialogue.is_active()))

-- ---------------------------------------------------------------------------
-- 5. Hide dialogue immediately
-- ---------------------------------------------------------------------------

ecs.dialogue.show("This will be cut short.", {}, "Interrupter")
ecs.dialogue.hide()
print("After hide: Active = " .. tostring(ecs.dialogue.is_active()))

-- ---------------------------------------------------------------------------
-- 6. Save and load settings
-- ---------------------------------------------------------------------------

-- Save current settings to dialogue.json
local saved = ecs.dialogue.save_settings("dialogue.json")
print("Settings saved: " .. tostring(saved))

-- Load settings back (or from a different file)
local loaded = ecs.dialogue.load_settings("dialogue.json")
print("Settings loaded: " .. tostring(loaded))

-- ---------------------------------------------------------------------------
-- 7. Practical: Configure dialogue appearance for a scene
-- ---------------------------------------------------------------------------

function setup_dialogue_for_scene(scene_type)
    local scene_settings = ecs.dialogue.get_settings()

    if scene_type == "dark_cave" then
        scene_settings.background_opacity = 0.95
        scene_settings.box_height_fraction = 0.20
        scene_settings.font_size = 22.0
    elseif scene_type == "bright_outdoor" then
        scene_settings.background_opacity = 0.70
        scene_settings.box_height_fraction = 0.30
        scene_settings.font_size = 26.0
    elseif scene_type == "intimate_conversation" then
        scene_settings.background_opacity = 0.90
        scene_settings.box_height_fraction = 0.22
        scene_settings.font_size = 24.0
        scene_settings.speaker_font_size = 22.0
    end

    ecs.dialogue.set_settings(scene_settings)
    print("Dialogue configured for scene: " .. scene_type)
end

setup_dialogue_for_scene("dark_cave")
setup_dialogue_for_scene("bright_outdoor")

-- ---------------------------------------------------------------------------
-- 8. Practical: Show NPC dialogue with dynamic choices
-- ---------------------------------------------------------------------------

function show_npc_dialogue(npc_name, text, choices)
    ecs.dialogue.show(text, choices or {}, npc_name)
end

-- Merchant interaction
show_npc_dialogue(
    "Merchant",
    "Welcome to my shop! Best wares in town.",
    { "Buy Sword (50 gold)", "Buy Shield (30 gold)", "Leave" }
)

-- Guard interaction
show_npc_dialogue(
    "Guard",
    "Halt! Who goes there?",
    { "I'm a traveler", "I'm looking for the inn", "None of your business" }
)

-- ---------------------------------------------------------------------------
-- 9. Practical: Update dialogue based on game events
-- ---------------------------------------------------------------------------

function update_dialogue_after_event(event_type)
    if event_type == "quest_accepted" then
        ecs.dialogue.show(
            "Excellent! Bring me the artifact and you'll be rewarded.",
            { "Where do I find it?", "I'm on my way!", "Tell me more" },
            "Quest Giver"
        )
    elseif event_type == "combat_start" then
        ecs.dialogue.show(
            "Enemies approach! Prepare for battle!",
            {},
            "Companion"
        )
    elseif event_type == "level_up" then
        ecs.dialogue.show(
            "You feel a surge of power! You have reached a new level.",
            { "View skills", "Continue" },
            "System"
        )
    end
end

update_dialogue_after_event("quest_accepted")

-- =============================================================================
-- Summary
-- =============================================================================
-- Direct singleton API vs EventBus approach:
--
-- Singleton API (ecs.dialogue.*):
--   - Direct control over showing/hiding/progressing/selecting
--   - Read/write visual settings (font, size, opacity, position)
--   - Save/load settings to dialogue.json
--   - Best for scripted sequences and direct game logic
--
-- EventBus (ecs.send_event "dialogue_show"):
--   - Triggers dialogue via the event system
--   - Good for decoupled systems (e.g., NPCs, triggers, quests)
--   - Same underlying singleton, just a different entry point
--
-- Best practice: Use the singleton API for direct control, and EventBus
-- for triggering dialogue from other systems.
-- =============================================================================

print("Dialogue singleton API examples completed!")
