-- Pedro Addon: Wow Version 12.0.1
-- Pedro Addon: Pedro Version 8.3
-- Author: Darthcutie

-- Ensure both DB tables exist
PedroDB_Global = PedroDB_Global or {}
PedroDB_Char   = PedroDB_Char   or {}

-- Local references bound to SavedVariables
local G = PedroDB_Global
local C = PedroDB_Char

-- Define PedroSounds
local PedroSounds = {
    {name = "Pedro",      path = "Interface\\AddOns\\Pedro\\Media\\Pedro.ogg",      duration = 9},
    {name = "PedroLong",  path = "Interface\\AddOns\\Pedro\\Media\\PedroLong.ogg",  duration = 40},
    {name = "GoldenKpop", path = "Interface\\AddOns\\Pedro\\Media\\GoldenKpop.ogg", duration = 48},
}

-- Song length lookup table 
local PedroSongLengths = {}

for _, sound in ipairs(PedroSounds) do
    if sound.path and sound.duration then
        PedroSongLengths[sound.path] = sound.duration
	end
end

local function GetSelectedSongLength()
    return PedroSongLengths[G.selectedSound] or 10
end


local animationTimer = 0
local frameDuration = G.animationSpeed or 0.15
local currentFrame = 1
local elapsedTime = 0
local isAnimating = false
local tickerInCombat = nil
local PedroPositioningMode = false
local isFadingOut = false


function StartAnimation(songLength)
    if not songLength or songLength <= 0 then
        songLength = 10
	end
	
    if isAnimating then
        return
	end
	
    isAnimating = true
    isFadingOut = false
    PedroAnimationFrame:SetAlpha(1)
	
    if PedroAnimationFrame.closeBtn then
        PedroAnimationFrame.closeBtn:Hide()
	end
	
    animationTimer = 0
    elapsedTime = 0
    currentFrame = 1
	
	local frameDuration = G.animationSpeed or 0.15
	
    -- Sprite sheet settings (4x8 inside 1024x2048 canvas)
    local cellSize = 193 -- actual frame size
    local iconSize = 193 -- on-screen size
    
	
    local numCols = 4
    local numRows = 8
	
    -- Full texture size
    local totalWidth  = 1024
    local totalHeight = 2048
	
    PedroAnimationFrame:SetSize(iconSize, iconSize)
	
    -- Animation Loop
    PedroAnimationFrame:SetScript("OnUpdate", function(self, elapsed)
        if elapsed > 0.05 then
            elapsed = 0.05
		end
		
        animationTimer = animationTimer + elapsed
        elapsedTime = elapsedTime + elapsed
		
        if not isFadingOut and elapsedTime >= frameDuration then
            elapsedTime = elapsedTime - frameDuration
            currentFrame = currentFrame + 1
			
            if currentFrame > (numCols * numRows) then
                currentFrame = 1
			end
			
            local col = (currentFrame - 1) % numCols
            local row = math.floor((currentFrame - 1) / numCols)
			
			-- Pixel coords inside the region where icons actually are														  
            local left_px   = col * cellSize
            local right_px  = left_px + cellSize
            local top_px    = row * cellSize
            local bottom_px = top_px + cellSize
			
			-- Convert to normalized coords relative to full texture size															 
            local left   = left_px / totalWidth
            local right  = right_px / totalWidth
            local top    = top_px / totalHeight
            local bottom = bottom_px / totalHeight
			
            PedroAnimationTexture:SetTexCoord(left, right, top, bottom)
		end
		
        if not isFadingOut and animationTimer >= songLength then
            isFadingOut = true
            self.fadeStart = GetTime()
		end
		
        if isFadingOut then
            local fadeDuration = 0.25
            local progress = (GetTime() - self.fadeStart) / fadeDuration
			
            if progress >= 1 then
                -- Fade complete
                PedroAnimationFrame:SetAlpha(0)
                PedroAnimationFrame:EnableMouse(false)
                if PedroAnimationFrame.closeBtn then
                    PedroAnimationFrame.closeBtn:EnableMouse(false)
				end
				
                isAnimating = false
                isFadingOut = false
                PedroAnimationFrame:SetScript("OnUpdate", nil)
                return
				else
                -- Fade in progress
                PedroAnimationFrame:SetAlpha(1 - progress)
			end
		end
	end)
