--## Author: Célèbre-EU(Hyjal) ## Version: 1.0.0
local _, ns = ...
local M, R, U, I = unpack(ns)
local MISC = M:GetModule("Misc")

function MISC:HideQuests()
    if not R.db["Misc"]["HideQuests"] then return end

local HideQuests = {}

-- Système de localisation
local L = {}

-- Détection de la langue du client
local locale = GetLocale()

-- Textes en français
if locale == "frFR" then
    L = {
        TOOLTIP_TITLE = "HideQuests",
        TOOLTIP_DESC = "Cliquez pour ouvrir la configuration",
        MENU_NOT_AVAILABLE = "Menu non disponible. L'addon est peut-être en cours de chargement."
    }
else
    -- Textes en anglais (par défaut)
    L = {
        TOOLTIP_TITLE = "HideQuests",
        TOOLTIP_DESC = "Click to open configuration",
        MENU_NOT_AVAILABLE = "Menu not available. The addon may be loading."
    }
end

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("ADDON_LOADED")
f:RegisterEvent("PLAYER_REGEN_ENABLED")  -- Fin du combat


local needsCollapse = false

-- Configuration par défaut (tous les trackers activés)
local defaultConfig = {
    quest = true,
    achievement = true,
    campaign = true,
    worldQuest = true,
    monthlyActivities = true
}

-- Initialisation de la configuration sauvegardée
if not HideQuestsConfig then
    HideQuestsConfig = {}
end

-- Appliquer les valeurs par défaut pour les nouvelles options
for key, value in pairs(defaultConfig) do
    if HideQuestsConfig[key] == nil then
        HideQuestsConfig[key] = value
    end
end

-- Fonction pour obtenir la configuration actuelle
function HideQuests:GetConfig()
    return HideQuestsConfig
end

-- Fonction pour mettre à jour la configuration
function HideQuests:UpdateConfig()
    -- Cette fonction est appelée quand la configuration change
    -- Pas besoin de faire quelque chose de spécial ici, la config est déjà mise à jour
end

-- Fonction pour replier un tracker spécifique
local function CollapseQuestTracker()
    if QuestObjectiveTracker and QuestObjectiveTracker.Header and QuestObjectiveTracker.Header.MinimizeButton then
        if QuestObjectiveTracker.ContentsFrame:IsShown() then
            QuestObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function CollapseAchievementTracker()
    if AchievementObjectiveTracker and AchievementObjectiveTracker.Header and AchievementObjectiveTracker.Header.MinimizeButton then
        if AchievementObjectiveTracker.ContentsFrame:IsShown() then
            AchievementObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function CollapseCampaignTracker()
    if CampaignQuestObjectiveTracker and CampaignQuestObjectiveTracker.Header and CampaignQuestObjectiveTracker.Header.MinimizeButton then
        if CampaignQuestObjectiveTracker.ContentsFrame:IsShown() then
            CampaignQuestObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function CollapseWorldQuestTracker()
    if WorldQuestObjectiveTracker and WorldQuestObjectiveTracker.Header and WorldQuestObjectiveTracker.Header.MinimizeButton then
        if WorldQuestObjectiveTracker.ContentsFrame:IsShown() then
            WorldQuestObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function CollapseMonthlyActivitiesTracker()
    if MonthlyActivitiesObjectiveTracker and MonthlyActivitiesObjectiveTracker.Header and MonthlyActivitiesObjectiveTracker.Header.MinimizeButton then
        if MonthlyActivitiesObjectiveTracker.ContentsFrame:IsShown() then
            MonthlyActivitiesObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

-- Fonction principale qui appelle les trackers selon la configuration
local function CollapseObjectives()
    if HideQuestsConfig.quest then
        CollapseQuestTracker()
    end
    if HideQuestsConfig.achievement then
        CollapseAchievementTracker()
    end
    if HideQuestsConfig.campaign then
        CollapseCampaignTracker()
    end
    if HideQuestsConfig.worldQuest then
        CollapseWorldQuestTracker()
    end
    if HideQuestsConfig.monthlyActivities then
        CollapseMonthlyActivitiesTracker()
    end
end

