local _, PUP = ...

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

eventFrame:RegisterEvent("ADDON_LOADED")

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

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