end

-- Audio channels
local PedroAudioChannels = {
    {name = "Master (Default, Recommended)", value = "Master"},
    {name = "Sound Effects (Recommended if sound cuts out)", value = "SFX"},
    {name = "Dialog (Not recommended)", value = "Dialog"},
    {name = "Music (Not recommended)", value = "Music"},
    {name = "Ambience (Not recommended)", value = "Ambience"},
}

-- Core state variables
local lustActive = false
local canPlay = true
local lastTriggerTime = nil
local cooldown = 1.5
local lustGuardDuration = 48 -- seconds between allowed Pedro triggers

local pedroAudioChannel = G.audioChannel or "Master"
local pedroPlayingID = nil

local function ResetCooldown()
    canPlay = true
end

local function PlayPedroIfAllowed(ignoreGuard)
	if not ignoreGuard then
		if not canPlay then return false end
		if lastTriggerTime and (GetTime() - lastTriggerTime) < lustGuardDuration then return false end
	end
	
	if type(pedroPlayingID) == "number" then
		StopSound(pedroPlayingID)
		pedroPlayingID = nil
	end
	
	local id = PlaySoundFile(G.selectedSound, pedroAudioChannel)
	if not id then
		print("Pedro: Failed to play sound. File missing or invalid.")
		return false
	end
	pedroPlayingID = id
	
	if not ignoreGuard then
		canPlay = false
		lastTriggerTime = GetTime()
		C_Timer.After(cooldown, ResetCooldown)
	end
	
	return true
end

-- Debuffs to trigger Pedro (spell IDs)
local lustDebuffs = {
    [57723] = true,      -- Exhaustion
    [57724] = true,      -- Sated
    [80354] = true,      -- Temporal Displacement
    [95809] = true,      -- Hunter Pet Insanity
    [160455] = true,     -- Hunter Pet Fatigued
    [264689] = true,     -- Hunter Pet Fatigued (alt)
    [390435] = true,     -- Exhaustion (alt)
}
local function CheckLustDebuffByID()
    if EditModeManager and EditModeManager:ShouldIgnoreUnitEvents() then
        return false
	end
	
    local found = false
	
    for spellID in pairs(lustDebuffs) do
        local aura = C_UnitAuras.GetPlayerAuraBySpellID(spellID)
		
        if aura and aura.expirationTime then
            local timeLeft = aura.expirationTime - GetTime()
			
            -- Ignore nearly-expired lust debuffs 570 means if the debuff has less than 9:30 mins left, ignore it and don't trigger.
            if timeLeft >= 570 then
                found = true
                break
			end
		end
	end
	
    lustActive = found
    return found
end

-- Central trigger for Pedro based on debuffs
local function TryPlayPedro()
    local songLength = GetSelectedSongLength()
    local played = false
	
    local hasDebuff = CheckLustDebuffByID()
    lustActive = hasDebuff
	
    if hasDebuff then
        if not isAnimating then
            local playedSound = PlayPedroIfAllowed()
            if playedSound and not G.disableAnimation then
                StartAnimation(songLength)
			end
            played = playedSound
		end
	end
	
    return played
end

-- Manual play
local function TryPlayPedroManual()
    PlayPedroIfAllowed(true)
	
    -- Find the song in use
    local soundName = "Unknown"
    for _, sound in ipairs(PedroSounds) do
        if sound.path == G.selectedSound then
            soundName = sound.name
            break
		end
	end
	
    print("Pedro: Played " .. soundName .. " manually.")
	
    if not G.disableAnimation then
        local songLength = GetSelectedSongLength()
        StartAnimation(songLength)
	end
