--[[
Save/Load Lua API Example
==========================
Demonstrates how to use ecs.register_save_callback and
ecs.register_load_callback to persist custom Lua state.

The save/load system automatically calls all registered callbacks
when the player saves or loads a game from the pause menu.

Usage:
    require('save_load_example')  -- in your game script
--]]

-- =====================================================================
-- Example 1: Quest state persistence
-- =====================================================================

-- Track quest state in Lua
local quest_state = {
    active_quests = {},
    completed_quests = {},
    quest_stages = {}
}

-- Register save callback for quest data
ecs.register_save_callback('quest_data', function()
    return {
        active_quests = quest_state.active_quests,
        completed_quests = quest_state.completed_quests,
        quest_stages = quest_state.quest_stages
    }
end)

-- Register load callback for quest data
ecs.register_load_callback('quest_data', function(data)
    if data then
        quest_state.active_quests = data.active_quests or {}
        quest_state.completed_quests = data.completed_quests or {}
        quest_state.quest_stages = data.quest_stages or {}
        print('Quest state restored from save')
    end
end)

-- Helper functions for quest management
function start_quest(quest_id, quest_name)
    quest_state.active_quests[quest_id] = quest_name
    quest_state.quest_stages[quest_id] = 1
    print('Started quest:', quest_name)
end

function advance_quest(quest_id)
    local stage = quest_state.quest_stages[quest_id]
    if stage then
        quest_state.quest_stages[quest_id] = stage + 1
        print('Advanced quest', quest_id, 'to stage', stage + 1)
    end
end

function complete_quest(quest_id)
    local name = quest_state.active_quests[quest_id]
    if name then
        quest_state.active_quests[quest_id] = nil
        quest_state.quest_stages[quest_id] = nil
        table.insert(quest_state.completed_quests, quest_id)
        print('Completed quest:', name)
    end
end

-- =====================================================================
-- Example 2: NPC relationship tracking
-- =====================================================================

local npc_relationships = {}

ecs.register_save_callback('npc_relationships', function()
    return npc_relationships
end)

ecs.register_load_callback('npc_relationships', function(data)
    if data then
        npc_relationships = data
        print('NPC relationships restored from save')
    end
end)

function set_npc_relationship(npc_id, value)
    npc_relationships[npc_id] = value
end

function get_npc_relationship(npc_id)
    return npc_relationships[npc_id] or 0
end

-- =====================================================================
-- Example 3: World state flags
-- =====================================================================

local world_flags = {}

ecs.register_save_callback('world_flags', function()
    return world_flags
end)

ecs.register_load_callback('world_flags', function(data)
    if data then
        world_flags = data
        print('World flags restored from save')
    end
end)

function set_world_flag(flag_name, value)
    world_flags[flag_name] = value
end

function get_world_flag(flag_name)
    return world_flags[flag_name] or false
end

-- =====================================================================
-- Example 4: Reacting to save/load events
-- =====================================================================

ecs.subscribe_event('save_game_requested', function()
    print('Save is about to begin - syncing character positions...')
    -- Sync any runtime state that needs to be up-to-date before save
end)

ecs.subscribe_event('game_saved', function()
    print('Game was saved successfully!')
end)

ecs.subscribe_event('load_game_requested', function()
    print('Load is about to begin - cleaning up runtime state...')
    -- Clean up any runtime-only state before load
end)

ecs.subscribe_event('game_loaded', function()
    print('Game was loaded successfully!')
    -- Re-initialize any runtime systems that depend on loaded state
end)

-- =====================================================================
-- Example 5: Player stats extension
-- =====================================================================

-- Extend player stats with custom Lua-tracked values
local player_extras = {
    reputation = 0,
    discovery_percentage = 0.0,
    visited_locations = {}
}

ecs.register_save_callback('player_extras', function()
    return player_extras
end)

ecs.register_load_callback('player_extras', function(data)
    if data then
        player_extras = data
        print('Player extras restored from save')
    end
end)

function add_reputation(amount)
    player_extras.reputation = player_extras.reputation + amount
    print('Reputation is now:', player_extras.reputation)
end

function visit_location(location_name)
    if not player_extras.visited_locations[location_name] then
        player_extras.visited_locations[location_name] = true
        local count = 0
        for _, _ in pairs(player_extras.visited_locations) do
            count = count + 1
        end
        -- Estimate discovery percentage (example: 100 total locations)
        player_extras.discovery_percentage = (count / 100) * 100
        print('Discovered new location:', location_name)
    end
end

-- =====================================================================
-- Initialization
-- =====================================================================

print('Save/Load example loaded')
print('Registered callbacks: quest_data, npc_relationships, world_flags, player_extras')
