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
        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