end


-- Show Sound Selection Frame
local function ShowSoundFrame()
	
    if PedroSoundSelector then
        PedroSoundSelector:Show()
        PedroSoundSelector:Raise()
        return
	end
	
    local SoundFrame = CreateFrame("Frame", "PedroSoundSelector", UIParent, "BasicFrameTemplateWithInset")
    local count = #PedroSounds
    SoundFrame:SetSize(200, 120 + (count * 25) + 35)
    SoundFrame:SetPoint("CENTER")
    SoundFrame:SetMovable(true)
    SoundFrame:EnableMouse(true)
    SoundFrame:RegisterForDrag("LeftButton")
    SoundFrame:SetScript("OnDragStart", SoundFrame.StartMoving)
    SoundFrame:SetScript("OnDragStop", SoundFrame.StopMovingOrSizing)
	
    SoundFrame.title = SoundFrame:CreateFontString(nil, "OVERLAY")
    SoundFrame.title:SetFontObject("GameFontHighlight")
    SoundFrame.title:SetPoint("TOP", 0, -5)
    SoundFrame.title:SetText("Select Pedro Sound")
	
    local okButton = CreateFrame("Button", nil, SoundFrame, "UIPanelButtonTemplate")
    okButton:SetSize(80, 22)
    okButton:SetPoint("BOTTOM", 0, 10)
    okButton:SetText("OK")
    okButton:SetScript("OnClick", function()
        SoundFrame:Hide()
	end)
	
    local radioButtons = {}
	
    for i, sound in ipairs(PedroSounds) do
        local btn = CreateFrame("CheckButton", "PedroSoundBtn"..i, SoundFrame, "UICheckButtonTemplate")
        btn:SetPoint("TOPLEFT", 10, -(i * 25) - 20)
        btn.text:SetText(string.format("%s - %d second%s", sound.name, sound.duration or 0, (sound.duration == 1 and "" or "s")))
		
        if G.selectedSound == sound.path then
            btn:SetChecked(true)
		end
		
        btn:SetScript("OnClick", function(self)
            for _, other in ipairs(radioButtons) do
                if other ~= self then
                    other:SetChecked(false)
				end
			end
            G.selectedSound = sound.path
            print("Pedro: Selected song " .. sound.name)
		end)
		
        table.insert(radioButtons, btn)
	end
	
    SoundFrame:Show()
end  

-- Show Audio Channel Selection Frame 
local function ShowAudioChannelFrame()
	
    if PedroAudioSelector then
        PedroAudioSelector:Show()
        PedroAudioSelector:Raise()
        return
	end
	
    local ChannelFrame = CreateFrame("Frame", "PedroAudioSelector", UIParent, "BasicFrameTemplateWithInset")
    local count = #PedroAudioChannels
    ChannelFrame:SetSize(320, 120 + (count * 25) + 35)
    ChannelFrame:SetPoint("CENTER")
    ChannelFrame:SetMovable(true)
    ChannelFrame:EnableMouse(true)
    ChannelFrame:RegisterForDrag("LeftButton")
    ChannelFrame:SetScript("OnDragStart", ChannelFrame.StartMoving)
    ChannelFrame:SetScript("OnDragStop", ChannelFrame.StopMovingOrSizing)
	
    ChannelFrame.title = ChannelFrame:CreateFontString(nil, "OVERLAY")
    ChannelFrame.title:SetFontObject("GameFontHighlight")
    ChannelFrame.title:SetPoint("TOP", 0, -5)
    ChannelFrame.title:SetText("Select Audio Channel")
	
    local okButton = CreateFrame("Button", nil, ChannelFrame, "UIPanelButtonTemplate")
    okButton:SetSize(80, 22)
    okButton:SetPoint("BOTTOM", 0, 10)
    okButton:SetText("OK")
    okButton:SetScript("OnClick", function()
        ChannelFrame:Hide()
	end)
	
    local radioButtons = {}
	
    for i, channel in ipairs(PedroAudioChannels) do
        local btn = CreateFrame("CheckButton", "PedroAudioBtn"..i, ChannelFrame, "UICheckButtonTemplate")
        btn:SetPoint("TOPLEFT", 10, -(i * 25) - 20)
        btn.text:SetText(channel.name)
		
        if G.audioChannel == channel.value then
            btn:SetChecked(true)
		end
		
        btn:SetScript("OnClick", function(self)
            for _, other in ipairs(radioButtons) do
                if other ~= self then
                    other:SetChecked(false)
				end
			end
            G.audioChannel = channel.value
            pedroAudioChannel = channel.value
            print("Pedro: Audio channel set to " .. channel.name)
		end)
		
        table.insert(radioButtons, btn)
	end
	
    ChannelFrame:Show()
