local addonName, ZL = ...

local OBJECT_NAME = "ZoneLocked"
local DISPLAY_NAME = "Zone Locked"
local LOGO_TEXTURE = "Interface\\AddOns\\ZoneLocked\\assets\\zonelocked-logo.png"

function ZL:CreateMinimapButton()
    if self.MinimapDataObject then return end
    local LDB = LibStub and LibStub("LibDataBroker-1.1", true)
    local DBIcon = LibStub and LibStub("LibDBIcon-1.0", true)
    if not LDB or not DBIcon then
        self:Print("LibDataBroker or LibDBIcon could not be loaded; the minimap button is unavailable.")
        return
    end

    local dataObject = LDB:NewDataObject(OBJECT_NAME, {
        type = "launcher",
        label = DISPLAY_NAME,
        text = DISPLAY_NAME,
        icon = LOGO_TEXTURE,
        OnClick = function(_, mouseButton)
            if mouseButton == "RightButton" then ZL:OpenSettings() else ZL:ToggleUI() end
        end,
        OnTooltipShow = function(tooltip)
            tooltip:AddLine(DISPLAY_NAME, 1, 0.82, 0.15)
            local mapID = ZL:GetActiveZone()
            local data = mapID and ZL.ZoneData[mapID]
            if data then
                local done, total = ZL:GetProgress(data)
                local percent = total == 0 and 100 or math.floor((done / total) * 100 + 0.5)
                tooltip:AddLine(string.format("%s: %d%% (%d/%d)", data.name, percent, done, total), 0.40, 0.82, 1)
            else
                tooltip:AddLine("No active journey", 0.65, 0.68, 0.72)
            end
            tooltip:AddLine(" ")
            tooltip:AddLine("Left-click: open Zone Locked", 1, 1, 1)
            tooltip:AddLine("Right-click: open Settings", 1, 1, 1)
            tooltip:AddLine("Drag: move around the minimap", 1, 1, 1)
        end,
    })

    DBIcon:Register(OBJECT_NAME, dataObject, ZL_Saved.ui.minimap)
    self.MinimapDataObject = dataObject
    self.MinimapButton = DBIcon:GetMinimapButton(OBJECT_NAME)
end
