-- WarBoard_Minimap.lua
-- Minimap button (LibDBIcon) + addon-compartment launcher.
--   Left-click  → open / close the War Board.
--   Right-click → open the guide.
-- The icon ("tracking-warboard", file 1863940) matches the addon's .toc IconTexture.

local addonName, WB = ...

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

local ICON = 1863940   -- "tracking-warboard"

-- LibDBIcon persists the button's position / hidden state in this table.
local function MinimapDB()
    WarBoardCharDB = WarBoardCharDB or {}
    WarBoardCharDB.minimap = WarBoardCharDB.minimap or {}
    return WarBoardCharDB.minimap
end

local registered = false
local function SetupMinimap()
    if registered or not (LDB and LDBIcon) then return end

    local dataObject = LDB:NewDataObject(addonName, {
        type  = "launcher",
        label = "War Board",
        icon  = ICON,
        OnClick = function(_, mouseButton)
            if mouseButton == "RightButton" then
                if WB.UI and WB.UI.ShowGuide then WB.UI.ShowGuide() end
            elseif WB.UI and WB.UI.Toggle then
                WB.UI.Toggle()
            end
        end,
        OnTooltipShow = function(tt)
            tt:AddLine("War Board")
            tt:AddLine("|cffffff00Left-click|r  Open the War Board", 0.9, 0.9, 0.9)
            tt:AddLine("|cffffff00Right-click|r  Open the guide",     0.9, 0.9, 0.9)
        end,
    })
    if not dataObject then return end   -- already created (e.g. a reload race)

    LDBIcon:Register(addonName, dataObject, MinimapDB())
    registered = true
end

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", function(self)
    self:UnregisterEvent("PLAYER_LOGIN")
    SetupMinimap()
end)

-- Public show/hide for the minimap button (e.g. a future options toggle).
WB.Minimap = WB.Minimap or {}
function WB.Minimap.SetShown(show)
    MinimapDB().hide = not show
    if LDBIcon and registered then
        if show then LDBIcon:Show(addonName) else LDBIcon:Hide(addonName) end
    end
end
