--[[--------------------------------------------------------------------
  PaladinHolyPower – ui/settings_panel/sliders.lua
  CONF: sliders for HPL (wasted HP tracker) and HP (holy power bar)
  Note: Position offsets are now handled by edit_handle.lua, not here.
----------------------------------------------------------------------]]

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

local Util    = Util or PHP.Util
Sliders       = Sliders or {}

-- Helper to get the active style table for HP
local function ActiveHP(S)
  return S.styles and S.styles[S.style] or S
end

local CONF = {
  -- ======================= GENERAL TAB (HPL) =======================
  HPL_iconSize = {
    label = "Icon Size",
    range = {10, 100},
    step  = 1,
    get   = function(S) return S.iconSize end,
    set   = function(S, PHP, v)
      S.iconSize = v
      if PHP.UpdateHPLTracker then PHP.UpdateHPLTracker() end
    end,
    isPercent = false,
    tipTitle  = "Icon Size",
    tipText   = "Adjusts the tracker icon size.",
  },
  HPL_textSize = {
    label = "Text Size",
    range = {10, 100},
    step  = 1,
    get   = function(S) return S.textSize end,
    set   = function(S, PHP, v)
      S.textSize = v
      if PHP.UpdateHPLTracker then PHP.UpdateHPLTracker() end
    end,
    isPercent = false,
    tipTitle  = "Text Size",
    tipText   = "Adjusts the tracker text size.",
  },
  HPL_zoom = {
    label = "Zoom",
    range = {0, 100},
    step  = 1,
    get   = function(S) return math.floor((S.zoom or 0) * 100) end,
    set   = function(S, PHP, v)
      S.zoom = v / 100
      if PHP.UpdateHPLTracker then PHP.UpdateHPLTracker() end
    end,
    isPercent = true,
    tipTitle  = "Zoom",
    tipText   = "Adjusts the tracker icon zoom.",
  },
  HPL_alpha = {
    label = "Alpha",
    range = {0, 100},
    step  = 1,
    get   = function(S) return math.floor((S.alpha or 1) * 100) end,
    set   = function(S, PHP, v)
      S.alpha = v / 100
      if PHP.UpdateHPLTracker then PHP.UpdateHPLTracker() end
    end,
    isPercent = true,
    tipTitle  = "Alpha",
    tipText   = "Adjusts the tracker icon transparency.",
  },

  -- ======================= HOLY POWER TAB (HP) =======================
  -- NOTE: Visibility differs per style; handled below via style bus.
  hp_iconSize = {
    label = "Size",
    range = {10, 100},
    step  = 1,
    get   = function(S) return ActiveHP(S).iconSize end,
    set   = function(S, PHP, v)
      ActiveHP(S).iconSize = v
      if PHP.UpdateHPTracker then PHP.UpdateHPTracker() end
    end,
    isPercent = false,
    tipTitle  = "Icon Size",
    tipText   = "Adjusts the holy power icon size.",
  },
  hp_width = {
    label = "Width",
    range = {10, 200},
    step  = 1,
    get   = function(S) return ActiveHP(S).width end,
    set   = function(S, PHP, v)
      ActiveHP(S).width = v
      if PHP.UpdateHPTracker then PHP.UpdateHPTracker() end
    end,
    isPercent = false,
    tipTitle  = "Bar Width",
    tipText   = "Adjusts the holy power bar width.",
  },
  hp_height = {
    label = "Height",
    range = {6, 100},
    step  = 1,
    get   = function(S) return ActiveHP(S).height end,
    set   = function(S, PHP, v)
      ActiveHP(S).height = v
      if PHP.UpdateHPTracker then PHP.UpdateHPTracker() end
    end,
    isPercent = false,
    tipTitle  = "Bar Height",
    tipText   = "Adjusts the holy power bar height.",
  },
  hp_iconSpacing = {
    label = "Spacing",
    range = {0, 24},
    step  = 1,
    get   = function(S) return ActiveHP(S).iconSpacing or 6 end,
    set   = function(S, PHP, v)
      ActiveHP(S).iconSpacing = v
      if PHP.UpdateHPTracker then PHP.UpdateHPTracker() end
    end,
    isPercent = false,
    tipTitle  = "Icon Spacing",
    tipText   = "Adjusts the holy power horizontal spacing.",
  },
  hp_verticalWave = {
    label = "Wave",
    range = {-50, 50},
    step  = 1,
    get   = function(S) return ActiveHP(S).verticalWave or 0 end,
    set   = function(S, PHP, v)
      ActiveHP(S).verticalWave = v
      if PHP.UpdateHPTracker then PHP.UpdateHPTracker() end
    end,
    isPercent = false,
    tipTitle  = "Wave",
    tipText   = "Adjusts the holy power vertical spacing",
  },
  hp_zoom = {
    label = "Zoom",
    range = {0, 100},
    step  = 1,
    get   = function(S) return math.floor((ActiveHP(S).zoom or 0) * 100) end,
    set   = function(S, PHP, v)
      ActiveHP(S).zoom = v / 100
      if PHP.UpdateHPTracker then PHP.UpdateHPTracker() end
    end,
    isPercent = true,
    tipTitle  = "Zoom",
    tipText   = "Adjusts the holy power icon zoom",
  },
  hp_alpha = {
    label = "Alpha",
    range = {0, 100},
    step  = 1,
    get   = function(S) return math.floor((ActiveHP(S).alpha or 1) * 100) end,
    set   = function(S, PHP, v)
      ActiveHP(S).alpha = v / 100
      if PHP.UpdateHPTracker then PHP.UpdateHPTracker() end
    end,
    isPercent = true,
    tipTitle  = "Alpha",
    tipText   = "Adjusts the holy power transparency.",
  },
}

-- HPL tab: iconSize, textSize, zoom, alpha
-- HP tab:
--   * Default/Custom Icons → iconSize, iconSpacing, verticalWave, zoom, alpha
--   * Custom Bar           → width, height, iconSpacing, verticalWave, zoom, alpha

function Sliders.AddSlider(parent, PHP, S, offsetX, offsetY, key)
  local cfg = CONF[key]; if not cfg then return end
  local min, max
  if type(cfg.range) == "function" then
    min, max = cfg.range()
  else
    min, max = cfg.range[1], cfg.range[2]
  end

  local slider = PHP.CreateSlider(
    "PHP_Slider_" .. key,
    cfg.label, min, max, cfg.step, cfg.get(S),
    parent, offsetX, offsetY,
    function(v) cfg.set(S, PHP, v) end,
    cfg.isPercent,
    cfg.tipTitle,
    cfg.tipText
  )

  if Util and slider.valueText then
    Util.MakeValueLabelEditable(slider, slider.valueText, min, max,
      function(v) cfg.set(S, PHP, v) end)
  end

  -- === Visibility rules (listen to actual style bus) ===
  if key == "hp_iconSize" or key == "hp_width" or key == "hp_height" then
    local function ApplyVisibility(style)
      style = tostring(style or S.style or "default"):lower()
      if key == "hp_iconSize" then
        slider:SetShown(style == "default" or style == "custom")
      else -- hp_width / hp_height
        slider:SetShown(style == "custom_bar")
      end
    end

    -- initial state
    ApplyVisibility(S.style)

    -- subscribe to the HPBarDD bus (created in dropdowns.lua)
    local HPBarDD = _G.HPBarDD
    if HPBarDD and HPBarDD._RegisterStyleListener then
      HPBarDD._RegisterStyleListener(ApplyVisibility)
    end
  end

  -- hp_verticalWave: available for ALL styles → always shown, no filter needed.

  return slider
end

return Sliders