-- Créer l'icône de la minimap
local function CreateMinimapIcon()
    local icon = CreateFrame("Button", "HideQuestsMinimapIcon", Minimap)
    icon:SetSize(32, 32)
    icon:SetFrameStrata("MEDIUM")
    icon:SetFrameLevel(8)
    icon:RegisterForClicks("AnyUp")
    icon:SetToplevel(true)
    
    -- Position sur la minimap
    icon:SetPoint("TOPLEFT", Minimap, "TOPLEFT", -2, 2)
    
    -- Texture de l'icône (antenne de soie)
    local texture = icon:CreateTexture(nil, "BACKGROUND")
    texture:SetAllPoints()
    texture:SetTexture("Interface\\Icons\\inv_112_raidtrinkets_silkwormsantenna")
    
    -- Bordure
    --local border = icon:CreateTexture(nil, "ARTWORK")
    --border:SetAllPoints()
    --border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
    
    -- Clic pour ouvrir le menu
    icon:SetScript("OnClick", function(self, button)
        if button == "LeftButton" then
            if HideQuestsMenu and HideQuestsMenu.OpenMenu then
                HideQuestsMenu:OpenMenu()
            else
                print("HideQuests: " .. L.MENU_NOT_AVAILABLE)
            end
        end
    end)
    
    -- Animation au survol et tooltip
    icon:SetScript("OnEnter", function(self)
        self:SetScale(1.1)
        GameTooltip:SetOwner(self, "ANCHOR_LEFT")
        GameTooltip:SetText(L.TOOLTIP_TITLE, 1, 1, 1)
        GameTooltip:AddLine(L.TOOLTIP_DESC, 0.5, 0.5, 0.5)
        GameTooltip:Show()
    end)
    
    icon:SetScript("OnLeave", function(self)
        self:SetScale(1.0)
        GameTooltip:Hide()
    end)
    
    return icon
end

-- Exposer l'objet HideQuests globalement
HideQuests = HideQuests

f:SetScript("OnEvent", function(self, event, arg1)
    if event == "ADDON_LOADED" and arg1 == "OrzUI" then
        CreateMinimapIcon()
    end
    
    if event == "PLAYER_REGEN_ENABLED" then
        -- Le combat est fini, on exécute le repli si besoin
        if needsCollapse then
            needsCollapse = false
            CollapseObjectives()
        end
        return
    end

    if InCombatLockdown() then
        -- On est en combat, on retentera plus tard
        needsCollapse = true
        return
    end

    if event == "ADDON_LOADED" and arg1 == "Blizzard_ObjectiveTracker" then
        self:UnregisterEvent("ADDON_LOADED")
        CollapseObjectives()
    elseif event == "PLAYER_ENTERING_WORLD" then
        CollapseObjectives()
    end
end)

-- Commande slash pour ouvrir le menu (gardée pour compatibilité)
SLASH_HIDEQUESTS1 = "/hidequests"
SLASH_HIDEQUESTS2 = "/hq"
SlashCmdList["HIDEQUESTS"] = function(msg)
    if HideQuestsMenu and HideQuestsMenu.OpenMenu then
        HideQuestsMenu:OpenMenu()
    else
        print("HideQuests: " .. L.MENU_NOT_AVAILABLE)
    end
end

-- HideQuestsMenu.lua
-- Menu de configuration pour l'addon HideQuests

-- Créer l'objet global directement
HideQuestsMenu = {}

-- Système de localisation
local L = {}

-- Détection de la langue du client
local locale = GetLocale()

-- Textes en français
if locale == "frFR" then
    L = {
        TITLE = "HideQuests - Configuration",
        DESCRIPTION = "Choisissez quels trackers replier automatiquement :",
        QUEST_NORMAL = "Quêtes normales",
        ACHIEVEMENT = "Hauts faits",
        CAMPAIGN = "Quêtes de campagne",
        WORLD_QUEST = "Quêtes mondiales",
        MONTHLY_ACTIVITIES = "Journal de voyage",
        RESET = "Réinitialiser",
        CLOSE = "Fermer",
        TOOLTIP_TITLE = "HideQuests",
        TOOLTIP_DESC = "Cliquez pour ouvrir la configuration"
    }
else
    -- Textes en anglais (par défaut)
    L = {
        TITLE = "HideQuests - Configuration",
        DESCRIPTION = "Choose which trackers to automatically collapse:",
        QUEST_NORMAL = "Normal Quests",
        ACHIEVEMENT = "Achievements",
        CAMPAIGN = "Campaign Quests",
        WORLD_QUEST = "World Quests",
        MONTHLY_ACTIVITIES = "Traveler's Journal",
        RESET = "Reset",
        CLOSE = "Close",
        TOOLTIP_TITLE = "HideQuests",
        TOOLTIP_DESC = "Click to open configuration"
    }
end

-- Configuration par défaut (tous les trackers activés)
local defaultConfig = {
    quest = true,
    achievement = true,
    campaign = true,
    worldQuest = true,
    monthlyActivities = true
}

-- Initialisation de la configuration sauvegardée
if not HideQuestsConfig then
    HideQuestsConfig = {}
end

