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

addon.frames = {}
addon.FRAME_HEIGHT = 500
addon.FRAME_WIDTH_HUB = 220
addon.FRAME_WIDTH_PLAYERLIST = 260
addon.FRAME_WIDTH_LOOT = 400
addon.FRAME_WIDTH_FILTER = 160
addon.FRAME_SPACING = 10

function addon:CreateFrames()
    self:CreateHubFrame()
    self:CreatePlayerListFrame()
    self:CreateLootFrames()
end

-- Début de la fonction
function addon:CreateHubFrame()
    local hubFrame = CreateFrame("Frame", "RPEHubFrame", UIParent, "BackdropTemplate")
    hubFrame:SetSize(self.FRAME_WIDTH_HUB, self.FRAME_HEIGHT)
    hubFrame:SetPoint("CENTER")
    hubFrame:SetMovable(true); hubFrame:EnableMouse(true); hubFrame:SetClampedToScreen(true)
    hubFrame:RegisterForDrag("LeftButton")
    hubFrame:SetScript("OnDragStart", function(self) self:StartMoving() end)
    hubFrame:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)
    hubFrame:SetBackdrop({ bgFile = "Interface/Tooltips/UI-Tooltip-Background", edgeFile = "Interface/DialogFrame/UI-DialogBox-Border", tile = true, tileSize = 32, edgeSize = 16, insets = { left = 4, right = 4, top = 4, bottom = 4 } })
    hubFrame:SetBackdropColor(0.1, 0.1, 0.1, 0.95)
    hubFrame:Hide()
    self.frames.hub = hubFrame

    tinsert(UISpecialFrames, "RPEHubFrame")
    hubFrame:SetScript("OnHide", function()
        addon.frames.playerListFrame:Hide()
        addon.frames.loot:Hide()
        addon.frames.filter:Hide()
        -- On réinitialise la couleur des textes des boutons
        if addon.frames.hub.playerListButton then addon.frames.hub.playerListButton:GetFontString():SetTextColor(1, 1, 1) end
        if addon.frames.hub.lootHistoryButton then addon.frames.hub.lootHistoryButton:GetFontString():SetTextColor(1, 1, 1) end
    end)

    local closeButton = CreateFrame("Button", nil, hubFrame, "UIPanelCloseButton"); closeButton:SetPoint("TOPRIGHT", -6, -6); closeButton:SetScript("OnClick", function() addon:ToggleWindows() end)
    local optionsButtonTop = CreateFrame("Button", nil, hubFrame); optionsButtonTop:SetSize(24, 24); optionsButtonTop:SetPoint("TOPLEFT", 8, -8); optionsButtonTop:SetNormalTexture("Interface\\CHATFRAME\\UI-ChatIcon-Settings-Up"); optionsButtonTop:SetPushedTexture("Interface\\CHATFRAME\\UI-ChatIcon-Settings-Down"); optionsButtonTop:SetHighlightTexture("Interface\\BUTTONS\\UI-Common-MouseHilight", "ADD"); optionsButtonTop:SetScript("OnClick", function() LibStub("AceConfigDialog-3.0"):Open(addonName) end)
    hubFrame.title = hubFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightLarge"); hubFrame.title:SetPoint("TOP", 0, -18); hubFrame.title:SetText(addonName)
    
    -- On repasse à des "Button" normaux
    local playerListButton = CreateFrame("Button", nil, hubFrame, "UIPanelButtonTemplate"); playerListButton:SetSize(180, 30); playerListButton:SetPoint("CENTER", 0, 20); playerListButton:SetText("Liste des joueurs"); playerListButton:SetScript("OnClick", function() addon:TogglePlayerListFrame() end)
    local lootHistoryButton = CreateFrame("Button", nil, hubFrame, "UIPanelButtonTemplate"); lootHistoryButton:SetSize(180, 30); lootHistoryButton:SetPoint("TOP", playerListButton, "BOTTOM", 0, -10); lootHistoryButton:SetText("Historique des butins"); lootHistoryButton:SetScript("OnClick", function() addon:ToggleLootHistoryFrame() end)
    
    self.frames.hub.playerListButton = playerListButton
    self.frames.hub.lootHistoryButton = lootHistoryButton
    
    local credits = hubFrame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall"); credits:SetPoint("BOTTOM", 0, 35);
    credits:SetText("Addon créé par |cff0070DEEiganjos|r")
    
    local optionsButtonBottom = CreateFrame("Button", nil, hubFrame, "UIPanelButtonTemplate"); optionsButtonBottom:SetSize(80, 22); optionsButtonBottom:SetPoint("BOTTOM", 0, 8); optionsButtonBottom:SetText("Options"); optionsButtonBottom:SetScript("OnClick", function() LibStub("AceConfigDialog-3.0"):Open(addonName) end)
