-- TOG Tools — Minimap Button
-- LibDataBroker-1.1 launcher + LibDBIcon-1.0 minimap button.
--
-- Left-click  → toggle main window
-- Right-click → open Name Prefix tab directly

local _, addon = ...

local LDB = LibStub("LibDataBroker-1.1", true)
if not LDB then return end   -- lib absent; button silently disabled

-- ---------------------------------------------------------------------------
-- LDB data object
-- ---------------------------------------------------------------------------
local dataObj = LDB:NewDataObject("TOGTools", {
    type  = "launcher",
    label = "TOG Tools",
    icon  = "Interface\\AddOns\\TOGTools\\textures\\ToGTools_PH_MMB",

    OnClick = function(_self, button)
        if button == "LeftButton" then
            if addon.MainWindow then
                addon.MainWindow:Toggle()
            end
        elseif button == "RightButton" then
            if addon.MainWindow then
                addon.MainWindow:Open("nameprefix")
            end
        end
    end,

    OnTooltipShow = function(tt)
        tt:AddLine("|cffFF8000TOG Tools|r")
        tt:AddLine(" ")
        tt:AddLine("|cffffd100Left-click|r to toggle the main window")
        tt:AddLine("|cffffd100Right-click|r to open Name Prefix settings")
    end,
})

-- ---------------------------------------------------------------------------
-- Registration
-- ---------------------------------------------------------------------------
local function SetupMinimapButton()
    local icon = LibStub("LibDBIcon-1.0", true)
    if not icon then return end

    local db = addon.DB.char
    if type(db.minimapPos) ~= "number" then
        db.minimapPos = 220
    end

    icon:Register("TOGTools", dataObj, {
        hide       = db.minimapHide or false,
        minimapPos = db.minimapPos,
    })
end

-- Run once the DB is available (OnEnable fires after OnInitialize)
local Ace = addon.lib
local _origEnable = Ace.OnEnable
function Ace:OnEnable()
    if _origEnable then _origEnable(self) end
    SetupMinimapButton()
end

-- ---------------------------------------------------------------------------
-- Show / Hide helpers (called by slash or settings toggle)
-- ---------------------------------------------------------------------------
function addon:ShowMinimapButton()
    addon.DB.char.minimapHide = false
    local icon = LibStub("LibDBIcon-1.0", true)
    if icon then icon:Show("TOGTools") end
end

function addon:HideMinimapButton()
    addon.DB.char.minimapHide = true
    local icon = LibStub("LibDBIcon-1.0", true)
    if icon then icon:Hide("TOGTools") end
end
