--[[--------------------------------------------------------------------
  PaladinHolyPower – ui/settings_panel/tabs/hp_bar_subtab.lua
  Holy Power Bar Sub-Tab with style-specific subpanels (fixed width)

  • Three subpanels:
      - DefaultPanel → for "default"
      - IconsPanel   → for "custom"      (includes Custom Icon textbox)
      - BarPanel     → for "custom_bar"  (width/height + texture)

  • Panels SetAllPoints(parent) so left/right columns render correctly.

  • PREVIEW HOOKS (important):
      - OnShow  : sets PHP._ActiveHPSubTab = "hpbar" and calls PHP.UpdateHPTracker()
      - OnHide  : clears the flag and calls PHP.UpdateHPTracker()
    This ensures the bar hides when you leave the subtab (unless Edit Mode is ON).
----------------------------------------------------------------------]]

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

local Sliders   = Sliders   or _G.Sliders
local HPBarDD   = HPBarDD   or _G.HPBarDD
local barTexDD  = HPBarDD   -- alias
local HPBarBox  = HPBarBox  or _G.HPBarBox
local Color     = Color     or _G.Color
local HPBarTB   = HPBarTB   or _G.HPBarTB
local iconTB    = HPBarTB   -- alias

local M = {}
_G.TotalHPSubTab = M

-- Small helpers
local function NewAnchor(parent, point, x, y)
  local f = CreateFrame("Frame", nil, parent)
  f:SetSize(1, 1)
  f:SetPoint(point or "TOPLEFT", parent, point or "TOPLEFT", x or 0, y or 0)
  return f
end

local function GreyOutControls(enabled, list)
  for _, control in ipairs(list or {}) do
    if control and control.Enable and control.Disable then
      if enabled then control:Enable(); control:SetAlpha(1)
      else control:Disable(); control:SetAlpha(0.45) end
    elseif control and control.SetAlpha then
      control:SetAlpha(enabled and 1 or 0.45)
      if control.EnableMouse then control:EnableMouse(enabled) end
    end
  end
end

-- Subtab preview flag management for HP Bar
local function SetHPBarPreviewFlag(active)
  PHP._ActiveHPSubTab = active and "hpbar" or nil
  if PHP._HPBAR_ApplyVisibility then pcall(PHP._HPBAR_ApplyVisibility) end
  if PHP.UpdateHPTracker        then pcall(PHP.UpdateHPTracker)        end
end

