--[[--------------------------------------------------------------------
  PaladinHolyPower – ui/settings_panel/checkboxes.lua
  Checkbox Utilities for settings panel, supports any tab/subtable.
----------------------------------------------------------------------]]

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

LockBox    = LockBox    or {}
MinimapBox = MinimapBox or {}
HPBarBox   = HPBarBox   or {}

-- per-style helper for HP
local function ActiveHP(S)
  if not S then return {} end
  S.styles = S.styles or {}
  local style = S.style or "default"
  S.styles[style] = S.styles[style] or {}
  return S.styles[style]
end

-- ========================= HPL (LOG) TAB =========================

-- Enable/Disable checkbox for Holy Power Log (HPL)
function LockBox.AddEnableHPLCheckbox(parent, PHP, S, x, y, key)
  local settingKey = key or "enabled"
  local checkbox = PHP.CreateEnableCheckbox(
    parent,
    "Enable",
    S[settingKey] ~= false, -- default to enabled if nil
    x, y,
    function(checked)
      S[settingKey] = checked
      if PHP.InitializeHPLTrackerEnable then
        PHP.InitializeHPLTrackerEnable(checked)
      end
      if PHP.UpdateHPLTracker then
        PHP.UpdateHPLTracker()
      end
      if PHP._HPL_ApplyVisibility then
        PHP._HPL_ApplyVisibility()
      end
    end,
    "Enable Tracker",
    "Enable or disable the Wasted Holy Power tracker completely."
  )
  return checkbox
end

-- ========================= INFO TAB =========================

-- Minimap Button Checkbox (default key: "hideMinimap")
function MinimapBox.AddMinimapCheckbox(parent, PHP, S, x, y, key)
  local settingKey = key or "hideMinimap"
  return PHP.CreateModernCheckbox(
    parent,
    "Hide Minimap Button",
    S[settingKey],
    x, y,
    function(checked)
      S[settingKey] = checked
      if PHP.OnMinimapVisibilityChanged then
        PHP.OnMinimapVisibilityChanged(checked)
      end
    end,
    "Hide Minimap Button",
    "Hides the minimap button for this addon."
  )
end

-- ======================= HOLY POWER BAR TAB =======================

-- Enable/Disable checkbox for Holy Power Bar tracker
function HPBarBox.AddEnableHPCheckbox(parent, PHP, S, x, y, key)
  local settingKey = key or "enabled"
  local checkbox = PHP.CreateEnableCheckbox(
    parent,
    "Enable",
    S[settingKey] ~= false,
    x, y,
    function(checked)
      S[settingKey] = checked
      if PHP.InitializeHPTrackerEnable then
        PHP.InitializeHPTrackerEnable(checked)
      end
      if PHP.UpdateHPTracker then
        PHP.UpdateHPTracker()
      end
    end,
    "Enable Tracker",
    "Enable or disable the Holy Power Bar tracker completely."
  )
  return checkbox
end

-- Pulse Animation (now PER-STYLE)
function HPBarBox.AddEnablePulseCheckbox(parent, PHP, S, x, y, key)
  local settingKey = key or "pulseAnim"
  local active     = ActiveHP(S)

  return PHP.CreateModernCheckbox(
    parent,
    "Pulse Animation",
    (active[settingKey] ~= false),  -- treat nil as enabled by default
    x, y,
    function(checked)
      ActiveHP(S)[settingKey] = checked
      if PHP.UpdateHPTracker then PHP.UpdateHPTracker() end
    end,
    "Pulse Animation",
    "Enable a pulse animation when you gain Holy Power."
  )
end

return {
  LockBox    = LockBox,
  MinimapBox = MinimapBox,
  HPBarBox   = HPBarBox,
}