end
-- Fin de la fonction

function addon:CreatePlayerListFrame()
    local playerListFrame = CreateFrame("Frame", "RPEPlayerListFrame", UIParent, "BackdropTemplate"); playerListFrame:SetSize(self.FRAME_WIDTH_PLAYERLIST, self.FRAME_HEIGHT); playerListFrame:SetPoint("TOPRIGHT", self.frames.hub, "TOPLEFT", -self.FRAME_SPACING, 0); playerListFrame:SetBackdrop({ bgFile = "Interface/Tooltips/UI-Tooltip-Background", edgeFile = "Interface/DialogFrame/UI-DialogBox-Border", tile = true, tileSize = 32, edgeSize = 16, insets = { left = 4, right = 4, top = 4, bottom = 4 } }); playerListFrame:SetBackdropColor(0.1, 0.1, 0.1, 0.95); playerListFrame:Hide(); self.frames.playerListFrame = playerListFrame
    playerListFrame:EnableMouse(true); playerListFrame:RegisterForDrag("LeftButton"); playerListFrame:SetScript("OnDragStart", function() addon.frames.hub:StartMoving() end); playerListFrame:SetScript("OnDragStop", function() addon.frames.hub:StopMovingOrSizing() end)
    playerListFrame.title = playerListFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightLarge"); playerListFrame.title:SetPoint("TOP", 0, -18);
    playerListFrame.stats = CreateFrame("Frame", nil, playerListFrame); playerListFrame.stats:SetSize(self.FRAME_WIDTH_PLAYERLIST - 40, 120); playerListFrame.stats:SetPoint("TOP", playerListFrame.title, "BOTTOM", 0, -5); self.statsWidgets = {}
    local scrollFrame = CreateFrame("ScrollFrame", nil, playerListFrame, "UIPanelScrollFrameTemplate"); scrollFrame:SetPoint("TOP", playerListFrame.stats, "BOTTOM", -15, -10); scrollFrame:SetPoint("BOTTOMRIGHT", -15, 50); scrollFrame:SetPoint("BOTTOMLEFT", 15, 50)
    local editBox = CreateFrame("EditBox", nil, scrollFrame); editBox:SetMultiLine(true); editBox:SetPoint("TOPLEFT"); editBox:SetWidth(scrollFrame:GetWidth()); editBox:SetFontObject("NumberFontNormal"); editBox:SetAutoFocus(false); editBox:SetJustifyH("LEFT"); scrollFrame:SetScrollChild(editBox); self.frames.playerList = editBox
    editBox:SetScript("OnTextChanged", function(self) if self.officialText and self:GetText() ~= self.officialText then self:SetText(self.officialText); self:HighlightText(0, 0) end end)
    local copyButton = CreateFrame("Button", nil, playerListFrame, "UIPanelButtonTemplate"); copyButton:SetSize(120, 25); copyButton:SetPoint("BOTTOM", 0, 15); copyButton:SetText("Copier"); copyButton:SetScript("OnClick", function() editBox:SetFocus(true); editBox:HighlightText() end)
end

