--[[--------------------------------------------------------------------
  PaladinHolyPower – tracker/holy_power/events.lua
----------------------------------------------------------------------]]

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

-- -------------------------- Settings helpers ------------------------
local function GetSettings()
  local S = PHP.settings or {}
  return S.HP or S
end

local function InCombat()
  return (type(InCombatLockdown)=="function" and InCombatLockdown())
      or (_G.UnitAffectingCombat and _G.UnitAffectingCombat("player"))
      or false
end

-- PREVIEW is ON only when Edit Mode is ON or the HP Bar subtab is active.
local function PreviewOn()
  if PHP.EditModeActive then return true end
  return PHP._ActiveHPSubTab == "hpbar"
end

-- Expose a preview query for the renderer (used for “fill all pips”)
function PHP.HPBar_IsPreview()
  return PreviewOn()
end

-- ---------------------------- Visibility ----------------------------
local function ApplyVisibility()
  local S = GetSettings()
  if S.enabled == false then
    if PHP.DisableHPBarFrame then PHP.DisableHPBarFrame() end
    return
  end

  if InCombat() or PreviewOn() then
    if PHP.EnableHPBarFrame then PHP.EnableHPBarFrame() end
  else
    if PHP.DisableHPBarFrame then PHP.DisableHPBarFrame() end
  end

  if PHP.UpdateHPTracker then PHP.UpdateHPTracker() end
end

function PHP._HPBAR_ApplyVisibility()
  ApplyVisibility()
end

-- ------------------------- Event & Hooks ----------------------------
local f = CreateFrame("Frame", "PHP_HPBar_EventDriver")
f:SetScript("OnEvent", function(_, ev, unit, powerType)
  if ev == "PLAYER_LOGIN" or ev == "PLAYER_ENTERING_WORLD" then
    ApplyVisibility()

  elseif ev == "PLAYER_REGEN_DISABLED" then
    ApplyVisibility()

  elseif ev == "PLAYER_REGEN_ENABLED" then
    ApplyVisibility()

  elseif ev == "UNIT_POWER_UPDATE" then
    if unit == "player" and (powerType == "HOLY_POWER" or powerType == "9" or not powerType) then
      if PHP.UpdateHPTracker then PHP.UpdateHPTracker() end
    end
  end
end)

f:RegisterEvent("PLAYER_LOGIN")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("PLAYER_REGEN_DISABLED")
f:RegisterEvent("PLAYER_REGEN_ENABLED")
f:RegisterEvent("UNIT_POWER_UPDATE")

-- Lightweight hooks to re-apply visibility on panel/edit-mode toggles
if not PHP._HP_VIS_HOOKS then
  PHP._HP_VIS_HOOKS = true

  -- Re-apply when settings panel toggles (but don't force show only because it opened)
  if type(PHP.ShowSettingsPanel) == "function" and _G.hooksecurefunc then
    hooksecurefunc(PHP, "ShowSettingsPanel", function()
      local p = PHP.SettingsPanel
      if p and p.HookScript and not p._hp_vis_hooked then
        p._hp_vis_hooked = true
        p:HookScript("OnShow", function() ApplyVisibility() end)
        p:HookScript("OnHide", function() ApplyVisibility() end)
      end
      ApplyVisibility()
    end)
  end

  -- Re-apply immediately when Edit Mode is toggled
  if type(PHP.ToggleEditMode) == "function" and _G.hooksecurefunc then
    hooksecurefunc(PHP, "ToggleEditMode", function()
      ApplyVisibility()
    end)
  end
end