local _, PUP = ...

PUP.specs = {}

local LDB = LibStub("LibDataBroker-1.1")
local LDBIcon = LibStub("LibDBIcon-1.0")
local eventFrame = CreateFrame("Frame")

eventFrame:RegisterEvent("ADDON_LOADED")
eventFrame:RegisterEvent("PLAYER_LOGIN")

eventFrame:SetScript(
    "OnEvent",
    function(_, event, ...)
        if event == "ADDON_LOADED" then
            local addOnName = ...
            if addOnName == "PrepullUpdater" then
                if not PrepullUpdaterSaved then PrepullUpdaterSaved = {} end
                if not PrepullUpdaterSaved.minimap then PrepullUpdaterSaved.minimap = {} end
                if not PrepullUpdaterSaved.settings then PrepullUpdaterSaved.settings = {} end
                if not PrepullUpdaterSaved.settings.frames then PrepullUpdaterSaved.settings.frames = {} end
                if PrepullUpdaterSaved.settings.readyCheckPopup == nil then PrepullUpdaterSaved.settings.readyCheckPopup = true end
                if PrepullUpdaterSaved.settings.disableBigWigsAssignments == nil then PrepullUpdaterSaved.settings.disableBigWigsAssignments = true end
                if PrepullUpdaterSaved.settings.checkCooldowns == nil then PrepullUpdaterSaved.settings.checkCooldowns = false end
                PUP.CooldownList = PUP.CooldownList or {}

                if not InCombatLockdown() then
                    SetCVar("Sound_NumChannels", 128)
                end

                PUP.version = tonumber(C_AddOns.GetAddOnMetadata(addOnName, "Version"))
                
                -- Minimap icon
                PUP.LDB = LDB:NewDataObject(
                    "Prepull Updater",
                    {
                        type = "data source",
                        text = "Prepull Updater",
                        icon = [[Interface\Addons\PrepullUpdater\Media\Textures\minimap_logo.tga]],
                        OnClick = function() PUP.window:SetShown(not PUP.window:IsShown()) end
                    }
                )

                LDBIcon:Register("Prepull Updater", PUP.LDB, PrepullUpdaterSaved.minimap)

                local minimapButton = LDBIcon:GetMinimapButton("Prepull Updater")

                minimapButton:SetScript("OnEnter", function(self)
                    GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
                    GameTooltip:AddLine("|cffFFD200Prepull Updater|r")
                    GameTooltip:AddLine("Linksklick: Fenster ein-/ausblenden", 0.8, 0.8, 0.8)
                    GameTooltip:Show()
                end)

                minimapButton:SetScript("OnLeave", function(self)
                    GameTooltip:Hide()
                end)

                PUP:UpdateMinimapIconVisibility()
                PUP:InitializeBigWigsDisabler()
                PUP:InitializeWeakAurasImporter()
                PUP:InitializeInterface()
                PUP:InitializeAuraUpdater()
                PUP:InitializeAuraChecker()
                PUP:InitializeOtherChecker()
                PUP:DeleteOldWeakAuras()
            end
        elseif event == "PLAYER_LOGIN" then
            PUP:InitializeCooldowns()
            if C_AddOns.IsAddOnLoaded("WeakAuras") then
                PUP:DeleteOldWeakAuras()
            end
			
			local pafound = false
			local macrocount = 0
			
			if C_AddOns.IsAddOnLoaded("MegaMacro") then return end -- don't mess with macros if user has MegaMacro as it will spam create macros
			for i=1, 120 do
				local macroname = C_Macro.GetMacroName(i)
				if not macroname then break end
				macrocount = i
				if macroname == "Prepull PA Macro" then
					local macrotext = "/run NSAPI:PrivateAura();"
					macrotext = macrotext.."\n/ping [@player] Warning;"
					EditMacro(i, "Prepull PA Macro", 132288, macrotext, false)
					pafound = true
				end
				if pafound then break end
			end
			
			if macrocount >= 120 and not pafound then
				print("You reached the global Macro cap so the Private Aura Macro could not be created")
			elseif not pafound then
				macrocount = macrocount+1            
				local macrotext = "/run NSAPI:PrivateAura();"
				macrotext = macrotext.."\n/ping [@player] Warning;"
				CreateMacro("Prepull PA Macro", 132288, macrotext, false)
			end
        end
    end
)

SLASH_PREPULLUPDATER1 = "/ppu"
SLASH_PREPULLUPDATER2 = "/prepullupdate"
SLASH_PREPULLUPDATER3 = "/prepullupdater"
SLASH_PREPULLUPDATER4 = "/pu"

function SlashCmdList.PREPULLUPDATER()
    PUP.window:SetShown(not PUP.window:IsShown())
end

