-- ArenaCore Localization Module
-- Clean, isolated system for getting localized spell names and player names
-- Compatible with all WoW versions and locales (Chinese, Korean, etc.)

local AC = ArenaCore
if not AC then return end

local Localization = {}

-- Current locale detection
local currentLocale = GetLocale()
Localization.currentLocale = currentLocale

-- Supported locales for extended features
local supportedLocales = {
    ["enUS"] = true,
    ["zhCN"] = true,  -- Simplified Chinese
    ["zhTW"] = true,  -- Traditional Chinese
    ["koKR"] = true,  -- Korean
    ["deDE"] = true,  -- German
    ["frFR"] = true,  -- French
    ["ruRU"] = true,  -- Russian
    ["esES"] = true,  -- Spanish
    ["esMX"] = true,  -- Spanish (Mexico)
    ["ptBR"] = true,  -- Portuguese
    ["itIT"] = true,  -- Italian
}

-- Is this a non-English locale?
Localization.isNonEnglish = supportedLocales[currentLocale] and currentLocale ~= "enUS"

--- Get localized spell name with comprehensive fallback system
--- @param spellID number The spell ID to get name for
--- @param fallbackName string Optional fallback name if all APIs fail
--- @return string|nil Localized spell name or fallback
function Localization:GetSpellName(spellID, fallbackName)
    if not spellID then return fallbackName end
    
    -- Try new API first (12.0.0+): C_Spell.GetSpellName returns just the name string
    if C_Spell and C_Spell.GetSpellName then
        local name = C_Spell.GetSpellName(spellID)
        if name and name ~= "" then return name end
    end
    
    -- Try new API alternative: C_Spell.GetSpellInfo returns a table with .name field
    if C_Spell and C_Spell.GetSpellInfo then
        local spellInfo = C_Spell.GetSpellInfo(spellID)
        if spellInfo and spellInfo.name and spellInfo.name ~= "" then return spellInfo.name end
    end
    
    -- Fallback to legacy API (deprecated in 12.0.0 but may still work)
    if GetSpellInfo then
        local name = GetSpellInfo(spellID)
        if name and name ~= "" then return name end
    end
    
    -- Last resort: use provided fallback or "Unknown"
    return fallbackName or "Unknown"
end

--- Get localized player name with proper formatting
--- @param unit string Unit token (e.g., "arena1", "player")
--- @return string|nil Formatted player name
function Localization:GetPlayerName(unit)
    if not unit then return nil end
    
    local name = UnitFullName(unit)
    -- CRITICAL FIX: Handle secret string values in Midnight 12.0+
    -- Arena units return secret values that can't be compared with ""
    if issecretvalue(name) then
        -- Secret value - can't compare, return nil to trigger fallback
        return nil
    end
    if not name or name == "" then return nil end
    
    -- For non-English locales, we might want to preserve original formatting
    -- This can be extended later based on user preferences
    return name
end

--- Get localized spell name for cast bars with special handling
--- @param spellID number The spell ID
--- @param fallbackName string Fallback name
--- @return string Localized spell name
function Localization:GetCastBarSpellName(spellID, fallbackName)
    local name = self:GetSpellName(spellID, fallbackName)
    
    -- Special formatting for non-English locales can be added here
    -- For example, some languages might need different text handling
    
    return name
end

--- Check if client locale supports extended features
--- @return boolean True if locale supports extended features
function Localization:SupportsExtendedFeatures()
    return supportedLocales[currentLocale] or false
end

--- Get locale-specific configuration
--- @return table Locale-specific settings
function Localization:GetLocaleConfig()
    local config = {
        useLocalizedNames = self.isNonEnglish,
        showOriginalInTooltip = false,  -- Can be user-configurable later
        specialFormatting = false,
    }
    
    -- Locale-specific adjustments
    if currentLocale == "zhCN" or currentLocale == "zhTW" then
        -- Chinese-specific settings
        config.specialFormatting = true
        -- Add more Chinese-specific settings as needed
    elseif currentLocale == "koKR" then
        -- Korean-specific settings
        config.specialFormatting = true
    end
    
    return config
end

-- Debug function to test localization
function Localization:TestLocalization()
    print("|cff00FF00[ArenaCore Localization]|r Testing...")
    print("Current Locale:", currentLocale)
    print("Supports Extended:", self:SupportsExtendedFeatures())
    print("Is Non-English:", self.isNonEnglish)
    
    -- Test common spells
    local testSpells = {
        [118] = "Polymorph",        -- Mage CC
        [527] = "Purify",          -- Priest dispel
        [4987] = "Cleanse",        -- Paladin dispel
        [336126] = "PvP Trinket",  -- Trinket
    }
    
    for spellID, fallbackName in pairs(testSpells) do
        local localizedName = self:GetSpellName(spellID, fallbackName)
        print(string.format("Spell %d: %s -> %s", spellID, fallbackName, localizedName))
    end
end

-- Register the localization module
AC.Localization = Localization

-- Slash command for testing
SLASH_ARENACORELOCALIZATION1 = "/aclocale"
SlashCmdList.ARENACORELOCALIZATION = function()
    if AC and AC.Localization then
        AC.Localization:TestLocalization()
    else
        print("|cffFF0000[ArenaCore]|r Localization module not found")
    end
end

