-- Début du fichier
local addonName = "RaidPlayerExport"
local addon = LibStub("AceAddon-3.0"):GetAddon(addonName)
if not addon then return end

local function createClassIcons(parent, title, yOffset, classes)
    local titleStr = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    titleStr:SetPoint("TOP", 0, yOffset)
    titleStr:SetText(title)
    table.insert(addon.statsWidgets, titleStr)
    
    if #classes == 0 then return end

    local iconSize, spacing, maxIconsPerRow = 20, 4, 9
    local startY = yOffset - 20
    
    -- Regrouper les classes par rangée
    local rows = {}
    for i = 1, #classes, maxIconsPerRow do
        local row = {}
        for j = i, math.min(i + maxIconsPerRow - 1, #classes) do
            table.insert(row, classes[j])
        end
        table.insert(rows, row)
    end
    
    -- Créer et positionner les icônes pour chaque rangée
    for rowIndex, classRow in ipairs(rows) do
        local numIconsInRow = #classRow
        
        -- Calculer la largeur totale de cette rangée spécifique
        local rowWidth = (numIconsInRow * iconSize) + (math.max(0, numIconsInRow - 1) * spacing)
        
        for colIndex, classFile in ipairs(classRow) do
            -- Calculer la position X pour chaque icône afin de centrer la rangée
            local x = (-rowWidth / 2) + ((colIndex - 1) * (iconSize + spacing)) + (iconSize / 2)
            local y = startY - (rowIndex - 1) * (iconSize + spacing)
            
            local icon = parent:CreateTexture(nil, "ARTWORK")
            icon:SetSize(iconSize, iconSize)
            icon:SetPoint("TOP", x, y)
            icon:SetTexture("Interface/AddOns/"..addonName.."/Media/classes_icons/classicon_" .. strlower(classFile) .. ".tga")
            
            if title:find("Absents") then
                icon:SetVertexColor(0.5, 0.5, 0.5)
            end
            table.insert(addon.statsWidgets, icon)
        end
    end
end

function addon:ClearStatsWidgets()
    if not self.statsWidgets then return end
    for _, widget in ipairs(self.statsWidgets) do
        widget:Hide()
    end
    wipe(self.statsWidgets)
end

function addon:UpdateRaidList()
    self:ClearStatsWidgets()

    if not IsInRaid() then
        self.frames.playerList.officialText = "Vous n'êtes pas dans un groupe de raid."
        self.frames.playerList:SetText(self.frames.playerList.officialText)
        addon.frames.main.title:SetText("Liste des joueurs (0)")
        return
    end

    local raidList, presentClasses = {}, {}
    for i = 1, GetNumGroupMembers() do
        local name, _, _, _, _, classFileName = GetRaidRosterInfo(i)
        if name and classFileName then
            name = name:gsub("-.*", "")

            local classColor = C_ClassColor.GetClassColor(classFileName)
            local colorHex = string.format("|cff%02x%02x%02x", classColor.r * 255, classColor.g * 255, classColor.b * 255)
            table.insert(raidList, string.format("%s%s|r", colorHex, name))
            presentClasses[classFileName] = true
        end
    end

    self.frames.playerList.officialText = table.concat(raidList, "\n")
    self.frames.playerList:SetText(self.frames.playerList.officialText)
    addon.frames.main.title:SetText("Liste des joueurs (" .. #raidList .. ")")

    local allClassFiles = {}
    for classFile in pairs(RAID_CLASS_COLORS) do
        table.insert(allClassFiles, classFile)
    end
    table.sort(allClassFiles)

    local absentClasses, presentClassesSorted = {}, {}
    for _, classFile in ipairs(allClassFiles) do
        if presentClasses[classFile] then
            table.insert(presentClassesSorted, classFile)
        else
            table.insert(absentClasses, classFile)
        end
    end
    
    createClassIcons(self.frames.stats, "|cffffd100Absents|r", -5, absentClasses)
    createClassIcons(self.frames.stats, "|cffffd100Présents|r", -65, presentClassesSorted)
end
-- Fin du fichier