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

local RequireApi = INTERNAL_API.LIDPANELS_RequireApi_Impl
if type(RequireApi) ~= "function" then
  error("LidPanels missing shared editor page helpers.")
end

local RootDB = RequireApi("LIDPANELS_RootDB", _G.LIDPANELS_RootDB)
local ApplyFrameSkin = RequireApi("LIDPANELS_ApplyFrameSkin", _G.LIDPANELS_ApplyFrameSkin)
local MakeYellowSubHeader = RequireApi("LIDPANELS_MakeYellowSubHeader", _G.LIDPANELS_MakeYellowSubHeader)
local MakeSlider = RequireApi("LIDPANELS_MakeSlider", _G.LIDPANELS_MakeSlider)

local function TryUpdateMinimapButton()
  local updateFn = _G.LIDPANELS_UpdateMinimapButton
  if type(updateFn) == "function" then
    updateFn()
  end
end

local function BuildUIPage(editor, uiPage)
  if not editor or not uiPage then return end

  local uiNavBg = CreateFrame("Frame", nil, uiPage, "BackdropTemplate")
  uiNavBg:SetPoint("TOPLEFT", 0, -44)
  uiNavBg:SetPoint("BOTTOMLEFT", 0, 0)
  uiNavBg:SetWidth(240)
  ApplyFrameSkin(uiNavBg)
  editor.uiNavBg = uiNavBg

  local uiRightBg = CreateFrame("Frame", nil, uiPage, "BackdropTemplate")
  uiRightBg:SetPoint("TOPLEFT", uiNavBg, "TOPRIGHT", 12, 0)
  uiRightBg:SetPoint("BOTTOMRIGHT", 0, 0)
  ApplyFrameSkin(uiRightBg)
  uiRightBg:SetClipsChildren(true)
  editor.uiRightBg = uiRightBg

  local uiContent = CreateFrame("Frame", nil, uiRightBg)
  uiContent:SetPoint("TOPLEFT", 18, -12)
  uiContent:SetPoint("BOTTOMRIGHT", -18, 12)
  editor.uiContent = uiContent

  local uiTitle = uiContent:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  uiTitle:SetPoint("TOPLEFT", 0, 0)
  uiTitle:SetText("|cffffd100UI Settings|r")

  local uiHeaderGeneral = MakeYellowSubHeader(uiContent, "General", 0, -32)
  local uiHeaderMinimap = MakeYellowSubHeader(uiContent, "Minimap", 0, -92)
  editor.uiHeaderGeneral = uiHeaderGeneral
  editor.uiHeaderMinimap = uiHeaderMinimap

  local uiSkinLabel = uiContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  uiSkinLabel:SetPoint("TOPLEFT", 0, -58)
  uiSkinLabel:SetText("Editor Skin:")

  local uiSkinValue = uiContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  uiSkinValue:SetPoint("LEFT", uiSkinLabel, "RIGHT", 6, 0)
  uiSkinValue:SetText("LidPanels")
  editor.uiSkinValue = uiSkinValue

  local minimapSizeSlider = MakeSlider(uiContent, "Minimap Icon Size", 0, -118, 16, 64, 1, function(val)
    local ui = RootDB().ui
    ui.minimap = ui.minimap or { size = 28, angle = 45, alpha = 1, onlyOnHover = false }
    ui.minimap.size = math.floor((val or 28) + 0.5)
    TryUpdateMinimapButton()
  end)
  editor.minimapSizeSlider = minimapSizeSlider

  local minimapAlphaSlider = MakeSlider(uiContent, "Minimap Icon Visibility (%)", 0, -164, 0, 100, 1, function(val)
    local ui = RootDB().ui
    ui.minimap = ui.minimap or { size = 28, angle = 45, alpha = 1, onlyOnHover = false }
    ui.minimap.alpha = (tonumber(val) or 100) / 100
    TryUpdateMinimapButton()
  end)
  editor.minimapAlphaSlider = minimapAlphaSlider

  local minimapHoverCheck = CreateFrame("CheckButton", "LIDPANELS_MinimapHoverCheck", uiContent, "UICheckButtonTemplate")
  minimapHoverCheck:SetPoint("TOPLEFT", 0, -202)
  _G[minimapHoverCheck:GetName() .. "Text"]:SetText("Only show on mouseover")
  minimapHoverCheck:SetScript("OnClick", function(self)
    local ui = RootDB().ui
    ui.minimap = ui.minimap or { size = 28, angle = 45, alpha = 1, onlyOnHover = false }
    ui.minimap.onlyOnHover = self:GetChecked() and true or false
    TryUpdateMinimapButton()
  end)
  editor.minimapHoverCheck = minimapHoverCheck

  local minimapResetBtn = CreateFrame("Button", nil, uiContent, "UIPanelButtonTemplate")
  minimapResetBtn:SetSize(180, 22)
  minimapResetBtn:SetPoint("TOPLEFT", 0, -234)
  minimapResetBtn:SetText("Reset Minimap Icon")
  minimapResetBtn:SetScript("OnClick", function()
    StaticPopup_Show("LIDPANELS_RESET_MINIMAP")
  end)
  editor.minimapResetBtn = minimapResetBtn

  function editor:UpdateUILayout()
    local ui = RootDB().ui
    ui.skin = "LP"
    uiPage.__contentHeight = 520
    if self.UpdateScrollLayout then self:UpdateScrollLayout() end
  end

  if editor.UpdateUILayout then editor:UpdateUILayout() end
end

INTERNAL_API.LIDPANELS_BuildUIPage_Impl = BuildUIPage
