-- Minimap.lua
-- Registers a standard LibDataBroker launcher and hands it to LibDBIcon-1.0
-- for the actual minimap button. Using the standard libraries (instead of a
-- hand-rolled button) means minimap-button managers like MBB/MinimapButtonButton
-- recognize and can collect/hide this button like any other addon's.

local WMM = WheresMeMouse
local ICON_TEXTURE = "Interface\\AddOns\\WheresMeMouse\\Media\\icon.png"

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

local dataObject = LDB:NewDataObject("WheresMeMouse", {
    type = "launcher",
    text = "Where's me mouse?",
    icon = ICON_TEXTURE,
    OnClick = function(self, mouseButton)
        if mouseButton == "RightButton" then
            WMM.db.enabled = not WMM.db.enabled
            WMM.UpdateVisibility()
            if WMM.RefreshOptionsPanel then WMM.RefreshOptionsPanel() end
            print("|cff4de0ffWhere's me mouse?|r " .. (WMM.db.enabled and "enabled" or "disabled"))
        else
            if WMM.OpenOptions then
                WMM.OpenOptions()
            end
        end
    end,
    OnTooltipShow = function(tooltip)
        tooltip:AddLine("Where's me mouse?")
        tooltip:AddLine("Left-click: options", 0.9, 0.9, 0.9)
        tooltip:AddLine("Right-click: toggle on/off", 0.9, 0.9, 0.9)
        tooltip:AddLine("Drag: move this button", 0.9, 0.9, 0.9)
    end,
})

function WMM.CreateMinimapButton()
    if LDBIcon:IsRegistered("WheresMeMouse") then
        WMM.UpdateMinimapButton()
        return
    end
    LDBIcon:Register("WheresMeMouse", dataObject, WMM.db.minimap)
end

function WMM.UpdateMinimapButton()
    if not LDBIcon:IsRegistered("WheresMeMouse") then return end
    if WMM.db.minimap.hide then
        LDBIcon:Hide("WheresMeMouse")
    else
        LDBIcon:Show("WheresMeMouse")
    end
end
