-- =============================================================================
-- Dialogue Lua API Examples
-- =============================================================================
-- This file demonstrates how to use the ecs.dialogue API for managing
-- in-game dialogue boxes with text, choices, speaker names, and settings.
--
-- The dialogue system provides:
--   - Show/hide dialogue boxes with text and optional choices
--   - Speaker name display
--   - Choice selection and progression
--   - Settings management (font, opacity, positioning)
--   - Settings persistence (save/load to JSON)
-- =============================================================================

-- =============================================================================
-- Basic Dialogue Display
-- =============================================================================

-- Show a simple dialogue with text only
ecs.dialogue.show("Hello, traveler! Welcome to our village.")

print("Showed basic dialogue")

-- Show dialogue with speaker name
ecs.dialogue.show("I have a quest for you.", {}, "Elder Marcus")

print("Showed dialogue with speaker")

-- =============================================================================
-- Dialogue with Choices
-- =============================================================================

-- Show dialogue with multiple choices
ecs.dialogue.show("What would you like to do?", {
    "Ask about the quest",
    "Browse his wares",
    "Say goodbye"
}, "Shopkeeper")

print("Showed dialogue with choices")

-- Select a choice (simulates player clicking option 1)
ecs.dialogue.select_choice(1)

print("Selected choice 1")

-- =============================================================================
-- Hiding Dialogue
-- =============================================================================

-- Hide the current dialogue
ecs.dialogue.hide()

print("Dialogue hidden")

-- =============================================================================
-- Checking Dialogue State
-- =============================================================================

-- Check if dialogue is currently active
local active = ecs.dialogue.is_active()
print("Dialogue active: " .. tostring(active))

-- =============================================================================
-- Progressing Through Dialogue
-- =============================================================================

-- Show a multi-line narration and progress through it
ecs.dialogue.show("The sun sets over the horizon...")
ecs.dialogue.progress()

ecs.dialogue.show("A cool breeze sweeps through the valley...")
ecs.dialogue.progress()

ecs.dialogue.show("You hear footsteps in the distance.")
ecs.dialogue.progress()

print("Progressed through narration")

-- =============================================================================
-- Dialogue Settings Management
-- =============================================================================

-- Get current settings
local settings = ecs.dialogue.get_settings()
print("Current settings:")
print("  Font: " .. settings.font_name)
print("  Font size: " .. settings.font_size)
print("  Speaker font size: " .. settings.speaker_font_size)
print("  Background opacity: " .. settings.background_opacity)
print("  Box height fraction: " .. settings.box_height_fraction)
print("  Box position fraction: " .. settings.box_position_fraction)

-- Modify settings
ecs.dialogue.set_settings({
    font_name = "Jupiteroid-Regular.ttf",
    font_size = 20.0,
    speaker_font_size = 18.0,
    background_opacity = 0.85,
    box_height_fraction = 0.25,
    box_position_fraction = 0.75
})

print("Updated dialogue settings")

-- Verify the changes
local updated = ecs.dialogue.get_settings()
print("Updated font size: " .. updated.font_size)

-- =============================================================================
-- Saving and Loading Settings
-- =============================================================================

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

-- Save settings to a custom path
local saved_custom = ecs.dialogue.save_settings("my_dialogue_config.json")
print("Settings saved to custom path: " .. tostring(saved_custom))

-- Load settings from default path
local loaded = ecs.dialogue.load_settings()
print("Settings loaded: " .. tostring(loaded))

-- Load settings from custom path
local loaded_custom = ecs.dialogue.load_settings("my_dialogue_config.json")
print("Settings loaded from custom path: " .. tostring(loaded_custom))

-- =============================================================================
-- Practical: Dialogue Sequence with Choices
-- =============================================================================

-- A simple dialogue tree simulation
function run_dialogue_tree()
    -- Node 1: Greeting
    ecs.dialogue.show("Greetings, adventurer! How can I help you?", {
        "Tell me about the local area",
        "I need supplies",
        "I'm just passing through"
    }, "Innkeeper")

    -- Simulate player choosing option 1
    ecs.dialogue.select_choice(1)

    -- Node 2: Response to choice 1
    ecs.dialogue.show("Ah, you must be new here! This town has a rich history. "
        .. "To the north lies the ancient forest, and to the east, "
        .. "the old ruins.", {}, "Innkeeper")

    ecs.dialogue.progress()

    -- Node 3: Offer quest
    ecs.dialogue.show("If you're looking for adventure, I heard the ruins "
        .. "hold a legendary treasure. But beware of the traps!", {
        "I'll check it out!",
        "Sounds too dangerous"
    }, "Innkeeper")

    -- Simulate player choosing option 1
    ecs.dialogue.select_choice(1)

    -- Node 4: Final response
    ecs.dialogue.show("Excellent! Good luck on your journey, adventurer!", {}, "Innkeeper")
    ecs.dialogue.progress()

    -- End dialogue
    ecs.dialogue.hide()

    print("Dialogue tree completed")
end

run_dialogue_tree()

-- =============================================================================
-- API Reference
-- =============================================================================
--
-- ecs.dialogue.show(text, choices?, speaker?)
--   text    - string, the dialogue text to display
--   choices - optional table of strings, player response options
--   speaker - optional string, name of the speaking character
--
-- ecs.dialogue.hide()
--   Hides the current dialogue box.
--
-- ecs.dialogue.is_active() -> bool
--   Returns true if a dialogue is currently being displayed.
--
-- ecs.dialogue.select_choice(index)
--   Simulates selecting a choice by its 1-based index.
--
-- ecs.dialogue.progress()
--   Advances to the next line of dialogue (click-to-dismiss).
--
-- ecs.dialogue.get_settings() -> table
--   Returns a table with fields:
--     font_name, font_size, speaker_font_size,
--     background_opacity, box_height_fraction, box_position_fraction
--
-- ecs.dialogue.set_settings(settings_table)
--   Updates dialogue display settings. Partial tables are accepted.
--
-- ecs.dialogue.save_settings(path?) -> bool
--   Saves current settings to JSON. Default path: "dialogue.json"
--
-- ecs.dialogue.load_settings(path?) -> bool
--   Loads settings from JSON. Default path: "dialogue.json"
-- =============================================================================

print("Dialogue examples completed successfully!")