function M.Build(parent, PHP, S)
  local spacing = 55

  -- Header anchors for top row
  local LeftTop  = NewAnchor(parent, "TOPLEFT",  0, -30)
  local RightTop = NewAnchor(parent, "TOPRIGHT", 0, -30)

  -- Enable checkbox (top-right)
  local enableCB = HPBarBox.AddEnableHPCheckbox(RightTop, PHP, S, -100, 55)

  -- Style dropdown (top-left) — UI labels: Default / Icons / Bar
  local styleDD  = HPBarDD.AddStyleDropdown(LeftTop, PHP, S, 20, -spacing * 0, "style")

  local allControls = { styleDD, enableCB }

  --------------------------------------------------------------------
  -- DefaultPanel: for style Default
  --------------------------------------------------------------------
  local DefaultPanel = CreateFrame("Frame", nil, parent)
  DefaultPanel:SetAllPoints(parent)

  do
    local L = NewAnchor(DefaultPanel, "TOPLEFT",  0, -30)
    local R = NewAnchor(DefaultPanel, "TOPRIGHT", 0, -30)

    -- Row 1: Icon Size (L) + Spacing (R)
    local slIconSize   = Sliders.AddSlider(L, PHP, S,  20,   -spacing * 0.8, "hp_iconSize")
    local slSpacing1   = Sliders.AddSlider(R, PHP, S, -180,  -spacing * 0.8, "hp_iconSpacing")

    -- Row 2: Vertical Wave (L) + Zoom (R)
    local slWave1      = Sliders.AddSlider(L, PHP, S,  20,   -spacing * 1.8, "hp_verticalWave")
    local slZoom1      = Sliders.AddSlider(R, PHP, S, -180,  -spacing * 1.8, "hp_zoom")

    -- Row 3: Alpha (L) + (Colors start next row)
    local slAlpha1     = Sliders.AddSlider(L, PHP, S,  20,   -spacing * 2.8, "hp_alpha")

    -- Row 4: Colors
    local cpFill1      = Color.AddColorPicker(L, PHP, S,  20,   -spacing * 3.8, "fillColor")
    local cpEmpty1     = Color.AddColorPicker(R, PHP, S, -180,  -spacing * 3.8, "emptyColor")

    -- Row 5: Pulse
    local cbPulse1     = HPBarBox.AddEnablePulseCheckbox(L, PHP, S, 15, -spacing * 4.8)

    for _, w in ipairs({ slIconSize, slSpacing1, slWave1, slZoom1, slAlpha1, cpFill1, cpEmpty1, cbPulse1 }) do
      if w then table.insert(allControls, w) end
    end
  end

  --------------------------------------------------------------------
  -- IconsPanel: for style Custom Icons
  --------------------------------------------------------------------
  local IconsPanel = CreateFrame("Frame", nil, parent)
  IconsPanel:SetAllPoints(parent)

  do
    local L = NewAnchor(IconsPanel, "TOPLEFT",  0, -30)
    local R = NewAnchor(IconsPanel, "TOPRIGHT", 0, -30)

    -- Row 0: Custom Icon textbox (Icons only)
    local tbCustomIcon = iconTB.AddIconTextbox(R, PHP, S, -180, -spacing * 0, "customIcon")

    -- Row 1: Icon Size (L) + Spacing (R)
    local slIconSize   = Sliders.AddSlider(L, PHP, S,  20,   -spacing * 0.8, "hp_iconSize")
    local slSpacing1   = Sliders.AddSlider(R, PHP, S, -180,  -spacing * 0.8, "hp_iconSpacing")

    -- Row 2: Vertical Wave (L) + Zoom (R)
    local slWave2      = Sliders.AddSlider(L, PHP, S,  20,   -spacing * 1.8, "hp_verticalWave")
    local slZoom1      = Sliders.AddSlider(R, PHP, S, -180,  -spacing * 1.8, "hp_zoom")

    -- Row 3: Alpha (L)
    local slAlpha1     = Sliders.AddSlider(L, PHP, S,  20,   -spacing * 2.8, "hp_alpha")

    -- Row 4: Colors
    local cpFill1      = Color.AddColorPicker(L, PHP, S,  20,   -spacing * 3.8, "fillColor")
    local cpEmpty1     = Color.AddColorPicker(R, PHP, S, -180,  -spacing * 3.8, "emptyColor")

    -- Row 5: Pulse
    local cbPulse1     = HPBarBox.AddEnablePulseCheckbox(L, PHP, S, 15, -spacing * 4.8)

    for _, w in ipairs({ tbCustomIcon, slIconSize, slSpacing1, slWave2, slZoom1, slAlpha1, cpFill1, cpEmpty1, cbPulse1 }) do
      if w then table.insert(allControls, w) end
    end
  end

  --------------------------------------------------------------------
  -- BarPanel: for style Custom Bar
  --------------------------------------------------------------------
  local BarPanel = CreateFrame("Frame", nil, parent)
  BarPanel:SetAllPoints(parent)

  do
    local L = NewAnchor(BarPanel, "TOPLEFT",  0, -30)
    local R = NewAnchor(BarPanel, "TOPRIGHT", 0, -30)

    -- Row 0: Texture dropdown (Bar only)
    local ddTexture    = barTexDD.AddBarTextureDropdown(R, PHP, S, -180, -spacing * 0, "barTexture")

    -- Row 1: Width (L) + Height (R)
    local slWidth      = Sliders.AddSlider(L, PHP, S,  20,   -spacing * 0.8, "hp_width")
    local slHeight     = Sliders.AddSlider(R, PHP, S, -180,  -spacing * 0.8, "hp_height")

    -- Row 2: Spacing (L) + Vertical Wave (R)
    local slSpacing2   = Sliders.AddSlider(L, PHP, S,  20,   -spacing * 1.8, "hp_iconSpacing")
    local slWave3      = Sliders.AddSlider(R, PHP, S, -180,  -spacing * 1.8, "hp_verticalWave")

    -- Row 3: Zoom (L) + Alpha (R)
    local slZoom2      = Sliders.AddSlider(L, PHP, S,  20,   -spacing * 2.8, "hp_zoom")
    local slAlpha2     = Sliders.AddSlider(R, PHP, S, -180,  -spacing * 2.8, "hp_alpha")

    -- Row 4: Colors
    local cpFill2      = Color.AddColorPicker(L, PHP, S,  20,   -spacing * 3.8, "fillColor")
    local cpEmpty2     = Color.AddColorPicker(R, PHP, S, -180,  -spacing * 3.8, "emptyColor")

    -- Row 5: Pulse
    local cbPulse2     = HPBarBox.AddEnablePulseCheckbox(L, PHP, S, 15, -spacing * 4.8)

    for _, w in ipairs({ ddTexture, slWidth, slHeight, slSpacing2, slWave3, slZoom2, slAlpha2, cpFill2, cpEmpty2, cbPulse2 }) do
      if w then table.insert(allControls, w) end
    end
  end

  --------------------------------------------------------------------
  -- Panel switching by style (internal keys kept for compatibility)
  --------------------------------------------------------------------
  local function ApplyStylePanels(style)
    style = tostring(style or S.style or "default"):lower()
    DefaultPanel:SetShown(style == "default")
    IconsPanel:SetShown(  style == "custom")
    BarPanel:SetShown(    style == "custom_bar")
    if PHP.UpdateHPTracker then pcall(PHP.UpdateHPTracker) end
  end

  -- Initial state + subscribe to style changes
  ApplyStylePanels(S.style)
  if HPBarDD and HPBarDD._RegisterStyleListener then
    HPBarDD._RegisterStyleListener(ApplyStylePanels)
  end

  --------------------------------------------------------------------
  -- Grey-out handling (Enable checkbox)
  --------------------------------------------------------------------
  local function UpdateControlsEnabled()
    local enabled = (S.enabled ~= false)
    GreyOutControls(enabled, allControls)
  end
  UpdateControlsEnabled()

  if enableCB and enableCB.SetScript then
    local oldOnClick = enableCB:GetScript("OnClick")
    enableCB:SetScript("OnClick", function(self, ...)
      if oldOnClick then oldOnClick(self, ...) end
      UpdateControlsEnabled()
      if PHP.UpdateHPTracker then pcall(PHP.UpdateHPTracker) end
    end)
  end

  --------------------------------------------------------------------
  -- PREVIEW HOOKS
  --------------------------------------------------------------------
  parent:HookScript("OnShow", function()
    SetHPBarPreviewFlag(true)   -- entering HP Bar subtab
  end)
  parent:HookScript("OnHide", function()
    SetHPBarPreviewFlag(false)  -- leaving HP Bar subtab
  end)

  return {
    enableCB     = enableCB,
    styleDD      = styleDD,
    DefaultPanel = DefaultPanel,
    IconsPanel   = IconsPanel,
    BarPanel     = BarPanel,
    controls     = allControls,
  }
end

return M