--[[--------------------------------------------------------------------
  PaladinHolyPower – ui/settings_panel/tabs/hp_log_subtab.lua
  Holy Power Log (HPL) Settings Subtab

  Notes:
  • Writes HPLFont, HPL_iconSize, HPL_textSize, HPL_zoom, HPL_alpha to S.
  • Uses Fonts.AddModeDropdown(...) for Generated / Spent / Wasted (S.mode).

  • PREVIEW HOOKS (mirror of HP Bar):
      - OnShow  : PHP._ActiveHPSubTab = "hplog";  PHP._HPL_ApplyVisibility() + PHP.UpdateHPLTracker()
      - OnHide  : PHP._ActiveHPSubTab = nil;      PHP._HPL_ApplyVisibility() + PHP.UpdateHPLTracker()
----------------------------------------------------------------------]]

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

local Sliders = Sliders or _G.Sliders
local Color   = Color   or _G.Color
local Fonts   = Fonts   or _G.Fonts
local LockBox = LockBox or _G.LockBox

local M = {}

-- Subtab preview flag management for HP Log
local function SetHPLPreviewFlag(active)
  if active then PHP._ActiveHPSubTab = "hplog"
  else PHP._ActiveHPSubTab = nil end
  if PHP._HPL_ApplyVisibility then pcall(PHP._HPL_ApplyVisibility) end
  if PHP.UpdateHPLTracker     then pcall(PHP.UpdateHPLTracker)     end
end

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

  -- Ensure default mode
  if type(S.mode) ~= "string" or (S.mode ~= "wasted" and S.mode ~= "generated" and S.mode ~= "spent") then
    S.mode = "wasted"
  end

  local LeftAnchor = CreateFrame("Frame", nil, parent)
  LeftAnchor:SetSize(1, 1)
  LeftAnchor:SetPoint("TOPLEFT", parent, "TOPLEFT", 0, -30)

  local RightAnchor = CreateFrame("Frame", nil, parent)
  RightAnchor:SetSize(1, 1)
  RightAnchor:SetPoint("TOPRIGHT", parent, "TOPRIGHT", 0, -30)

  -- === Enable/Disable checkbox at top ===
  local enableCB = LockBox.AddEnableHPLCheckbox(RightAnchor, PHP, S, -100, 55)

  local controls = {}

  controls[#controls + 1] = Sliders.AddSlider(LeftAnchor,  PHP, S, 20,    -spacing * 0.8, "HPL_iconSize")
  controls[#controls + 1] = Sliders.AddSlider(RightAnchor, PHP, S, -180, -spacing * 0.8, "HPL_textSize")
  controls[#controls + 1] = Sliders.AddSlider(LeftAnchor,  PHP, S, 20,    -spacing * 1.8, "HPL_zoom")
  controls[#controls + 1] = Sliders.AddSlider(RightAnchor, PHP, S, -180, -spacing * 1.8, "HPL_alpha")

  controls[#controls + 1] = Fonts.AddModeDropdown(LeftAnchor,  PHP, S, 20, -spacing * 0, "mode")
  controls[#controls + 1] = Fonts.AddFontDropdown(RightAnchor, PHP, S, -180, -spacing * 0, "HPLFont")
  
  controls[#controls + 1] = Color.AddColorPicker(LeftAnchor,  PHP, S, 20, -spacing * 2.8, "valueColor")

  local function UpdateControlsEnabled()
    local enabled = S.enabled ~= false
    for _, control in ipairs(controls) 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

  -- Initial update
  UpdateControlsEnabled()

  -- Hook checkbox click to update control states dynamically
  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._HPL_ApplyVisibility then pcall(PHP._HPL_ApplyVisibility) end
      if PHP.UpdateHPLTracker     then pcall(PHP.UpdateHPLTracker)     end
    end)
  end

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

-- Keep original global for compatibility; add HPL alias as well
WastedHPSubTab = M
HPLSubTab = M
_G.WastedHPSubTab = M
_G.HPLSubTab = M

return M