local ADDON_NAME, ns = ...
ns = ns or {}
local GetAddOnMetadata = GetAddOnMetadata
local C_AddOns = C_AddOns
local type = type
local tostring = tostring
local tonumber = tonumber

local MinimapMod = {}
ns.Minimap = MinimapMod

local ICON = "Interface\\AddOns\\" .. (ADDON_NAME or "AlmostCompletedAchievements") .. "\\Media\\Icon.png"
local SHARED_ICON = "Interface\\AddOns\\" .. (ADDON_NAME or "AlmostCompletedAchievements") .. "\\Templates\\Media\\Launcher\\kagrok_full.png"
local DISPLAY_NAME = "Almost Completed Achievements"

local function openPanel()
    if ns.Panel and ns.Panel.Toggle then
        ns.Panel:Toggle()
    end
end

local function openOptions()
    if ns.OpenOptionsPanel then
        ns.OpenOptionsPanel()
    end
end

local function getAddonMetadataValue(field)
    local value = ""
    if type(GetAddOnMetadata) == "function" then
        value = tostring(GetAddOnMetadata(ADDON_NAME, field) or "")
        if value == "" then
            value = tostring(GetAddOnMetadata("AlmostCompletedAchievements", field) or "")
        end
    end
    if value == "" and C_AddOns and type(C_AddOns.GetAddOnMetadata) == "function" then
        value = tostring(C_AddOns.GetAddOnMetadata(ADDON_NAME, field) or "")
        if value == "" then
            value = tostring(C_AddOns.GetAddOnMetadata("AlmostCompletedAchievements", field) or "")
        end
    end
    return value
end

local function getLauncherPriority()
    return tonumber(getAddonMetadataValue("X-KagrokLauncherPriority")) or 90
end

local function getResultCount()
    return (ns.State and ns.State.results and #ns.State.results) or 0
end

local function isPanelShown()
    return ns.Panel and ns.Panel.frame and ns.Panel.frame:IsShown()
end

local function isTrackerShown()
    if ns.Tracker and ns.Tracker.frame and ns.Tracker.frame:IsShown() then
        return true
    end
    return ACAU_Top5DB and ACAU_Top5DB.shown == true
end

function MinimapMod:BuildSharedLauncherMenuItems()
    return {
        {
            text = isPanelShown() and "Hide main panel" or "Open main panel",
            func = function()
                openPanel()
            end,
        },
        {
            text = "Run scan now",
            func = function()
                if ns.TriggerScan then
                    ns.TriggerScan(true)
                end
            end,
        },
        {
            text = isTrackerShown() and "Hide Top 5" or "Show Top 5",
            func = function()
                if ns.Tracker and ns.Tracker.Toggle then
                    ns.Tracker:Toggle()
                end
            end,
        },
    }
end

function MinimapMod:BuildSharedLauncherSharedItems()
    return {
        {
            key = "developer_info",
            text = "Developer Info",
            order = 100,
            func = function()
                ns.ShowDeveloperInfoPanel()
            end,
        },
    }
end

function MinimapMod:EnsureSharedLauncher()
    if self.sharedLauncher and self._sharedLauncherRegistered then
        return self.sharedLauncher
    end

    local launcher = ns.KagrokSharedLauncher
    if type(launcher) ~= "table" or type(launcher.RegisterAddon) ~= "function" then
        return nil
    end

    self.sharedLauncher = launcher
    launcher:RegisterAddon({
        id = ADDON_NAME,
        addon_name = ADDON_NAME,
        name = DISPLAY_NAME,
        priority = getLauncherPriority(),
        icon = ICON,
        shared_icon = SHARED_ICON,
        is_enabled = function()
            return true
        end,
        left_click = function()
            openPanel()
        end,
        tooltip = function(_, tooltip)
            tooltip:SetText(DISPLAY_NAME)
            tooltip:AddLine("Left Click: Toggle main panel", 1, 1, 1)
            tooltip:AddLine("Right Click: Launcher menu", 1, 1, 1)
            tooltip:AddLine("Drag: Move along minimap", 0.65, 0.85, 1.0)
            tooltip:AddLine(" ")
            tooltip:AddDoubleLine("Results", tostring(getResultCount()), 1, 1, 1, 0.4, 0.9, 0.4)
            tooltip:AddDoubleLine("Top 5", isTrackerShown() and "Shown" or "Hidden", 1, 1, 1, 0.55, 0.78, 1)
        end,
        menu_items = function()
            return self:BuildSharedLauncherMenuItems()
        end,
        shared_items = function()
            return self:BuildSharedLauncherSharedItems()
        end,
    })
    self._sharedLauncherRegistered = true
    return launcher
end

function MinimapMod:EnsureMinimapButton()
    local launcher = self:EnsureSharedLauncher()
    if launcher and launcher.EnsureButton then
        return launcher:EnsureButton()
    end
    return nil
end

function MinimapMod:UpdateMinimapButtonVisibility()
    local launcher = self:EnsureSharedLauncher()
    if launcher and launcher.Refresh then
        launcher:Refresh()
    end
end

function MinimapMod:Init()
    if self.initialized then return end
    self.initialized = true

    self:EnsureMinimapButton()
    self:UpdateMinimapButtonVisibility()
end
