local _, ns = ...
local MythicTools = ns.MythicTools

local ADDON_ICON = "Interface\\AddOns\\SkyMythicHistory\\Media\\smh.tga"
local LDB_NAME = "SkyMythicHistory"

-- The minimap button is a standard LibDataBroker launcher rendered by
-- LibDBIcon-1.0. This is what minimap-button collectors (MBB!, Minimap Button
-- Bag, SexyMap, ...) and the Addon Compartment expect, so the button behaves
-- like every other addon's instead of fighting them.

function MythicTools:GetMinimapButtonDB()
    if not (self.db and self.db.settings) then
        return nil
    end

    local settings = self.db.settings
    if type(settings.minimap) ~= "table" then
        settings.minimap = {}

        -- Migrate the legacy custom-button position (degrees, possibly
        -- negative) and visibility into the LibDBIcon layout.
        local legacyAngle = tonumber(settings.minimapAngle)
        settings.minimap.minimapPos = ((legacyAngle or -35) % 360)
        settings.minimap.hide = settings.showMinimapButton == false
    end

    return settings.minimap
end

function MythicTools:InitializeMinimapButton()
    if self.minimapButtonInitialized then
        return true
    end

    local LDB = LibStub and LibStub:GetLibrary("LibDataBroker-1.1", true)
    local DBIcon = LibStub and LibStub:GetLibrary("LibDBIcon-1.0", true)
    local buttonDB = self:GetMinimapButtonDB()
    if not (LDB and DBIcon and buttonDB) then
        return false
    end

    local dataObject = LDB:GetDataObjectByName(LDB_NAME) or LDB:NewDataObject(LDB_NAME, {
        type = "launcher",
        text = "Sky Mythic History",
        icon = ADDON_ICON,
        OnClick = function(_, mouseButton)
            if type(MythicTools.ToggleMainFrame) ~= "function" then
                return
            end

            if mouseButton == "RightButton" then
                MythicTools:ToggleMainFrame(true)
                if type(MythicTools.ShowTab) == "function" then
                    MythicTools:ShowTab("Settings")
                end
            else
                MythicTools:ToggleMainFrame()
            end
        end,
        OnTooltipShow = function(tooltip)
            if not tooltip or not tooltip.AddLine then
                return
            end
            tooltip:AddLine(MythicTools:L("ADDON_TITLE"), 1, 0.82, 0)
            tooltip:AddLine(MythicTools:L("MINIMAP_LEFT_CLICK"), 1, 1, 1)
            tooltip:AddLine(MythicTools:L("MINIMAP_RIGHT_CLICK"), 1, 1, 1)
        end,
    })

    if not dataObject then
        return false
    end

    if not DBIcon:IsRegistered(LDB_NAME) then
        DBIcon:Register(LDB_NAME, dataObject, buttonDB)
    end

    self.minimapButton = DBIcon:GetMinimapButton(LDB_NAME)
    self.minimapButtonInitialized = true
    return true
end

function MythicTools:UpdateMinimapButton()
    if not (self.db and self.db.settings) then
        return
    end

    if not self:InitializeMinimapButton() then
        return
    end

    local DBIcon = LibStub and LibStub:GetLibrary("LibDBIcon-1.0", true)
    local buttonDB = self:GetMinimapButtonDB()
    if not (DBIcon and buttonDB) then
        return
    end

    buttonDB.hide = self.db.settings.showMinimapButton == false
    DBIcon:Refresh(LDB_NAME, buttonDB)
end
