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)
	if event ~= "ADDON_LOADED" and not G then
        return
	end
    if event == "ADDON_LOADED" and arg1 == "Pedro" then
        PedroDB_Global = PedroDB_Global or {}
        PedroDB_Char   = PedroDB_Char   or {}
		
        G = PedroDB_Global
        C = PedroDB_Char
		
        G.customSongs = G.customSongs or {}
		
        RebuildPedroSounds()
		
        if G.selectedSound == nil then
            if PedroSounds[2] and PedroSounds[2].path then
                G.selectedSound = PedroSounds[2].path
			end
		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
        if G.enabled == nil then
            G.enabled = true
		end	
		G.minimap = G.minimap or {}
		if G.minimap.hide == nil then
			G.minimap.hide = false
		end
		
		LDBI:Register("Pedro", PedroLDB, G.minimap)
		ApplyMinimapVisibility()
		
		
        C._initialized = true
		
        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")
			
            if PedroAnimationFrame.closeBtn then
                PedroAnimationFrame.closeBtn:Hide()
                PedroAnimationFrame.closeBtn:EnableMouse(false)
			end
			
            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
		
        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
		
        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)
		
		elseif event == "UNIT_AURA" then
        if arg1 == "player" then
            if G.enabled and not InCombatLockdown() then
                TryPlayPedro()
			end
		end
		
		elseif event == "PLAYER_REGEN_DISABLED" then
        if tickerInCombat then
            tickerInCombat:Cancel()
            tickerInCombat = nil
		end
		
        if G.enabled then
            tickerInCombat = C_Timer.NewTicker(1.0, TryPlayPedro)
		end
		
		elseif event == "PLAYER_REGEN_ENABLED" then
        if tickerInCombat then
            tickerInCombat:Cancel()
            tickerInCombat = nil
		end
		
        lustActive = false
	end
end)
