---@class AddonPrivate
local Private = select(2, ...)

---@class MinimapButton
local minimapButton = {
    ---@class MinimapIcon : table, LibDBIcon-1.0
    icon = {},
}
Private.MinimapButton = minimapButton

local const = Private.constants

function minimapButton:Initialize()
    local addon = Private.Addon
    local ldb = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject(const.ADDON_NAME, {
        type = "launcher",
        text = const.ADDON_NAME,
        icon = const.ADDON_LOGO,
        OnClick = function(clickedFrame, button)
            if button == "LeftButton" then
                Private.UI:ToggleUI()
            elseif button == "RightButton" then
                MenuUtil.CreateContextMenu(clickedFrame, function(_, root)
                    root:CreateTitle("Meta Tracker")
                    root:CreateButton("Open Tracker Window", function()
                        Private.UI:ToggleUI()
                    end)
                    root:CreateCheckbox("Nearby Now HUD",
                        function() return Private.NearbyHud:IsEnabled() end,
                        function() Private.NearbyHud:Toggle() end)
                    root:CreateCheckbox("Do-it-now Route",
                        function() return Private.RouteHud:IsEnabled() end,
                        function() Private.RouteHud:Toggle() end)
                    root:CreateCheckbox("Rare-spawn alerts",
                        function() return Private.RareAlerts:IsEnabled() end,
                        function() Private.RareAlerts:Toggle() end)
                    root:CreateCheckbox("Rares: meta-relevant only",
                        function() local s = Private.Settings:Rares(); return s and s.onlyRelevant end,
                        function()
                            local s = Private.Settings:Rares()
                            if s then s.onlyRelevant = not s.onlyRelevant end
                            Private.RareAlerts:UpdateFilterLabel()
                            Private.RareAlerts:Refresh()
                        end)
                    root:CreateCheckbox("Auto open/close by continent",
                        function() local db = Private.Settings and Private.Settings.db; return db and db.autoContinent end,
                        function()
                            local db = Private.Settings and Private.Settings.db
                            if db then
                                db.autoContinent = not db.autoContinent
                                if db.autoContinent then
                                    -- apply immediately for the current continent
                                    if Private.Events then Private.Events.lastContinent = nil; Private.Events:AutoContinent() end
                                end
                            end
                        end)
                    root:CreateCheckbox("Lock HUD position",
                        function() local s = Private.Settings:Hud(); return s and s.locked end,
                        function() local s = Private.Settings:Hud(); if s then s.locked = not s.locked end end)
                    local restore = root:CreateButton("Restore ignored objective")
                    Private.Objectives:BuildIgnoredMenu(restore)
                end)
            end
        end,
        OnTooltipShow = function(tooltip)
            tooltip:AddLine("Meta Achievement Tracker")
            tooltip:AddLine("|cff00ff00Left-Click|r to toggle the tracker.")
            tooltip:AddLine("|cff00ff00Right-Click|r for options.")
        end,
    })

    ---@class MinimapIcon : table, LibDBIcon-1.0
    local icon = LibStub("LibDBIcon-1.0")
    icon:Register(Private.constants.ADDON_NAME, ldb, addon.DB.minimap)
    self.icon = icon
end

function minimapButton:GetIcon()
    return self.icon
end