end	

-- /Pedro debug command
SLASH_PEDRO1 = "/pedro"
SlashCmdList.PEDRO = function()
    local soundName = PedroSounds[1].name
    for _, sound in ipairs(PedroSounds) do
        if PedroDB_Global.selectedSound == sound.path then
            soundName = sound.name
            break
		end
	end
	
    local audioChannel = PedroDB_Global.audioChannel or "Master"
    local lustState = lustActive and "Yes" or "No"
	
    -- Debuff info
    local debuffName = "None"
    local timeLeft = 0
    local valid = false
	
    for spellID in pairs(lustDebuffs) do
        local aura = C_UnitAuras.GetPlayerAuraBySpellID(spellID)
        if aura and aura.expirationTime then
            debuffName = GetSpellInfo(spellID)
            timeLeft = aura.expirationTime - GetTime()
            valid = timeLeft >= 570
            break
		end
	end
	
    print("---- Pedro Info ----")
    print("Lust Detected:", lustState)
    print("Debuff:", debuffName)
    print("Time Left:", string.format("%.1f seconds", timeLeft))
    print("Debuff Valid (>=570s):", valid and "Yes" or "No")
    print("Sound:", soundName)
    print("Channel:", audioChannel)
    print("Animation Enabled:", not G.disableAnimation and "Yes" or "No")
	print("Animation Speed:", G.animationSpeed or "Default")
end

-- Main Pedro event frame
local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:RegisterEvent("UNIT_AURA")
f:RegisterEvent("PLAYER_REGEN_DISABLED")
f:RegisterEvent("PLAYER_REGEN_ENABLED")

