EbonholdHub.ViewRouter = {}

local views       = {}
local currentName = nil
local container   = nil
local listeners   = {}

function EbonholdHub.ViewRouter.SetContainer(frame)
    container = frame
end

function EbonholdHub.ViewRouter.Register(name, view)
    views[name] = view
end

function EbonholdHub.ViewRouter.OnChanged(fn)
    listeners[#listeners + 1] = fn
end

local function NotifyChanged(name)
    for i = 1, #listeners do
        listeners[i](name)
    end
end

function EbonholdHub.ViewRouter.Show(name, context)
    if not container then return end
    local view = views[name]
    if not view then return end

    if currentName and currentName ~= name then
        local prev = views[currentName]
        if prev and prev.Hide then prev.Hide() end
    end
    currentName = name
    view.Show(container, context)
    NotifyChanged(name)
end

function EbonholdHub.ViewRouter.Current()
    return currentName
end
