-- PageRegistry.lua
--
-- Provides a central registry for all UI pages that are built on the new
-- PageBase framework.
--
-- GOAL:
-- This registry will eventually be used by the main window's navigation logic
-- to show and hide pages. For now, it serves as the foundational database
-- where new, migrated pages will be registered.
--
-- The main window (`UI/SinsToolkit_Window.lua`) will initialize this registry.
-- As pages are migrated out of the main window file, they will be registered here.
--
-- FUTURE MIGRATION EXAMPLE (in a new file like UI/Pages/General.lua):
--
--   local addonName, SinsToolkit = ...
--   local Page = setmetatable({}, { __index = SinsToolkit.UI.Pages.Base })
--   SinsToolkit.UI.Pages.General = Page
--
--   -- ... implement Page methods ...
--
--   -- At the end of the file, register the newly created page object.
--   SinsToolkit.UI.Pages.Registry:RegisterPage("General", Page)
--

local addonName, SinsToolkit = ...

-- Ensure the namespace exists.
SinsToolkit.UI = SinsToolkit.UI or {}
SinsToolkit.UI.Pages = SinsToolkit.UI.Pages or {}

local PageRegistry = {}
SinsToolkit.UI.Pages.Registry = PageRegistry

local pages = {}
local orderedPageIDs = {}

--[[
    Registers a page object with the framework.
    @param id A unique string identifier for the page (e.g., "General", "Gear").
    @param page The page object, which should inherit from PageBase.
--]]
function PageRegistry:RegisterPage(id, page)
    if type(id) ~= "string" or id == "" then return end
    if type(page) ~= "table" then return end
    if pages[id] then return end -- Do not allow re-registration.

    pages[id] = page
    orderedPageIDs[#orderedPageIDs + 1] = id
end

--[[
    Retrieves a registered page object by its ID.
    @param id The unique string identifier of the page.
    @return The page object, or nil if not found.
--]]
function PageRegistry:GetPage(id)
    return pages[id]
end

--[[
    Retrieves a list of all registered page objects in registration order.
    @return A new table containing all registered page objects.
--]]
function PageRegistry:GetPages()
    local list = {}
    for _, id in ipairs(orderedPageIDs) do
        list[#list + 1] = pages[id]
    end
    return list
end

--[[
    Shows a specific page and hides all others.
    NOTE: This is part of the future architecture and is not yet integrated
    into the main window's navigation. It is safe to call but will not
    affect the legacy page navigation system.
    @param id The unique string identifier of the page to show.
--]]
function PageRegistry:ShowPage(id)
    for pageID, page in pairs(pages) do
        if pageID == id then
            if page.Show then page:Show() end
        else
            if page.Hide then page:Hide() end
        end
    end
end
