local API = rawget(_G, "LIDCOUNCIL_API")
local INTERNAL_API = type(API) == "table" and type(API.__internal) == "table" and API.__internal or nil
if type(INTERNAL_API) ~= "table" then
  error("LidCouncil missing internal API registry.")
end

local RequireApi = INTERNAL_API.LIDCOUNCIL_RequireApi_Impl
local CreatePanelsPageScaffold = INTERNAL_API.LIDCOUNCIL_CreatePanelsPageScaffold_Impl
local CreatePanelBackgroundSection = INTERNAL_API.LIDCOUNCIL_CreatePanelBackgroundSection_Impl
local CreatePanelDesignSection = INTERNAL_API.LIDCOUNCIL_CreatePanelDesignSection_Impl
local BuildPanelDiagnosticsSection = INTERNAL_API.LIDCOUNCIL_BuildPanelDiagnosticsSection_Impl
if type(RequireApi) ~= "function" then
  error("LidCouncil missing shared editor page helpers.")
end
if type(CreatePanelsPageScaffold) ~= "function" then
  error("LidCouncil missing panels page scaffold builder.")
end
if type(CreatePanelBackgroundSection) ~= "function" then
  error("LidCouncil missing panel background section builder.")
end
if type(CreatePanelDesignSection) ~= "function" then
  error("LidCouncil missing panel design section builder.")
end
if type(BuildPanelDiagnosticsSection) ~= "function" then
  error("LidCouncil missing panel diagnostics section builder.")
end

local DB = RequireApi("LIDCOUNCIL_DB", _G.LIDCOUNCIL_DB)
local MakeSlider = RequireApi("LIDCOUNCIL_MakeSlider", _G.LIDCOUNCIL_MakeSlider)
local RebuildAllPanels = RequireApi("LIDCOUNCIL_RebuildAllPanels", _G.LIDCOUNCIL_RebuildAllPanels)
local ResetPanelToCenterDefaultSize = RequireApi("LIDCOUNCIL_ResetPanelToCenterDefaultSize", _G.LIDCOUNCIL_ResetPanelToCenterDefaultSize)
local GetSelectedPanelName = RequireApi("LIDCOUNCIL_GetSelectedPanelName", _G.LIDCOUNCIL_GetSelectedPanelName)

local function BuildPanelsPage(editor, panelsPage)
  if not editor or not panelsPage then return end
-- ============================================================
-- ##----------------------PANELS PAGE--------------------------
-- ============================================================

local scaffold = CreatePanelsPageScaffold(editor, panelsPage)
local props = scaffold and scaffold.props
if not props then
  error("LidCouncil panels page scaffold failed to create props container.")
end

-- Sliders for Size + X/Y
local wSlider = MakeSlider(props, "Width", 0, -248, 10, 2200, 1, function(val)
  if not GetSelectedPanelName() then return end
  local p = DB().panels[GetSelectedPanelName()]; if not p then return end
  p.w = math.floor(val + 0.5)
  RebuildAllPanels()
  if editor and editor.RefreshPanelPropertiesSection then editor:RefreshPanelPropertiesSection() elseif editor and editor.RefreshPanelsSection then editor:RefreshPanelsSection() elseif editor and editor.Refresh then editor:Refresh() end
end)
editor.wSlider = wSlider

local hSlider = MakeSlider(props, "Height", 0, -294, 10, 1200, 1, function(val)
  if not GetSelectedPanelName() then return end
  local p = DB().panels[GetSelectedPanelName()]; if not p then return end
  p.h = math.floor(val + 0.5)
  RebuildAllPanels()
  if editor and editor.RefreshPanelPropertiesSection then editor:RefreshPanelPropertiesSection() elseif editor and editor.RefreshPanelsSection then editor:RefreshPanelsSection() elseif editor and editor.Refresh then editor:Refresh() end
end)
editor.hSlider = hSlider

local xSlider = MakeSlider(props, "Position X", 0, -340, -4000, 4000, 1, function(val)
  if not GetSelectedPanelName() then return end
  local db = DB()
  db.positions[GetSelectedPanelName()] = db.positions[GetSelectedPanelName()] or {}
  local pos = db.positions[GetSelectedPanelName()]
  pos.point, pos.relativePoint = "CENTER", "CENTER"
  pos.x = math.floor(val + 0.5)
  pos.y = pos.y or 0
  RebuildAllPanels()
end)
editor.xSlider = xSlider

local ySlider = MakeSlider(props, "Position Y", 0, -386, -4000, 4000, 1, function(val)
  if not GetSelectedPanelName() then return end
  local db = DB()
  db.positions[GetSelectedPanelName()] = db.positions[GetSelectedPanelName()] or {}
  local pos = db.positions[GetSelectedPanelName()]
  pos.point, pos.relativePoint = "CENTER", "CENTER"
  pos.y = math.floor(val + 0.5)
  pos.x = pos.x or 0
  RebuildAllPanels()
end)
editor.ySlider = ySlider

