-- Assistant Global Font scoped detail registry.
-- Loaded before MSUF_AssistantRegistry_GlobalFontSettings.lua; consumed by the global font registry.
local addonName, MSUF = ...
MSUF = MSUF or _G.MSUF_NS or {}

local M = MSUF.MSUF2 or _G.MSUF2 or {}
MSUF.MSUF2 = M

local A = MSUF.Assistant or {}
MSUF.Assistant = A
M.Assistant = A

A.GlobalRegistry = A.GlobalRegistry or {}

local Unpack = unpack or (table and table.unpack)

function A.GlobalRegistry.RegisterScopedFontDetailSettings(ctx)
    if type(ctx) ~= "table" then return false end

    local GeneralDB = ctx.GeneralDB
    local ApplyFonts = ctx.ApplyFonts
    local GlobalScopeIsGroup = ctx.GlobalScopeIsGroup
    local GlobalScopeRead = ctx.GlobalScopeRead
    local GlobalScopeWrite = ctx.GlobalScopeWrite
    local RegisterScopedSetting = ctx.RegisterScopedSetting
    local FontContext = ctx.FontContext or {}
    local FontData = FontContext.FontData or {}

    local SCOPED_FONT_CONTROL_SCOPES = FontData.SCOPED_FONT_CONTROL_SCOPES
    local FONT_OUTLINE_VALUES = FontData.FONT_OUTLINE_VALUES
    local FONT_OUTLINE_ALIASES = FontData.FONT_OUTLINE_ALIASES
    local FONT_RENDERING_VALUES = FontData.FONT_RENDERING_VALUES
    local FONT_RENDERING_ALIASES = FontData.FONT_RENDERING_ALIASES
    local FONT_SHADOW_STRENGTH_VALUES = FontData.FONT_SHADOW_STRENGTH_VALUES
    local FONT_SHADOW_STRENGTH_ALIASES = FontData.FONT_SHADOW_STRENGTH_ALIASES
    local CLASS_DEFAULT_VALUES = FontData.CLASS_DEFAULT_VALUES
    local CLASS_DEFAULT_ALIASES = FontData.CLASS_DEFAULT_ALIASES
    local SharedOrScopedAliases = FontContext.SharedOrScopedAliases
    local NormalizeFontTextAlpha = FontContext.NormalizeFontTextAlpha
    local ScopedFontOutline = FontContext.ScopedFontOutline
    local SetScopedFontOutline = FontContext.SetScopedFontOutline
    local ScopedFontNameColor = FontContext.ScopedFontNameColor
    local SetScopedFontNameColor = FontContext.SetScopedFontNameColor

    if type(GeneralDB) ~= "function" or type(RegisterScopedSetting) ~= "function" then return false end
    if type(GlobalScopeIsGroup) ~= "function" or type(GlobalScopeRead) ~= "function" then return false end
    if type(GlobalScopeWrite) ~= "function" then return false end
    if type(SCOPED_FONT_CONTROL_SCOPES) ~= "table" or type(FONT_OUTLINE_VALUES) ~= "table" then return false end
    if type(FONT_OUTLINE_ALIASES) ~= "table" or type(FONT_RENDERING_VALUES) ~= "table" then return false end
    if type(FONT_RENDERING_ALIASES) ~= "table" or type(FONT_SHADOW_STRENGTH_VALUES) ~= "table" then return false end
    if type(FONT_SHADOW_STRENGTH_ALIASES) ~= "table" or type(CLASS_DEFAULT_VALUES) ~= "table" then return false end
    if type(CLASS_DEFAULT_ALIASES) ~= "table" then return false end
    if type(SharedOrScopedAliases) ~= "function" or type(NormalizeFontTextAlpha) ~= "function" then return false end
    if type(ScopedFontOutline) ~= "function" or type(SetScopedFontOutline) ~= "function" then return false end
    if type(ScopedFontNameColor) ~= "function" or type(SetScopedFontNameColor) ~= "function" then return false end
    local RegisterScopedUnitFontTextSettings = A.GlobalRegistry and A.GlobalRegistry.RegisterScopedUnitFontTextSettings
    if type(RegisterScopedUnitFontTextSettings) ~= "function" then return false end

    for _, scope in ipairs(SCOPED_FONT_CONTROL_SCOPES) do
        local isShared = scope == "shared"
        local outlineAliases = {
            "font outline", "text outline", "outline style",
        }
        if isShared then
            outlineAliases[#outlineAliases + 1] = "global font outline"
            outlineAliases[#outlineAliases + 1] = "global text outline"
            outlineAliases[#outlineAliases + 1] = "shared font outline"
            outlineAliases[#outlineAliases + 1] = "shared text outline"
            outlineAliases[#outlineAliases + 1] = "default font outline"
            outlineAliases[#outlineAliases + 1] = "default text outline"
        end
        RegisterScopedSetting("fontScope", scope, "outline", "outline", "Font Outline", "enum", "OUTLINE", SharedOrScopedAliases(scope, {
            Unpack(outlineAliases),
        }), {
            flag = "fontOverride",
            values = FONT_OUTLINE_VALUES,
            valueAliases = FONT_OUTLINE_ALIASES,
            get = ScopedFontOutline,
            set = SetScopedFontOutline,
            apply = ApplyFonts,
            reason = "MSUF_ASSISTANT_FONT_OUTLINE",
        })
        local renderingAliases = {
            "font rendering", "text rendering", "font smoothing", "text smoothing", "sharp text", "pixel font", "monochrome font",
        }
        if isShared then
            renderingAliases[#renderingAliases + 1] = "global font rendering"
            renderingAliases[#renderingAliases + 1] = "global text rendering"
            renderingAliases[#renderingAliases + 1] = "global font smoothing"
            renderingAliases[#renderingAliases + 1] = "global monochrome font"
            renderingAliases[#renderingAliases + 1] = "global font monochrome"
            renderingAliases[#renderingAliases + 1] = "shared font rendering"
            renderingAliases[#renderingAliases + 1] = "shared text rendering"
            renderingAliases[#renderingAliases + 1] = "shared font smoothing"
            renderingAliases[#renderingAliases + 1] = "shared monochrome font"
            renderingAliases[#renderingAliases + 1] = "shared font monochrome"
            renderingAliases[#renderingAliases + 1] = "default font rendering"
            renderingAliases[#renderingAliases + 1] = "default font monochrome"
            renderingAliases[#renderingAliases + 1] = "font outline monochrome"
            renderingAliases[#renderingAliases + 1] = "global font outline monochrome"
            renderingAliases[#renderingAliases + 1] = "shared font outline monochrome"
        end
        RegisterScopedSetting("fontScope", scope, "fontMonochrome", "rendering", "Rendering", "enum", "SMOOTH", SharedOrScopedAliases(scope, {
            Unpack(renderingAliases),
        }), {
            flag = "fontOverride",
            values = FONT_RENDERING_VALUES,
            valueAliases = FONT_RENDERING_ALIASES,
            get = function(scopeKey) return GlobalScopeRead(scopeKey, "fontOverride", GeneralDB(), "fontMonochrome", false) and "SHARP" or "SMOOTH" end,
            set = function(scopeKey, value) GlobalScopeWrite(scopeKey, "fontOverride", GeneralDB(), "fontMonochrome", value == "SHARP") end,
            apply = ApplyFonts,
            reason = "MSUF_ASSISTANT_FONT_RENDERING",
        })
        local opacityAliases = {
            "text opacity", "font opacity", "text alpha", "font alpha",
        }
        if isShared then
            opacityAliases[#opacityAliases + 1] = "global text opacity"
            opacityAliases[#opacityAliases + 1] = "global font opacity"
            opacityAliases[#opacityAliases + 1] = "shared text opacity"
            opacityAliases[#opacityAliases + 1] = "shared font opacity"
        end
        RegisterScopedSetting("fontScope", scope, "fontTextAlpha", "textOpacity", "Text Opacity", "number", 1, SharedOrScopedAliases(scope, {
            Unpack(opacityAliases),
        }), {
            flag = "fontOverride",
            min = 0.7,
            max = 1,
            step = 0.05,
            percent = true,
            get = function(scopeKey) return NormalizeFontTextAlpha(GlobalScopeRead(scopeKey, "fontOverride", GeneralDB(), "fontTextAlpha", 1)) end,
            set = function(scopeKey, value) GlobalScopeWrite(scopeKey, "fontOverride", GeneralDB(), "fontTextAlpha", NormalizeFontTextAlpha(value)) end,
            apply = ApplyFonts,
            reason = "MSUF_ASSISTANT_FONT_TEXT_ALPHA",
        })
        local baselineAliases = {
            "text baseline", "font baseline", "baseline offset", "vertical font offset", "font y nudge", "text y nudge",
        }
        if isShared then
            baselineAliases[#baselineAliases + 1] = "global text baseline"
            baselineAliases[#baselineAliases + 1] = "global font baseline"
            baselineAliases[#baselineAliases + 1] = "global baseline offset"
            baselineAliases[#baselineAliases + 1] = "shared text baseline"
            baselineAliases[#baselineAliases + 1] = "shared font baseline"
            baselineAliases[#baselineAliases + 1] = "shared baseline offset"
        end
        RegisterScopedSetting("fontScope", scope, "fontBaselineOffset", "baseline", "Baseline", "number", 0, SharedOrScopedAliases(scope, {
            Unpack(baselineAliases),
        }), {
            flag = "fontOverride",
            min = -4,
            max = 4,
            step = 1,
            apply = ApplyFonts,
            reason = "MSUF_ASSISTANT_FONT_BASELINE",
        })
        local nameColorAliases = {
            "name color", "name text color", "player name color", "unit name color",
            "name text by class", "name text color by class", "color name by class",
            "color name text by class", "class color name text", "class colored name text",
        }
        if isShared then
            nameColorAliases[#nameColorAliases + 1] = "global name color"
            nameColorAliases[#nameColorAliases + 1] = "global name text color"
            nameColorAliases[#nameColorAliases + 1] = "shared name color"
            nameColorAliases[#nameColorAliases + 1] = "shared name text color"
        end
        RegisterScopedSetting("fontScope", scope, "nameColorMode", "nameColor", "Name Text Color Mode", "enum", "DEFAULT", SharedOrScopedAliases(scope, {
            Unpack(nameColorAliases),
        }), {
            flag = "fontOverride",
            values = CLASS_DEFAULT_VALUES,
            valueAliases = CLASS_DEFAULT_ALIASES,
            get = ScopedFontNameColor,
            set = SetScopedFontNameColor,
            apply = ApplyFonts,
            reason = "MSUF_ASSISTANT_NAME_COLOR_MODE",
        })
        local shadowAliases = {
            "text shadow", "font shadow", "shadow text", "shadow",
        }
        if isShared then
            shadowAliases[#shadowAliases + 1] = "global text shadow"
            shadowAliases[#shadowAliases + 1] = "global font shadow"
            shadowAliases[#shadowAliases + 1] = "shared text shadow"
            shadowAliases[#shadowAliases + 1] = "shared font shadow"
        end
        RegisterScopedSetting("fontScope", scope, "textBackdrop", "textShadow", "Text Shadow", "boolean", true, SharedOrScopedAliases(scope, {
            Unpack(shadowAliases),
        }), {
            flag = "fontOverride",
            apply = ApplyFonts,
            reason = "MSUF_ASSISTANT_FONT_SHADOW",
        })
        local shadowStrengthAliases = {
            "shadow strength", "text shadow strength", "font shadow strength", "shadow intensity",
        }
        if isShared then
            shadowStrengthAliases[#shadowStrengthAliases + 1] = "global shadow strength"
            shadowStrengthAliases[#shadowStrengthAliases + 1] = "global text shadow strength"
            shadowStrengthAliases[#shadowStrengthAliases + 1] = "global font shadow strength"
            shadowStrengthAliases[#shadowStrengthAliases + 1] = "shared shadow strength"
            shadowStrengthAliases[#shadowStrengthAliases + 1] = "shared text shadow strength"
            shadowStrengthAliases[#shadowStrengthAliases + 1] = "shared font shadow strength"
        end
        RegisterScopedSetting("fontScope", scope, "fontShadowStrength", "shadowStrength", "Shadow Strength", "enum", "NORMAL", SharedOrScopedAliases(scope, {
            Unpack(shadowStrengthAliases),
        }), {
            flag = "fontOverride",
            values = FONT_SHADOW_STRENGTH_VALUES,
            valueAliases = FONT_SHADOW_STRENGTH_ALIASES,
            apply = ApplyFonts,
            reason = "MSUF_ASSISTANT_FONT_SHADOW_STRENGTH",
        })
        if not GlobalScopeIsGroup(scope) then
            if RegisterScopedUnitFontTextSettings(ctx, scope) == false then return false end
        end
    end
    return true
end
