--[[--------------------------------------------------------------------
  PaladinHolyPower - ui/blizzard_ui.lua
  Hide/Show Blizzard's Paladin Holy Power bar based on our HP Bar setting.

  Behavior:
    • If our HP Bar (S.HP.enabled) is enabled  → hide Blizzard bar
    • If our HP Bar is disabled                 → show Blizzard bar
----------------------------------------------------------------------]]

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

-- Only relevant for Paladins who actually use Holy Power.
local function IsPaladinWithHolyPower()
  local _, class = UnitClass("player")
  if class ~= "PALADIN" then return false end
  local HOLY = (Enum and Enum.PowerType and Enum.PowerType.HolyPower) or 9
  return (UnitPowerMax("player", HOLY) or 0) > 0
end

local function GetHPSettings()
  return PHP.settings and PHP.settings.HP or nil
end

-- Should we hide Blizzard's bar right now?
local function ShouldHideBlizz()
  if not IsPaladinWithHolyPower() then return false end
  local S = GetHPSettings()
  -- Our HP bar is considered "enabled" unless explicitly false.
  return S and (S.enabled ~= false)
end

-- Ensure we have a single guarded OnShow hook we can toggle logic around
local function AttachShowHook(f)
  if not f or f._php_hooked_show then return end
  f:HookScript("OnShow", function(self)
    if ShouldHideBlizz() then self:Hide() end
  end)
  f._php_hooked_show = true
end

local function UpdateBlizzardHolyPowerBar()
  local f = _G.PaladinPowerBarFrame
  if not f or not f.IsObjectType or not f:IsObjectType("Frame") then return end

  AttachShowHook(f)

  if ShouldHideBlizz() then
    -- Hide it now; if Blizzard tries to re-show, our OnShow hook hides again.
    f:Show()  -- trigger OnShow if needed so our hook can immediately hide
    f:Hide()
  else
    -- Allow it to be visible (don’t unregister events; we want it functional)
    if IsPaladinWithHolyPower() then
      f:Show()
    else
      f:Hide()
    end
  end
end

-- Events where the Blizzard bar may (re)appear or player state changes
local evt = CreateFrame("Frame")
evt:RegisterEvent("PLAYER_LOGIN")
evt:RegisterEvent("PLAYER_ENTERING_WORLD")
evt:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
evt:RegisterEvent("PLAYER_TALENT_UPDATE")
evt:RegisterEvent("UNIT_ENTERED_VEHICLE")
evt:RegisterEvent("UNIT_EXITED_VEHICLE")
evt:RegisterEvent("PLAYER_REGEN_ENABLED")
evt:SetScript("OnEvent", function()
  UpdateBlizzardHolyPowerBar()
end)

-- Also update immediately whenever our addon refreshes its HP tracker
if type(PHP.UpdateHPTracker) == "function" and type(hooksecurefunc) == "function" then
  hooksecurefunc(PHP, "UpdateHPTracker", function()
    UpdateBlizzardHolyPowerBar()
  end)
end

-- Expose for internal calls (optional)
PHP.UpdateDefaultHolyPowerBarVisibility = UpdateBlizzardHolyPowerBar