local addonName, TOAST = ...

local icon = LibStub("LibDBIcon-1.0")
local ldb  = LibStub("LibDataBroker-1.1")

local dataobj = ldb:NewDataObject("TOAST", {
    type = "launcher",
    icon = "Interface\\AddOns\\TOAST\\media\\TOAST",
    OnClick = function(_, button)
        if button == "LeftButton" then
            TOAST.commands[""]()
        elseif button == "RightButton" then
            if TOAST.CheckOnboardingProgress then
                TOAST.CheckOnboardingProgress("lb")
                return
            end

            -- Right-click opens the leaderboard directly, so it needs the same
            -- community gate as TOAST.commands[""] or it's a way around it.
            if TOAST.IsInCommunity then
                TOAST.inCommunity = TOAST.IsInCommunity()
            end
            if not TOAST.inCommunity then
                if TOAST.ShowCommunityGate then
                    TOAST.ShowCommunityGate()
                end
                return
            end
            TOAST.commands["lb"]()
        end
    end,
    OnTooltipShow = function(tooltip)
        tooltip:AddLine("|cff33ff99TOAST|r")
        tooltip:AddLine("Left: Dashboard", 1, 1, 1)
        tooltip:AddLine("Right: Leaderboard", 1, 1, 1)
    end,
})

local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:SetScript("OnEvent", function(self, event, name)
    if name ~= addonName then return end
    self:UnregisterEvent(event)
    TOAST_DB.minimap = TOAST_DB.minimap or {}
    icon:Register("TOAST", dataobj, TOAST_DB.minimap)
end)