f:SetScript("OnEvent", function(self, event, arg1)
	
    -- Addon loaded
    if event == "ADDON_LOADED" and arg1 == "Pedro" then
		
        -- Ensure SavedVariables exist
        PedroDB_Global = PedroDB_Global or {}
        PedroDB_Char   = PedroDB_Char   or {}
		
        -- Rebind locals
        G = PedroDB_Global
        C = PedroDB_Char
		
        -- Global defaults
        if G.selectedSound == nil then
            G.selectedSound = PedroSounds[1].path
		end
        if G.audioChannel == nil then
            G.audioChannel = "Master"
		end
        if G.disableAnimation == nil then
            G.disableAnimation = false
		end
		if G.animationSpeed == nil then
			G.animationSpeed = 0.15
		end
		
        -- Mark initialized
        C._initialized = true
		
        -- Create animation frame if missing
        if not PedroAnimationFrame then
            PedroAnimationFrame = CreateFrame("Frame", "PedroAnimationFrame", UIParent)
            PedroAnimationFrame:SetClampedToScreen(true)
			
            PedroAnimationTexture = PedroAnimationFrame:CreateTexture(nil, "BACKGROUND")
            PedroAnimationTexture:SetAllPoints()
            PedroAnimationTexture:SetTexture("Interface\\AddOns\\Pedro\\Media\\Pedro_Animation.tga")
			
			-- Ensure close button is hidden on load
			if PedroAnimationFrame.closeBtn then
				PedroAnimationFrame.closeBtn:Hide()
				PedroAnimationFrame.closeBtn:EnableMouse(false)
			end
			
			-- Set initial TexCoord to the first frame so the full sheet isn't shown
			local cellSize = 193
			local totalWidth = 1024
			local totalHeight = 2048
			
			local left   = 0 / totalWidth
			local right  = cellSize / totalWidth
			local top    = 0 / totalHeight
			local bottom = cellSize / totalHeight
			
			PedroAnimationTexture:SetTexCoord(left, right, top, bottom)
		end
		
        -- Set size and position
        PedroAnimationFrame:SetSize(193, 193)
        if G.animPoint and G.animPoint.point then
            PedroAnimationFrame:SetPoint(
                G.animPoint.point,
                UIParent,
                G.animPoint.relativePoint,
                G.animPoint.x,
                G.animPoint.y
			)
			else
            PedroAnimationFrame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 100, -120)
		end
		
        -- Make movable
        PedroAnimationFrame:SetMovable(true)
        PedroAnimationFrame:EnableMouse(true)
        PedroAnimationFrame:RegisterForDrag("LeftButton")
		
        PedroAnimationFrame:SetScript("OnDragStart", function(self)
            if InCombatLockdown() then return end
            self:StartMoving()
		end)
		
        PedroAnimationFrame:SetScript("OnDragStop", function(self)
            self:StopMovingOrSizing()
            local point, relativeTo, relativePoint, xOfs, yOfs = self:GetPoint()
            G.animPoint = {
                point = point,
                relativePoint = relativePoint,
                x = xOfs,
                y = yOfs
			}
		end)
		
        PedroAnimationFrame:SetAlpha(0)
        PedroAnimationFrame:EnableMouse(false)
		
		-- Player auras changed
		elseif event == "UNIT_AURA" then
        if arg1 == "player" then
            if not InCombatLockdown() then
                TryPlayPedro()
			end
		end
		
		-- Entering combat
		elseif event == "PLAYER_REGEN_DISABLED" then
        if tickerInCombat then
            tickerInCombat:Cancel()
            tickerInCombat = nil
		end
		
        tickerInCombat = C_Timer.NewTicker(1.0, TryPlayPedro)
		
		-- Leaving combat
		elseif event == "PLAYER_REGEN_ENABLED" then
        if tickerInCombat then
            tickerInCombat:Cancel()
            tickerInCombat = nil
		end
		
        lustActive = false
	end
end)

-- Minimap icon setup
local LDB  = LibStub("LibDataBroker-1.1")
local LDBI = LibStub("LibDBIcon-1.0")

-- Create dropdown
if not PedroDropdown then
    PedroDropdown = CreateFrame("Frame", "PedroDropdown", UIParent, "UIDropDownMenuTemplate")
end

