--[[--------------------------------------------------------------------
  PaladinHolyPower - modules/minimap.lua
  Minimap Icon & Launcher (Left-click toggles settings; Right-click opens log)
  - Uses PHP.MEDIA for icon path (robust to folder renames)
  - Single visibility function (SafeShowMinimapButton calls it)
  - Tooltip header uses Theme gradient text
----------------------------------------------------------------------]]

local PHP = _G.PaladinHolyPower
if not PHP then return end

function PHP.InitMinimapButton()
  local NAME = "PaladinHolyPower_MinimapButton"
  local ICON = (PHP.MEDIA or "Interface\\AddOns\\PaladinHolyPower\\media\\") .. "holy_power.png"

  local btn = _G[NAME]
  if not btn then
    btn = CreateFrame("Button", NAME, Minimap)
    btn:SetSize(28, 28)
    btn:SetFrameStrata("MEDIUM")
    btn:SetFrameLevel(8)
    btn:SetPoint("TOPLEFT", Minimap, "TOPLEFT", 0, 0)
    btn:EnableMouse(true)
    btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")

    local tex = btn:CreateTexture(nil, "BACKGROUND")
    tex:SetTexture(ICON)
    tex:SetSize(24, 24)
    tex:SetPoint("CENTER", btn, "CENTER", 0, 1)
    tex:SetTexCoord(0.08, 0.92, 0.08, 0.92)
    btn.icon = tex
  else
    -- If PHP.MEDIA changed (e.g., folder rename during reload), refresh icon path
    if btn.icon and btn.icon.SetTexture then
      btn.icon:SetTexture(ICON)
    end
  end

  -- Tooltip with gradient header from Theme.lua
  btn:SetScript("OnEnter", function(self)
    GameTooltip:SetOwner(self, "ANCHOR_LEFT")

    local header = "Paladin Holy Power"
    local Theme = PHP and PHP.Theme
    if Theme and Theme.GradientText then
      -- Uses Theme’s default gradient endpoints (left → right)
      header = Theme.GradientText(header)
    end
    -- IMPORTANT: do NOT pass r,g,b to preserve inline gradient color codes
    GameTooltip:AddLine(header)

    GameTooltip:AddLine("Left-click to open settings", 0.8, 0.8, 0.8)
    GameTooltip:AddLine("Right-click to open logs", 0.8, 0.8, 0.8)
    if InCombatLockdown and InCombatLockdown() then
      GameTooltip:AddLine("Settings unavailable in combat.", 0.91, 0.38, 0.37)
    end
    GameTooltip:Show()
  end)

  btn:SetScript("OnLeave", function()
    GameTooltip:Hide()
  end)

  btn:SetScript("OnClick", function(_, button)
    if button == "LeftButton" then
      local panel = PHP.SettingsPanel
      if panel and panel.IsShown and panel:IsShown() then
        panel:Hide()
        return
      end
      if PHP.ShowSettingsPanel then
        PHP.ShowSettingsPanel()
      elseif SlashCmdList and SlashCmdList.PHP then
        SlashCmdList.PHP()
      else
        ChatFrame_OpenChat("/php ")
      end

    elseif button == "RightButton" then
      if PHP.ToggleHPLog then
        PHP.ToggleHPLog()
      else
        ChatFrame_OpenChat("/php log ")
      end
    end
  end)

  PHP.MinimapButton = btn

  -- Single source of truth for visibility
  function PHP.UpdateMinimapButtonVisibility()
    local b = PHP.MinimapButton
    if not b then return end
    if PHP.settings and PHP.settings.hideMinimap then
      b:Hide()
    else
      b:Show()
    end
  end

  -- Backward-friendly alias used elsewhere
  function PHP.SafeShowMinimapButton()
    PHP.UpdateMinimapButtonVisibility()
  end
end