function addon:CreateLootFrames()
    local lootFrame = CreateFrame("Frame", "RPELootHistoryFrame", UIParent, "BackdropTemplate"); lootFrame:SetSize(self.FRAME_WIDTH_LOOT, self.FRAME_HEIGHT); lootFrame:SetPoint("TOPLEFT", self.frames.hub, "TOPRIGHT", self.FRAME_SPACING, 0); lootFrame:Hide(); lootFrame:SetBackdrop({ bgFile = "Interface/Tooltips/UI-Tooltip-Background", edgeFile = "Interface/DialogFrame/UI-DialogBox-Border", tile = true, tileSize = 32, edgeSize = 16, insets = { left = 4, right = 4, top = 4, bottom = 4 } }); lootFrame:SetBackdropColor(0.1, 0.1, 0.1, 0.95); self.frames.loot = lootFrame
    lootFrame:EnableMouse(true); lootFrame:RegisterForDrag("LeftButton"); lootFrame:SetScript("OnDragStart", function() addon.frames.hub:StartMoving() end); lootFrame:SetScript("OnDragStop", function() addon.frames.hub:StopMovingOrSizing() end)
    lootFrame.title = lootFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightLarge"); lootFrame.title:SetPoint("TOP", 0, -18); lootFrame.title:SetText("Historique des Butins")
    local lootScrollFrame = CreateFrame("ScrollFrame", "RPELootHistoryScrollFrame", lootFrame, "UIPanelScrollFrameTemplate"); lootScrollFrame:SetPoint("TOPLEFT", 15, -45); lootScrollFrame:SetPoint("BOTTOMRIGHT", -15, 50)
    local lootEditBox = CreateFrame("EditBox", nil, lootScrollFrame); lootEditBox:SetMultiLine(true); lootEditBox:SetPoint("TOPLEFT"); lootEditBox:SetWidth(lootScrollFrame:GetWidth()); lootEditBox:SetFontObject("NumberFontNormalSmall"); lootEditBox:SetAutoFocus(false); lootEditBox:SetJustifyH("LEFT"); lootScrollFrame:SetScrollChild(lootEditBox); self.frames.lootHistory = lootEditBox
    lootEditBox:SetScript("OnTextChanged", function(self) if self.officialText and self:GetText() ~= self.officialText then self:SetText(self.officialText); self:HighlightText(0, 0) end end)
    lootEditBox:SetHyperlinksEnabled(true)
    lootEditBox:SetScript("OnHyperlinkEnter", function(self, link) GameTooltip:SetOwner(addon.frames.hub, "ANCHOR_TOP"); GameTooltip:SetHyperlink(link); GameTooltip:Show() end)
    lootEditBox:SetScript("OnLeave", function() GameTooltip:Hide() end)
    local lootCopyButton = CreateFrame("Button", nil, lootFrame, "UIPanelButtonTemplate"); lootCopyButton:SetSize(120, 25); lootCopyButton:SetPoint("BOTTOM", 0, 15); lootCopyButton:SetText("Copier"); lootCopyButton:SetScript("OnClick", function() lootEditBox:SetFocus(true); lootEditBox:HighlightText() end)

    local filterFrame = CreateFrame("Frame", "RPEFilterFrame", UIParent, "BackdropTemplate"); filterFrame:SetSize(self.FRAME_WIDTH_FILTER, self.FRAME_HEIGHT); filterFrame:SetPoint("TOPLEFT", lootFrame, "TOPRIGHT", self.FRAME_SPACING, 0); filterFrame:Hide(); filterFrame:SetBackdrop({ bgFile = "Interface/Tooltips/UI-Tooltip-Background", edgeFile = "Interface/DialogFrame/UI-DialogBox-Border", tile = true, tileSize = 32, edgeSize = 16, insets = { left = 4, right = 4, top = 4, bottom = 4 } }); filterFrame:SetBackdropColor(0.1, 0.1, 0.1, 0.95); self.frames.filter = filterFrame
    filterFrame:EnableMouse(true); filterFrame:RegisterForDrag("LeftButton"); filterFrame:SetScript("OnDragStart", function() addon.frames.hub:StartMoving() end); filterFrame:SetScript("OnDragStop", function() addon.frames.hub:StopMovingOrSizing() end)
    local AceGUI = LibStub("AceGUI-3.0")
    local filterTopGroup = AceGUI:Create("SimpleGroup"); filterTopGroup:SetLayout("Flow"); filterTopGroup.frame:SetParent(filterFrame); filterTopGroup.frame:SetPoint("TOPLEFT", 15, -15); filterTopGroup.frame:SetPoint("TOPRIGHT", -15, 0); filterTopGroup:SetHeight(110)
    local buttonAll = AceGUI:Create("Button"); buttonAll:SetText("Toutes les dates"); buttonAll:SetFullWidth(true); buttonAll.text:SetFontObject("GameFontNormal"); buttonAll:SetCallback("OnClick", function() if addon.isUpdatingLootDisplay then return end; addon.lootFilter = "all"; addon:UpdateLootHistoryDisplay() end); filterTopGroup:AddChild(buttonAll)
    local spacer = AceGUI:Create("Label"); spacer:SetHeight(10); filterTopGroup:AddChild(spacer)
    local header = AceGUI:Create("Label"); header:SetText("Dates spécifiques"); header:SetFullWidth(true); header:SetFontObject("GameFontNormal"); header:SetColor(1, 0.82, 0); header:SetJustifyH("CENTER"); filterTopGroup:AddChild(header)
    self.frames.lootFilterButtons = { all = buttonAll }
    local dateListScroll = CreateFrame("ScrollFrame", "RPEDateListScrollFrame", filterFrame, "UIPanelScrollFrameTemplate"); dateListScroll:SetPoint("TOPLEFT", filterTopGroup.frame, "BOTTOMLEFT", -5, -5); dateListScroll:SetPoint("BOTTOMRIGHT", -15, 15)
    local dateListScrollChild = CreateFrame("Frame"); dateListScroll:SetScrollChild(dateListScrollChild); dateListScrollChild:SetWidth(dateListScroll:GetWidth())
    self.frames.dateListScroll = dateListScroll; self.frames.dateListScrollChild = dateListScrollChild; self.frames.dateButtons = {}
end