-- Appliquer les valeurs par défaut pour les nouvelles options
for key, value in pairs(defaultConfig) do
    if HideQuestsConfig[key] == nil then
        HideQuestsConfig[key] = value
    end
end

-- Fonctions pour replier/déplier les trackers
local function CollapseQuestTracker()
    if QuestObjectiveTracker and QuestObjectiveTracker.Header and QuestObjectiveTracker.Header.MinimizeButton then
        if QuestObjectiveTracker.ContentsFrame:IsShown() then
            QuestObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function ExpandQuestTracker()
    if QuestObjectiveTracker and QuestObjectiveTracker.Header and QuestObjectiveTracker.Header.MinimizeButton then
        if not QuestObjectiveTracker.ContentsFrame:IsShown() then
            QuestObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function CollapseAchievementTracker()
    if AchievementObjectiveTracker and AchievementObjectiveTracker.Header and AchievementObjectiveTracker.Header.MinimizeButton then
        if AchievementObjectiveTracker.ContentsFrame:IsShown() then
            AchievementObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function ExpandAchievementTracker()
    if AchievementObjectiveTracker and AchievementObjectiveTracker.Header and AchievementObjectiveTracker.Header.MinimizeButton then
        if not AchievementObjectiveTracker.ContentsFrame:IsShown() then
            AchievementObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function CollapseCampaignTracker()
    if CampaignQuestObjectiveTracker and CampaignQuestObjectiveTracker.Header and CampaignQuestObjectiveTracker.Header.MinimizeButton then
        if CampaignQuestObjectiveTracker.ContentsFrame:IsShown() then
            CampaignQuestObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function ExpandCampaignTracker()
    if CampaignQuestObjectiveTracker and CampaignQuestObjectiveTracker.Header and CampaignQuestObjectiveTracker.Header.MinimizeButton then
        if not CampaignQuestObjectiveTracker.ContentsFrame:IsShown() then
            CampaignQuestObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function CollapseWorldQuestTracker()
    if WorldQuestObjectiveTracker and WorldQuestObjectiveTracker.Header and WorldQuestObjectiveTracker.Header.MinimizeButton then
        if WorldQuestObjectiveTracker.ContentsFrame:IsShown() then
            WorldQuestObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function ExpandWorldQuestTracker()
    if WorldQuestObjectiveTracker and WorldQuestObjectiveTracker.Header and WorldQuestObjectiveTracker.Header.MinimizeButton then
        if not WorldQuestObjectiveTracker.ContentsFrame:IsShown() then
            WorldQuestObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function CollapseMonthlyActivitiesTracker()
    if MonthlyActivitiesObjectiveTracker and MonthlyActivitiesObjectiveTracker.Header and MonthlyActivitiesObjectiveTracker.Header.MinimizeButton then
        if MonthlyActivitiesObjectiveTracker.ContentsFrame:IsShown() then
            MonthlyActivitiesObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

local function ExpandMonthlyActivitiesTracker()
    if MonthlyActivitiesObjectiveTracker and MonthlyActivitiesObjectiveTracker.Header and MonthlyActivitiesObjectiveTracker.Header.MinimizeButton then
        if not MonthlyActivitiesObjectiveTracker.ContentsFrame:IsShown() then
            MonthlyActivitiesObjectiveTracker.Header.MinimizeButton:Click()
        end
    end
end

-- Fonction pour appliquer immédiatement les changements de configuration
local function ApplyTrackerChanges()
    if HideQuestsConfig.quest then
        CollapseQuestTracker()
    else
        ExpandQuestTracker()
    end
    
    if HideQuestsConfig.achievement then
        CollapseAchievementTracker()
    else
        ExpandAchievementTracker()
    end
    
    if HideQuestsConfig.campaign then
        CollapseCampaignTracker()
    else
        ExpandCampaignTracker()
    end
    
    if HideQuestsConfig.worldQuest then
        CollapseWorldQuestTracker()
    else
        ExpandWorldQuestTracker()
    end
    
    if HideQuestsConfig.monthlyActivities then
        CollapseMonthlyActivitiesTracker()
    else
        ExpandMonthlyActivitiesTracker()
    end
end

