local _, ns = ...
local BDT = ns.DataTexts
local L = BDT.L
BDT.ModuleRegistry = {}

----------------------------------------------------
-- Helper function to add a separator/newline
----------------------------------------------------
function BDT.ModuleRegistry:AddModuleConfigSeparator(moduleName, label)
    self:AddModuleConfigSetting(moduleName, "header", label or " ", nil, nil)
end

----------------------------------------------------
-- Module Settings (for modules that need config)
----------------------------------------------------
function BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, settingType, label, settingKey, defaultValue, ...)
    -- Queue settings to be added to the config system
    BDT.queuedModuleSettings = BDT.queuedModuleSettings or {}
    BDT.queuedModuleSettings[moduleName] = BDT.queuedModuleSettings[moduleName] or {}
    
    local setting = {
        settingType = settingType,
        label = label,
        settingKey = settingKey,
        defaultValue = defaultValue,
    }

    -- Handle extra parameters for different setting types
    if settingType == "select" or settingType == "fontOutline" then
        setting.values = select(1, ...)
    elseif settingType == "range" or settingType == "fontSize" then
        setting.min = select(1, ...)
        setting.max = select(2, ...)
        setting.step = select(3, ...)
    elseif settingType == "description" then
        -- Description can have optional fontSize parameter
        setting.fontSize = select(1, ...) or "medium"
    end
    
    table.insert(BDT.queuedModuleSettings[moduleName], setting)
end

----------------------------------------------------
-- Create Module List
----------------------------------------------------
function BDT.ModuleRegistry:CreateModuleList()
    if BDT.cache.moduleNamesDirty or not BDT.cache.moduleNames then
        wipe(BDT.cache.moduleNames)
        for name in pairs(BDT.modules) do
            tinsert(BDT.cache.moduleNames, name)
        end
        table.sort(BDT.cache.moduleNames)
        BDT.cache.moduleNamesDirty = false
    end
end

----------------------------------------------------
-- Module Settings Exclusions
----------------------------------------------------
--[[ Note: Not currently excluding any modules, but keeping the framework.
BDT.ModuleRegistry.excludedModules = {
    ["HidingBar1"] = true,
}

function BDT.ModuleRegistry:ExcludedModule(moduleName)
    if self.excludedModules[moduleName] then return true end
    return false
end]]

----------------------------------------------------
-- Get Module Frame Strata
----------------------------------------------------
function BDT.ModuleRegistry:GetModuleFrameStrata(moduleName)
    local strata = BDT:GetModuleSetting(moduleName, "frameStrata", "MEDIUM")
    -- Validate strata value
    if not BDT.cache.validStratas[strata] then
        return "MEDIUM"
    end
    return strata
end

----------------------------------------------------
-- Global Module Settings
----------------------------------------------------
function BDT.ModuleRegistry:GlobalModuleSettings(moduleName)
    -- Text Settings
    BDT.ModuleRegistry:AddModuleConfigSeparator(moduleName, L["Text Color"])
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "checkbox", L["Override Text Color"], "overrideTextColor", false)
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "color", L["Text Custom Color"], "customTextColor", "#FFFFFF")

	-- Font Settings
    BDT.ModuleRegistry:AddModuleConfigSeparator(moduleName, L["Font Settings"])
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "checkbox", L["Override Global Font"], "overrideFont", false)
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "font", L["Display Font:"], "font", "Friz Quadrata TT")
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "fontSize", L["Font Size"], "fontSize", 12, 4, 40, 1)
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "fontOutline", L["Font Outline"], "fontOutline", "NONE", {
        ["NONE"] = L["None"],
        ["OUTLINE"] = "Outline",
        ["THICKOUTLINE"] = "Thick Outline",
        ["MONOCHROME"] = "Monochrome",
        ["OUTLINE, MONOCHROME"] = "Outline + Monochrome",
        ["THICKOUTLINE, MONOCHROME"] = "Thick Outline + Monochrome",
    })

	-- Slot Controls
    BDT.ModuleRegistry:AddModuleConfigSeparator(moduleName, L["Slot Controls"])
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "anchorPoint", L["Anchor Point"], "anchorPoint", "CENTER")
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "frameStrata", L["Frame Strata"], "frameStrata", "MEDIUM")
end

----------------------------------------------------
-- Module Registration
----------------------------------------------------
function BDT.ModuleRegistry:RegisterDatatext(name, module)
    BDT.modules[name] = module
    BDT.cache.moduleNamesDirty = true
end

----------------------------------------------------
-- Update Frame Strata for All Bars
----------------------------------------------------
function BDT.ModuleRegistry:UpdateAllModuleStrata()
    for _, bar in pairs(BDT.bars) do
        self:UpdateBarStrata(bar)
    end
end

----------------------------------------------------
-- Update Frame Strata for a Single Bar
----------------------------------------------------
function BDT.ModuleRegistry:UpdateBarStrata(bar)
    if not (bar and bar.slots) then return end

    for _, slot in ipairs(bar.slots) do
        BDT.ModuleRegistry:UpdateModuleStrata(slot)
    end
end

----------------------------------------------------
-- Apply Strata to a Single Slot
----------------------------------------------------
function BDT.ModuleRegistry:UpdateModuleStrata(slot)
    if not (slot.module and slot.module ~= "(spacer)" and BDT.modules[slot.module]) then return end

    local strata = BDT.ModuleRegistry:GetModuleFrameStrata(slot.module)

    slot:SetFrameStrata(strata)

    if slot.moduleFrame then
        slot.moduleFrame:SetFrameStrata(strata)
    end

    if slot.secureButton then
        slot.secureButton:SetFrameStrata(strata)
    end
end

----------------------------------------------------
-- Update All Modules
----------------------------------------------------
function BDT.ModuleRegistry:UpdateAllModules()
    for _, bar in pairs(BDT.bars) do
        if bar and bar.slots then
            for _, slot in ipairs(bar.slots) do
                if slot.moduleFrame and slot.moduleFrame.Update then
                    slot.moduleFrame:Update()
                end
            end
        end
    end
end
