local frame = ModernEscapeMenuFrame

-- Crear el menú desplegable de configuración
local ConfigurationDropDown = CreateFrame("Frame", "ConfigurationDropDown", UIParent, "UIDropDownMenuTemplate")

local function ShowVideoOptions()
    ShowUIPanel(VideoOptionsFrame)
    frame:Hide()
end

local function ShowAudioOptions()
    ShowUIPanel(AudioOptionsFrame)
    frame:Hide()
end

local function ShowOtherOptions()
    ToggleOptionsFrame()
    frame:Hide()
end

local function ShowAddOns()
    if ACP_AddonList then
        ShowUIPanel(ACP_AddonList)
    else
        InterfaceOptionsFrame_OpenToCategory("AddOns")
    end
    frame:Hide()
end

local function ShowKeyBindings()
    KeyBindingFrame_LoadUI()
    ShowUIPanel(KeyBindingFrame)
    frame:Hide()
end

local function ConfigurationDropDown_Initialize(self, level)
    local info = UIDropDownMenu_CreateInfo()
    
    if level == 1 then
        info.text = "Gráficos"
        info.notCheckable = true
        info.func = ShowVideoOptions
        UIDropDownMenu_AddButton(info, level)
        
        info.text = "Audio"
        info.notCheckable = true
        info.func = ShowAudioOptions
        UIDropDownMenu_AddButton(info, level)
        
        info.text = "Otras opciones"
        info.notCheckable = true
        info.func = ShowOtherOptions
        UIDropDownMenu_AddButton(info, level)
    end
end

UIDropDownMenu_Initialize(ConfigurationDropDown, ConfigurationDropDown_Initialize)

local function PlaySoundOnToggle()
    PlaySound(SOUNDKIT.IG_MAINMENU_OPEN)
end

local function PlaySoundOnClose()
    PlaySound(SOUNDKIT.IG_MAINMENU_CLOSE)
end

local function CloseSpecificWindows()
    if InterfaceOptionsFrame:IsShown() then
        InterfaceOptionsFrame:Hide()
    end
    if VideoOptionsFrame:IsShown() then
        VideoOptionsFrame:Hide()
    end
    if AudioOptionsFrame:IsShown() then
        AudioOptionsFrame:Hide()
    end
    if KeyBindingFrame and KeyBindingFrame:IsShown() then
        HideUIPanel(KeyBindingFrame)
    end
    if MacroFrame and MacroFrame:IsShown() then
        HideUIPanel(MacroFrame)
    end
    if StoreFrame and StoreFrame:IsShown() then
        StoreFrame:Hide()
    end
    if AddonList and AddonList:IsShown() then
        HideUIPanel(AddonList)
    end
    if HelpFrame and HelpFrame:IsShown() then
        HideUIPanel(HelpFrame)
    end
    if PCodeNash and PCodeNash:IsShown() then
        HideUIPanel(PCodeNash)
    end
    if CharacterFrame and CharacterFrame:IsShown() then
        HideUIPanel(CharacterFrame)
    end
    if SpellBookFrame and SpellBookFrame:IsShown() then
        HideUIPanel(SpellBookFrame)
    end
    if TalentFrame and TalentFrame:IsShown() then
        HideUIPanel(TalentFrame)
    end
    if AchievementFrame and AchievementFrame:IsShown() then
        HideUIPanel(AchievementFrame)
    end
    if QuestLogFrame and QuestLogFrame:IsShown() then
        HideUIPanel(QuestLogFrame)
    end
    if FriendsFrame and FriendsFrame:IsShown() then
        HideUIPanel(FriendsFrame)
    end
    if LookingForGroupFrame and LookingForGroupFrame:IsShown() then
        HideUIPanel(LookingForGroupFrame)
    end
    if CollectionsJournal and CollectionsJournal:IsShown() then
        HideUIPanel(CollectionsJournal)
    end
    if EncounterJournal and EncounterJournal:IsShown() then
        HideUIPanel(EncounterJournal)
    end
    if GossipFrame and GossipFrame:IsShown() then
        HideUIPanel(GossipFrame)
    end
    if MerchantFrame and MerchantFrame:IsShown() then
        HideUIPanel(MerchantFrame)
    end
    if ShopMainFrame and ShopMainFrame:IsShown() then
        HideUIPanel(ShopMainFrame)
    end
    if GuildRegistrarFrame and GuildRegistrarFrame:IsShown() then
        HideUIPanel(GuildRegistrarFrame)
    end
	if ACP_AddonList and ACP_AddonList:IsShown() then
    HideUIPanel(ACP_AddonList)
    end	
	if originalToggleGameMenu and originalToggleGameMenu:IsShown() then
    HideUIPanel(originalToggleGameMenu)
    end	
end

local function ReplaceEscapeMenu()
    -- Guardar la función original
    local originalToggleGameMenu = ToggleGameMenu
    
    -- Reemplazar ToggleGameMenu
    _G.ToggleGameMenu = function()
        CloseSpecificWindows()
        if frame:IsShown() then
            frame:Hide()
            PlaySoundOnClose()
        else
            frame:Show()
            PlaySoundOnToggle()
        end
    end
end

ReplaceEscapeMenu()

-- Sobrescribir la función del botón "?"
MainMenuMicroButton:SetScript("OnClick", function()
    if ModernEscapeMenuFrame:IsShown() then
        ModernEscapeMenuFrame:Hide()
    else
        ModernEscapeMenuFrame:Show()
    end
end)



-- Función para abrir el ticket de ayuda (si está disponible en tu versión de WoW)
local function ToggleHelpFrame()
    if HelpFrame_ShowReportIssue then
        HelpFrame_ShowReportIssue()
    else
        print("La función de ayuda no está disponible en esta versión.")
    end
end

-- Función para configurar los hooks de KeyBindingFrame
local function SetupKeyBindingFrameHooks()
    -- Verificar si KeyBindingFrame existe
    if not KeyBindingFrame then
        -- Intentar cargar el marco de atajos de teclado
        if LoadAddOn("Blizzard_BindingUI") then
            -- Si se carga correctamente, KeyBindingFrame debería existir ahora
            if not KeyBindingFrame then
                print("|cffff0000[ModernEscapeMenu] Error: No se pudo cargar el marco de atajos de teclado.")
                return
            end
        else
            print("|cffff0000[ModernEscapeMenu] Advertencia: La interfaz de atajos de teclado no está disponible en esta versión de WoW.")
            return
        end
    end

    -- Si llegamos aquí, KeyBindingFrame debería existir
    KeyBindingFrame:HookScript("OnHide", function()
        if ModernEscapeMenuFrame and not ModernEscapeMenuFrame:IsShown() then
            ToggleGameMenu()
        end
    end)
    
    -- Configurar botones de KeyBindingFrame si existen
    if KeyBindingFrameCancelButton then
        KeyBindingFrameCancelButton:SetScript("OnClick", function()
            HideUIPanel(KeyBindingFrame)
            ToggleGameMenu()
        end)
    end
    
    if KeyBindingFrameOkayButton then
        KeyBindingFrameOkayButton:SetScript("OnClick", function()
            SaveBindings(GetCurrentBindingSet())
            HideUIPanel(KeyBindingFrame)
            ToggleGameMenu()
        end)
    end
end

-- Llamar a la función para configurar los hooks
C_Timer.After(1, SetupKeyBindingFrameHooks)
