local addonName = ...
local AutoJoinChat = CreateFrame("Frame")

-- Colores de las clases
local CLASS_COLORS = {
    WARRIOR = "|cFFC79C6E",
    PALADIN = "|cFFF58CBA",
    HUNTER = "|cFFABD473",
    ROGUE = "|cFFFFF569",
    PRIEST = "|cFFFFFFFF",
    DEATHKNIGHT = "|cFFC41F3B",
    SHAMAN = "|cFF0070DE",
    MAGE = "|cFF69CCF0",
    WARLOCK = "|cFF9482C9",
    MONK = "|cFF00FF96",
    DRUID = "|cFFFF7D0A"
}

-- Iconos de facción
local FACTION_ICONS = {
    Alliance = "|TInterface\\FriendsFrame\\PlusManz-Alliance:16|t",
    Horde = "|TInterface\\FriendsFrame\\PlusManz-Horde:16|t"
}

-- Iconos de raza
local RACE_ICONS = {
    Human = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:64:128:64:128|t",
    Dwarf = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:128:192:64:128|t",
    NightElf = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:0:64:0:64|t",
    Gnome = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:64:128:128:192|t",
    Draenei = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:192:256:64:128|t",
    Worgen = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:0:64:192:256|t",
    Orc = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:0:64:64:128|t",
    Undead = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:64:128:0:64|t",
    Tauren = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:128:192:0:64|t",
    Troll = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:192:256:0:64|t",
    BloodElf = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:192:256:64:128|t",
    Goblin = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:64:128:192:256|t",
    Pandaren = "|TInterface\\Glues\\CharacterCreate\\UI-CharacterCreate-Races:16:16:0:0:128:128:128:192:192:256|t"
}

-- Registramos el evento PLAYER_LOGIN para unirnos al canal al inicio
AutoJoinChat:RegisterEvent("PLAYER_LOGIN")
AutoJoinChat:SetScript("OnEvent", function(self, event)
    if event == "PLAYER_LOGIN" then
        local channelName = "world"  -- Este es el canal al que te unirás
        JoinChannelByName(channelName)
        ChatFrame_AddChannel(DEFAULT_CHAT_FRAME, channelName)
    end
end)

-- Personalizamos los mensajes que ves en tu pantalla
local function CustomizeChat(self, event, message, sender, _, _, _, _, _, channelName)
    -- Verifica si el mensaje proviene de ti mismo (tu personaje)
    if sender == UnitName("player") then
        -- Obtener la clase, raza y facción de tu personaje
        local class = select(2, UnitClass(sender)) or "WARRIOR"
        local race = select(2, UnitRace(sender)) or "Human"
        local faction = UnitFactionGroup(sender) or "Alliance"
        
        -- Obtener el color de la clase, el icono de la raza y el icono de la facción
        local classColor = CLASS_COLORS[class] or "|cFFFFFFFF"
        local factionIcon = FACTION_ICONS[faction] or ""
        local raceIcon = RACE_ICONS[race] or ""
        
        -- Formatear el nombre con los iconos y colores
        local formattedName = string.format("%s%s %s%s|r", factionIcon, raceIcon, classColor, sender)
        
        -- Devolver el mensaje con el nombre formateado solo para ti
        return false, message, formattedName
    end
    
    -- Si no es tu mensaje, no hacemos ningún cambio
    return
end

-- Agregar un filtro de evento para los mensajes del canal
ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", CustomizeChat)