local PedroLDB = LDB:NewDataObject("Pedro", {
    type  = "launcher",
    icon  = "Interface\\AddOns\\Pedro\\Media\\Pedro.tga",
    label = "Pedro",
	
    OnClick = function(_, button)
        if button == "LeftButton" then
            SlashCmdList.PEDRO()
			
			elseif button == "RightButton" then
            local dropdown = PedroDropdown
			
            UIDropDownMenu_Initialize(dropdown, function(self, level, menuList)
                -- LEVEL 1
                if level == 1 then
                    local info
					
                    info = UIDropDownMenu_CreateInfo()
                    info.text = "Play Pedro Now"
                    info.func = TryPlayPedroManual
                    info.notCheckable = true
                    UIDropDownMenu_AddButton(info, level)
					
                    info = UIDropDownMenu_CreateInfo()
                    info.text = "Select Song"
                    info.func = ShowSoundFrame
                    info.notCheckable = true
                    UIDropDownMenu_AddButton(info, level)
					
                    info = UIDropDownMenu_CreateInfo()
                    info.text = "Select Audio Channel"
                    info.func = ShowAudioChannelFrame
                    info.notCheckable = true
                    UIDropDownMenu_AddButton(info, level)
					
                    info = UIDropDownMenu_CreateInfo()
                    info.text = "Reset Audio Channel"
                    info.func = function()
                        G.audioChannel = "Master"
                        pedroAudioChannel = "Master"
                        print("Pedro: Audio channel reset to Master")
					end
                    info.notCheckable = true
                    UIDropDownMenu_AddButton(info, level)
					
                    info = UIDropDownMenu_CreateInfo()
                    info.text = G.disableAnimation and "Enable Animation" or "Disable Animation"
                    info.func = function()
                        G.disableAnimation = not G.disableAnimation
                        print("Pedro: Animation " .. (G.disableAnimation and "disabled" or "enabled"))
					end
                    info.notCheckable = true
                    UIDropDownMenu_AddButton(info, level)
					
                    info = UIDropDownMenu_CreateInfo()
                    info.text = "Position Animation"
                    info.notCheckable = true
                    info.func = function()
                        if InCombatLockdown() then
                            print("Pedro: Leave combat to move the animation.")
                            return
						end
						
                        PedroPositioningMode = true
                        PedroAnimationFrame:SetAlpha(1)
                        PedroAnimationFrame:Raise()
                        PedroAnimationFrame:EnableMouse(true)
						
                        if PedroAnimationFrame.closeBtn then
                            PedroAnimationFrame.closeBtn:EnableMouse(true)
						end
						
                        print("Pedro: Drag the animation to position it. Click the X to finish positioning.")
						
                        if not PedroAnimationFrame.closeBtn then
                            local closeBtn = CreateFrame("Button", nil, PedroAnimationFrame, "UIPanelCloseButton")
                            closeBtn:SetPoint("TOPRIGHT", PedroAnimationFrame, "TOPRIGHT", 0, 0)
                            closeBtn:SetScript("OnClick", function()
                                local point, _, relativePoint, xOfs, yOfs = PedroAnimationFrame:GetPoint()
                                G.animPoint = { point = point, relativePoint = relativePoint, x = xOfs, y = yOfs }
								
                                PedroPositioningMode = false
                                PedroAnimationFrame:SetAlpha(0)
                                PedroAnimationFrame:EnableMouse(false)
								
                                if PedroAnimationFrame.closeBtn then
                                    PedroAnimationFrame.closeBtn:EnableMouse(false)
								end
							end)
                            PedroAnimationFrame.closeBtn = closeBtn
						end
						
                        PedroAnimationFrame:SetMovable(true)
                        PedroAnimationFrame:RegisterForDrag("LeftButton")
                        PedroAnimationFrame:SetScript("OnDragStart", function(self)
                            if InCombatLockdown() then return end
                            self:StartMoving()
						end)
                        PedroAnimationFrame:SetScript("OnDragStop", function(self)
                            self:StopMovingOrSizing()
                            local point, _, relativePoint, xOfs, yOfs = self:GetPoint()
                            G.animPoint = { point = point, relativePoint = relativePoint, x = xOfs, y = yOfs }
						end)
					end
                    UIDropDownMenu_AddButton(info, level)
                    info = UIDropDownMenu_CreateInfo()
                    info.text = "Animation Speed"
                    info.hasArrow = true
                    info.menuList = "ANIM_SPEED"
                    info.notCheckable = true
                    UIDropDownMenu_AddButton(info, level)
					
                    info = UIDropDownMenu_CreateInfo()
                    info.text = "Reset Animation Position"
                    info.notCheckable = true
                    info.func = function()
                        PedroAnimationFrame:ClearAllPoints()
                        PedroAnimationFrame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 100, -120)
                        G.animPoint = { point = "TOPLEFT", relativePoint = "TOPLEFT", x = 100, y = -120 }
                        print("Pedro: Animation position reset near top-left.")
					end
                    UIDropDownMenu_AddButton(info, level)
				end
				
                -- LEVEL 2: 
                if level == 2 and menuList == "ANIM_SPEED" then
                    local info
					
					info = UIDropDownMenu_CreateInfo()
					info.text = "  Slow"
					info.func = function()
						G.animationSpeed = 0.30
						print("Pedro: Animation speed set to Slow")
						CloseDropDownMenus() 
					end
					info.checked = (G.animationSpeed == 0.30)
					info.closeWhenClicked = true
					UIDropDownMenu_AddButton(info, level)
					
					info = UIDropDownMenu_CreateInfo()
					info.text = "  Normal"
					info.func = function()
						G.animationSpeed = 0.15
						print("Pedro: Animation speed set to Normal")
						CloseDropDownMenus() 
					end
					info.checked = (G.animationSpeed == 0.15)
					info.closeWhenClicked = true
					UIDropDownMenu_AddButton(info, level)
					
					info = UIDropDownMenu_CreateInfo()
					info.text = "  Fast"
					info.func = function()
						G.animationSpeed = 0.10
						print("Pedro: Animation speed set to Fast")
						CloseDropDownMenus() 
					end
					info.checked = (G.animationSpeed == 0.10)
					info.closeWhenClicked = true
					UIDropDownMenu_AddButton(info, level)
					
                    info = UIDropDownMenu_CreateInfo()
					info.text = "  Turbo"
					info.func = function()
						G.animationSpeed = 0.05
						print("Pedro: Animation speed set to Turbo")
						CloseDropDownMenus() 
					end
					info.checked = (G.animationSpeed == 0.05)
					info.closeWhenClicked = true
					UIDropDownMenu_AddButton(info, level)
				end
				
			end, "MENU") --
			-- Clear old watcher
			dropdown:SetScript("OnUpdate", nil)
			
			ToggleDropDownMenu(1, nil, dropdown, "cursor", 0, 0)
			
			local openTime = GetTime()
			local leaveTime = nil
			
			local function CursorOver(frame, padding)
				if not frame or not frame:IsShown() then return false end
				
				local scale = frame:GetEffectiveScale()
				local left   = frame:GetLeft()
				local right  = frame:GetRight()
				local bottom = frame:GetBottom()
				local top    = frame:GetTop()
				
				if not left or not right or not top or not bottom then
					return false
				end
				
				local cx, cy = GetCursorPosition()
				cx, cy = cx / scale, cy / scale
				
				padding = padding or 0
				
				return cx >= (left - padding)
				and cx <= (right + padding)
				and cy >= (bottom - padding)
				and cy <= (top + padding)
			end
			
			dropdown:SetScript("OnUpdate", function(self)
				local menu1 = _G["DropDownList1"]
				local menu2 = _G["DropDownList2"]
				
				if not menu1 or not menu1:IsShown() then
					self:SetScript("OnUpdate", nil)
					return
				end
				
				-- Delay to allow Blizzard to anchor the menu
				if GetTime() - openTime < 0.50 then return end
				
				local over1 = CursorOver(menu1, 25)
				local over2 = CursorOver(menu2, 25)
				
				if not over1 and not over2 then
					leaveTime = leaveTime or GetTime()
					
					if GetTime() - leaveTime > 0.2 then
						CloseDropDownMenus()
						self:SetScript("OnUpdate", nil)
					end
					else
					leaveTime = nil
				end
			end)
			
		end
	end,
	
    OnTooltipShow = function(tt)
        tt:AddLine("Pedro")
        tt:AddLine("Left-Click: Pedro/Sound Information")
        tt:AddLine("Right-Click: Options")
	end,
})

-- Register with LibDBIcon
LDBI:Register("Pedro", PedroLDB, PedroDB_Global)
LDBI:Show("Pedro")