-- Fonction pour créer le menu
function HideQuestsMenu:CreateMenu()
    -- Créer la fenêtre principale
    local frame = CreateFrame("Frame", "HideQuestsMenuFrame", UIParent, "BasicFrameTemplateWithInset")
    frame:SetSize(400, 300)
    frame:SetPoint("CENTER")
    frame:SetMovable(true)
    frame:EnableMouse(true)
    frame:RegisterForDrag("LeftButton")
    frame:SetScript("OnDragStart", frame.StartMoving)
    frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
    frame:Hide()
    
    -- Titre
    local title = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightLarge")
    title:SetPoint("TOP", 0, -30)
    title:SetText(L.TITLE)
    
    -- Description
    local desc = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    desc:SetPoint("TOP", title, "BOTTOM", 0, -10)
    desc:SetText(L.DESCRIPTION)
    desc:SetJustifyH("CENTER")
    
    -- Options de configuration
    local options = {
        {key = "campaign", text = L.CAMPAIGN, y = -70},
        {key = "quest", text = L.QUEST_NORMAL, y = -100},
        {key = "achievement", text = L.ACHIEVEMENT, y = -130},
        {key = "monthlyActivities", text = L.MONTHLY_ACTIVITIES, y = -160}, -- worldQuest
        {key = "worldQuest", text = L.WORLD_QUEST, y = -190} -- monthlyActivities
    }
    
    local checkboxes = {}
    
    for i, option in ipairs(options) do
        -- Checkbox
        local checkbox = CreateFrame("CheckButton", nil, frame, "UICheckButtonTemplate")
        checkbox:SetPoint("TOPLEFT", 30, option.y)
        checkbox:SetChecked(HideQuestsConfig[option.key])
        checkbox.optionKey = option.key
        
        -- Label
        local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        label:SetPoint("LEFT", checkbox, "RIGHT", 10, 0)
        label:SetText(option.text)
        
        -- Script pour sauvegarder les changements et appliquer immédiatement
        checkbox:SetScript("OnClick", function(self)
            HideQuestsConfig[self.optionKey] = self:GetChecked()
            
            -- Appliquer immédiatement le changement
            if self.optionKey == "quest" then
                if HideQuestsConfig.quest then
                    CollapseQuestTracker()
                else
                    ExpandQuestTracker()
                end
            elseif self.optionKey == "achievement" then
                if HideQuestsConfig.achievement then
                    CollapseAchievementTracker()
                else
                    ExpandAchievementTracker()
                end
            elseif self.optionKey == "campaign" then
                if HideQuestsConfig.campaign then
                    CollapseCampaignTracker()
                else
                    ExpandCampaignTracker()
                end
            elseif self.optionKey == "worldQuest" then
                if HideQuestsConfig.worldQuest then
                    CollapseWorldQuestTracker()
                else
                    ExpandWorldQuestTracker()
                end
            elseif self.optionKey == "monthlyActivities" then
                if HideQuestsConfig.monthlyActivities then
                    CollapseMonthlyActivitiesTracker()
                else
                    ExpandMonthlyActivitiesTracker()
                end
            end
            
            -- Notifier le code principal du changement
            if HideQuests and HideQuests.UpdateConfig then
                HideQuests.UpdateConfig()
            end
        end)
        
        checkboxes[option.key] = checkbox
    end
    
    -- Le template BasicFrameTemplateWithInset inclut déjà un bouton de fermeture
    
    -- Bouton Réinitialiser
    local resetButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
    resetButton:SetSize(120, 30)
    resetButton:SetPoint("BOTTOMLEFT", 20, 20)
    resetButton:SetText(L.RESET)
    resetButton:SetScript("OnClick", function()
        for key, value in pairs(defaultConfig) do
            HideQuestsConfig[key] = value
            if checkboxes[key] then
                checkboxes[key]:SetChecked(value)
            end
        end
        -- Appliquer immédiatement tous les changements
        ApplyTrackerChanges()
        if HideQuests and HideQuests.UpdateConfig then
            HideQuests.UpdateConfig()
        end
    end)
    
    -- Bouton Fermer (en bas)
    local closeButtonBottom = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
    closeButtonBottom:SetSize(120, 30)
    closeButtonBottom:SetPoint("BOTTOMRIGHT", -20, 20)
    closeButtonBottom:SetText(L.CLOSE)
    closeButtonBottom:SetScript("OnClick", function()
        frame:Hide()
    end)
    
    frame.checkboxes = checkboxes
    return frame
end

-- Fonction pour ouvrir le menu
function HideQuestsMenu:OpenMenu()
    if not self.menuFrame then
        self.menuFrame = self:CreateMenu()
    end
    self.menuFrame:Show()
end

-- Fonction pour obtenir la configuration actuelle
function HideQuestsMenu:GetConfig()
    return HideQuestsConfig
end

-- Fonction pour mettre à jour la configuration
function HideQuestsMenu:UpdateConfig(newConfig)
    for key, value in pairs(newConfig) do
        HideQuestsConfig[key] = value
    end
end

-- L'objet est déjà exposé globalement au début du fichier

end