local panelBackgroundSection = CreatePanelBackgroundSection(editor, props)
local GetColorMode = panelBackgroundSection and panelBackgroundSection.GetColorMode
local BG_CHOICES = panelBackgroundSection and panelBackgroundSection.bgOptions
local BuildCustomBackgroundOptions = panelBackgroundSection and panelBackgroundSection.BuildCustomBackgroundOptions
local CopyTexCoord = panelBackgroundSection and panelBackgroundSection.CopyTexCoord
local SameTexCoord = panelBackgroundSection and panelBackgroundSection.SameTexCoord
local FindTalentOptionForPanel = panelBackgroundSection and panelBackgroundSection.FindTalentOptionForPanel
local UpdateBgPreview = panelBackgroundSection and panelBackgroundSection.UpdateBgPreview
if type(GetColorMode) ~= "function"
  or type(BG_CHOICES) ~= "table"
  or type(CopyTexCoord) ~= "function"
  or type(SameTexCoord) ~= "function"
  or type(FindTalentOptionForPanel) ~= "function"
  or type(UpdateBgPreview) ~= "function" then
  error("LidCouncil panel background section returned invalid helpers.")
end

local panelDesignSection = CreatePanelDesignSection(editor, props, {
  GetColorMode = GetColorMode,
  bgOptions = BG_CHOICES,
  BuildCustomBackgroundOptions = BuildCustomBackgroundOptions,
  CopyTexCoord = CopyTexCoord,
  SameTexCoord = SameTexCoord,
  FindTalentOptionForPanel = FindTalentOptionForPanel,
  UpdateBgPreview = UpdateBgPreview,
})
local EnsurePanelExtraData = panelDesignSection and panelDesignSection.EnsurePanelExtraData
local PANEL_STAT_CHOICES = panelDesignSection and panelDesignSection.panelStatChoices
local PANEL_SPECIAL_FONT_CHOICES = panelDesignSection and panelDesignSection.panelSpecialFontChoices
if type(EnsurePanelExtraData) ~= "function"
  or type(PANEL_STAT_CHOICES) ~= "table"
  or type(PANEL_SPECIAL_FONT_CHOICES) ~= "table" then
  error("LidCouncil panel design section returned invalid helpers.")
end

BuildPanelDiagnosticsSection(editor, props, {
  EnsurePanelExtraData = EnsurePanelExtraData,
  panelStatChoices = PANEL_STAT_CHOICES,
  panelSpecialFontChoices = PANEL_SPECIAL_FONT_CHOICES,
})

-- Reset Panel button (center + default size only)
local resetPanelBtn = CreateFrame("Button", nil, props, "UIPanelButtonTemplate")
resetPanelBtn:SetSize(160, 22)
local resetDesc = props:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
resetDesc:SetPoint("TOPLEFT", 0, -770)
resetDesc:SetWidth(520)
resetDesc:SetJustifyH("LEFT")
resetDesc:SetText("When accidently setting the size to 0, the panel can become invisible. It makes you feel that you lost your panel? Pressing the button \"Reset panel\" will bring back the panel into the center of your screen in a certain size. :)")
editor.resetDesc = resetDesc

resetPanelBtn:SetPoint("TOPLEFT", 0, -830)
resetPanelBtn:SetText("Reset Panel Size & Position")
resetPanelBtn:SetScript("OnClick", function()
  if not GetSelectedPanelName() then return end
  ResetPanelToCenterDefaultSize(GetSelectedPanelName())
  if editor and editor.RefreshPanelPropertiesSection then editor:RefreshPanelPropertiesSection() elseif editor and editor.RefreshPanelsSection then editor:RefreshPanelsSection() elseif editor and editor.Refresh then editor:Refresh() end
end)
editor.resetPanelBtn = resetPanelBtn

function editor:UpdatePanelsPropsHeight()
  if not self.props or not self.propsScroll then return end
  local viewH = (self.propsScroll.GetHeight and self.propsScroll:GetHeight()) or 1
  local minH = math.max(1, math.floor(viewH))
  local target = self.resetPanelBtn or self.resetDesc or self.resetHeader
  if not (target and target.IsShown and target:IsShown()) then
    self.props:SetHeight(minH)
    if self.propsScroll.UpdateScrollChildRect then self.propsScroll:UpdateScrollChildRect() end
    return
  end

  local propsTop = self.props:GetTop()
  local targetBottom = target:GetBottom()
  if not propsTop or not targetBottom then
    self.props:SetHeight(minH)
    if self.propsScroll.UpdateScrollChildRect then self.propsScroll:UpdateScrollChildRect() end
    return
  end

  local padding = 36
  local height = math.ceil((propsTop - targetBottom) + padding)
  if height < minH then height = minH end
  self.props:SetHeight(height)
  if self.propsScroll.UpdateScrollChildRect then self.propsScroll:UpdateScrollChildRect() end
end
end

INTERNAL_API.LIDCOUNCIL_BuildPanelsPage_Impl = BuildPanelsPage


