--[[--------------------------------------------------------------------
  PaladinHolyPower - ui/settings_panel/info_tab.lua
  Information Tab: About, Highlights, Quick Actions, Options, Support

  Changes:
    • Titles have no underline.
    • Big vertical + bottom dividers render BEHIND the tab content.
    • Bottom divider is inset (doesn't span full width).
    • "Hide Minimap Button" checkbox is centered under Options.
    • Discord popup opens OUTSIDE, top-center of the settings panel.
    • All title gradients now come from Theme.ApplyGradientText.
----------------------------------------------------------------------]]

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

local MinimapBox = MinimapBox or _G.MinimapBox

-- Media / utils
local MEDIA_BASE   = (PHP and PHP.MEDIA) or "Interface\\AddOns\\PaladinHolyPower\\media\\"
local DISCORD_ICON = MEDIA_BASE .. "icons\\assets\\chevron_discord.png"
local WHITE_TEX    = "Interface\\Buttons\\WHITE8x8"
local PixelUtil    = _G.PixelUtil

local M = {}

----------------------------------------------------------------------
-- Palette
----------------------------------------------------------------------
local COL_WHITE = {1, 1, 1, 1}
local COL_MUTED = {0.85, 0.85, 0.85, 1}
local ACCENT    = PHP.ACCENT_COLOR or { 0.95, 0.90, 0.55, 0.20 }

-- Holy Power brand color (default #F2E699)
local HP_RGBA = (PHP.COLORS and (PHP.COLORS.HolyPower or PHP.COLORS.HP))
             or PHP.HP_COLOR or { 242/255, 230/255, 153/255, 1 }

local function rgba_to_hex(r, g, b)
  r = math.min(255, math.floor((r or 1) * 255 + 0.5))
  g = math.min(255, math.floor((g or 1) * 255 + 0.5))
  b = math.min(255, math.floor((b or 1) * 255 + 0.5))
  return string.format("%02x%02x%02x", r, g, b)
end
local HP_HEX = rgba_to_hex(HP_RGBA[1], HP_RGBA[2], HP_RGBA[3])

----------------------------------------------------------------------
-- Divider helpers (rendered on a frame *behind* content)
----------------------------------------------------------------------
local function DIV_THICK()
  -- Prefer Theme API if present
  if PHP.Theme and PHP.Theme.DividerThickness then
    return PHP.Theme.DividerThickness()
  end
  return 1
end

local function NewLineTex(parentFrameForStrata, holder)
  -- Create a holder frame once that sits *behind* the parent content
  if not holder._behind then
    local f = CreateFrame("Frame", nil, parentFrameForStrata)
    f:SetAllPoints(parentFrameForStrata)
    f:SetFrameStrata(parentFrameForStrata:GetFrameStrata())
    local lvl = (parentFrameForStrata:GetFrameLevel() or 1) - 1
    f:SetFrameLevel(lvl < 0 and 0 or lvl) -- ensure behind
    holder._behind = f
  end
  local t = holder._behind:CreateTexture(nil, "BACKGROUND")
  t:SetTexture(WHITE_TEX)
  t:SetColorTexture(ACCENT[1] or 1, ACCENT[2] or 1, ACCENT[3] or 1, ACCENT[4] or 0.2)
  if t.SetSnapToPixelGrid then t:SetSnapToPixelGrid(true); t:SetTexelSnappingBias(0) end
  return t
end

local function Recolor(line)
  if not line then return end
  line:SetColorTexture(ACCENT[1] or 1, ACCENT[2] or 1, ACCENT[3] or 1, ACCENT[4] or 0.2)
end

local function DrawHInset(line, frame, yFromTop, insetL, insetR)
  line:ClearAllPoints()
  insetL = insetL or 0
  insetR = insetR or 0
  if PixelUtil and PixelUtil.SetPoint then
    PixelUtil.SetPoint(line, "TOPLEFT",  frame, "TOPLEFT",  insetL, -yFromTop)
    PixelUtil.SetPoint(line, "TOPRIGHT", frame, "TOPRIGHT", -insetR, -yFromTop)
    PixelUtil.SetHeight(line, DIV_THICK())
  else
    line:SetPoint("TOPLEFT",  frame, "TOPLEFT",  insetL, -yFromTop)
    line:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -insetR, -yFromTop)
    line:SetHeight(DIV_THICK())
  end
end

local function DrawV(line, frame, xFromLeft, yTop, yBottom)
  line:ClearAllPoints()
  if PixelUtil and PixelUtil.SetPoint then
    PixelUtil.SetPoint(line, "TOPLEFT",    frame, "TOPLEFT", xFromLeft, -yTop)
    PixelUtil.SetPoint(line, "BOTTOMLEFT", frame, "TOPLEFT", xFromLeft, -yBottom)
    PixelUtil.SetWidth(line, DIV_THICK())
  else
    line:SetPoint("TOPLEFT",    frame, "TOPLEFT", xFromLeft, -yTop)
    line:SetPoint("BOTTOMLEFT", frame, "TOPLEFT", xFromLeft, -yBottom)
    line:SetWidth(DIV_THICK())
  end
end

----------------------------------------------------------------------
-- Titles (no underline) + text helpers
-- (Gradients provided by Theme.ApplyGradientText)
----------------------------------------------------------------------

-- Centered section title (NO underline)
local function SectionTitle(column, text, prev, yGap)
  local fs = column:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  fs:SetJustifyH("CENTER")
  if prev then
    fs:SetPoint("TOP", prev, "BOTTOM", 0, (yGap or -12))
  else
    fs:SetPoint("TOP", column, "TOP", 0, (yGap or 0))
  end

  if PHP and PHP.Theme and PHP.Theme.ApplyGradientText then
    -- Use Theme gradient (left/right endpoints from Theme)
    PHP.Theme.ApplyGradientText(fs, text, nil, nil, { outline = true })
  else
    fs:SetText(text)
  end

  -- keep vertex white (Theme.ApplyGradientText already neutralizes tint, this is safe)
  fs:SetTextColor(unpack(COL_WHITE))
  return fs
end

-- Centered paragraph
local function Paragraph(column, prev, text, yGap, color, pad)
  local fs = column:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  fs:SetJustifyH("CENTER")
  if prev then
    fs:SetPoint("TOP", prev, "BOTTOM", 0, (yGap or -8))
  else
    fs:SetPoint("TOP", column, "TOP", 0, 0)
  end
  fs:SetText(text)
  fs:SetWordWrap(true)
  if color then fs:SetTextColor(unpack(color)) end
  pad = pad or 20
  local function fit()
    if column and column.GetWidth then
      fs:SetWidth(math.max(10, (column:GetWidth() or 0) - pad))
    end
  end
  fit()
  column:HookScript("OnSizeChanged", fit)
  return fs
end

-- Centered bullet line using "• "
local function BulletLine(column, prev, text, yGap)
  return Paragraph(column, prev, "|cff"..HP_HEX.."•|r |cffffffff"..text.."|r", yGap)
end

-- Natural-size styled button centered under prev
local function CenteredButton(column, prev, label, onClick, yGap)
  local btn = PHP.CreateStyledButton(column, label, 0, 0)
  btn:ClearAllPoints()
  if prev then
    btn:SetPoint("TOP", prev, "BOTTOM", 0, (yGap or -10))
  else
    btn:SetPoint("TOP", column, "TOP", 0, (yGap or 0))
  end
  btn:SetScript("OnClick", onClick)
  return btn
end

----------------------------------------------------------------------
-- Discord popup (OUTSIDE the panel, top-center)
----------------------------------------------------------------------
local function EnsureDiscordPopup()
  local panel = PHP.SettingsPanel or UIParent
  local f = PHP._InfoDiscordPopup

  if not f then
    f = CreateFrame("Frame", "PHP_Info_DiscordPopup", UIParent, "BackdropTemplate")
    f:SetSize(420, 120)
    f:SetFrameStrata("DIALOG")

    f:SetBackdrop({ bgFile = (PHP.Theme and PHP.Theme.Texture and PHP.Theme.Texture())
                              or (MEDIA_BASE .. "icons\\background\\panel_1.png") })
    f:SetBackdropColor(0.98, 0.98, 0.99, 0.95)

    local title = f:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    title:SetPoint("TOP", f, "TOP", 0, -12)
    title:SetJustifyH("CENTER")
    if PHP and PHP.Theme and PHP.Theme.ApplyGradientText then
      PHP.Theme.ApplyGradientText(title, "Discord", nil, nil, { outline = true })
    else
      title:SetText("Discord")
    end

    local close = CreateFrame("Button", nil, f, "BackdropTemplate")
    close:SetSize(14, 14)
    close:SetPoint("TOPRIGHT", f, "TOPRIGHT", -8, -8)
    local l1 = close:CreateTexture(nil, "OVERLAY"); l1:SetTexture(WHITE_TEX); l1:SetSize(2,14); l1:SetPoint("CENTER"); l1:SetRotation(math.rad(45))
    local l2 = close:CreateTexture(nil, "OVERLAY"); l2:SetTexture(WHITE_TEX); l2:SetSize(2,14); l2:SetPoint("CENTER"); l2:SetRotation(math.rad(-45))
    l1:SetVertexColor(1,1,1,1); l2:SetVertexColor(1,1,1,1)
    close:SetScript("OnEnter", function() l1:SetVertexColor(.85,.85,.85); l2:SetVertexColor(.85,.85,.85) end)
    close:SetScript("OnLeave", function() l1:SetVertexColor(1,1,1,1);     l2:SetVertexColor(1,1,1,1)     end)
    close:SetScript("OnClick", function() f:Hide() end)

    local url = "https://discord.com/users/fendoran"
    local box = CreateFrame("EditBox", nil, f, "BackdropTemplate")
    box:SetPoint("TOPLEFT", f, "TOPLEFT", 14, -48)
    box:SetPoint("RIGHT",   f, "RIGHT",   -14, 0)
    box:SetHeight(26)
    box:SetAutoFocus(false)
    box:SetJustifyH("LEFT")
    box:SetFontObject("GameFontHighlight")
    box:SetTextInsets(8, 8, 0, 0)
    box:SetBackdrop({ bgFile = WHITE_TEX, edgeFile = WHITE_TEX, edgeSize = 1 })
    box:SetBackdropColor(0.12, 0.12, 0.12, 1)
    box:SetBackdropBorderColor(0, 0, 0, 1)
    box:SetText(url)
    box._const = url
    box:SetScript("OnEditFocusGained", function(self) self:HighlightText() end)
    box:SetScript("OnMouseUp",        function(self) self:SetFocus(); self:HighlightText() end)
    box:SetScript("OnEscapePressed",  function(self) self:ClearFocus() end)
    box:SetScript("OnEnterPressed",   function(self) self:ClearFocus() end)
    box:SetScript("OnTextChanged",    function(self, user)
      if user and self:GetText() ~= self._const then
        self:SetText(self._const); self:HighlightText()
      end
    end)

    PHP._InfoDiscordPopup = f
  end

  -- Anchor OUTSIDE, above the panel
  f:ClearAllPoints()
  f:SetPoint("BOTTOM", panel, "TOP", 0, 12)
  return f
end

----------------------------------------------------------------------
-- Build
----------------------------------------------------------------------
function M.Build(parent, PHP, S)
  local P = 18
  local topPad = P + 8
  local footerPad = P
  local FOOTER_BAR = 28

  -- Dividers created on a holder frame *behind* the content
  parent._infoLines = parent._infoLines or {}
  local L = parent._infoLines
  L.footer = L.footer or NewLineTex(parent, parent._infoLines)
  L.center = L.center or NewLineTex(parent, parent._infoLines)

  local function RedrawLines()
    Recolor(L.footer); Recolor(L.center)
    local h = parent:GetHeight() or 0
    local w = parent:GetWidth() or 0

    -- Bottom divider: inset so it doesn't span full width
    local footerY = h - (footerPad + FOOTER_BAR)
    DrawHInset(L.footer, parent, footerY, P + 6, P + 6)

    -- Center vertical seam: from topPad to just above bottom divider
    DrawV(L.center, parent, w * 0.5, topPad, footerY)
  end
  RedrawLines()
  parent:HookScript("OnSizeChanged", RedrawLines)

  -- Footer anchor
  local footerAnchor = CreateFrame("Frame", nil, parent)
  footerAnchor:SetPoint("LEFT",  parent, "LEFT",  P, 0)
  footerAnchor:SetPoint("RIGHT", parent, "RIGHT", -P, 0)
  footerAnchor:SetPoint("BOTTOM", parent, "BOTTOM", 0, footerPad + FOOTER_BAR)
  footerAnchor:SetHeight(0)

  -- Columns
  local left = CreateFrame("Frame", nil, parent)
  left:SetPoint("TOPLEFT", parent, "TOPLEFT", P, -topPad)
  left:SetPoint("RIGHT", parent, "CENTER", -12, 0)
  left:SetPoint("BOTTOM", footerAnchor, "TOP", 0, 0)

  local right = CreateFrame("Frame", nil, parent)
  right:SetPoint("TOPRIGHT", parent, "TOPRIGHT", -P, -topPad)
  right:SetPoint("LEFT", parent, "CENTER", 12, 0)
  right:SetPoint("BOTTOM", footerAnchor, "TOP", 0, 0)

  --------------------------------------------------------------------
  -- LEFT: About, Highlights (single bullet), Support
  --------------------------------------------------------------------
  local about     = SectionTitle(left, "About", nil, 0)
  local tagline   = Paragraph(left, about,
    "A modern Holy Power tracker and log, built to be fast, clean, and customizable.",
    -10, COL_MUTED, 28)

  local highlights = SectionTitle(left, "Highlights", tagline, -14)
  local feat       = BulletLine(left, highlights,
    "Optional |cff"..HP_HEX.."Masque|r + |cff"..HP_HEX.."SharedMedia|r integration.", -8)

  local support    = SectionTitle(left, "Support", feat, -16)

  -- Centered support row (discord icon + clickable name)
  local supportRow = CreateFrame("Frame", nil, left)
  supportRow:SetPoint("TOP", support, "BOTTOM", 0, -10)

  local nameBtn = CreateFrame("Button", nil, supportRow)
  local nameFS  = nameBtn:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  nameFS:SetText("|cffffffffFendoran|r")
  nameBtn:SetFontString(nameFS)
  nameBtn:SetSize(nameFS:GetStringWidth(), nameFS:GetStringHeight())

  local dIcon = supportRow:CreateTexture(nil, "ARTWORK")
  dIcon:SetSize(18, 18)
  dIcon:SetTexture(DISCORD_ICON)
  dIcon:SetVertexColor(1, 1, 1, 1)

  local pairWidth = 18 + 8 + nameBtn:GetWidth()
  supportRow:SetSize(pairWidth, 22)
  dIcon:SetPoint("LEFT", supportRow, "LEFT", 0, 0)
  nameBtn:SetPoint("LEFT", dIcon, "RIGHT", 8, 0)
  nameBtn:SetScript("OnClick", function()
    local pop = EnsureDiscordPopup()
    pop:Show()
  end)

  --------------------------------------------------------------------
  -- RIGHT: Quick Actions + Options (Minimap Button centered)
  --------------------------------------------------------------------
  local qTitle = SectionTitle(right, "Quick Actions", nil, 0)

  local btn1 = CenteredButton(right, qTitle, "/php", function()
    ChatFrame_OpenChat("/php ")
  end, -10)

  local btn2 = CenteredButton(right, btn1, "/php log", function()
    ChatFrame_OpenChat("/php log ")
  end, -8)

  local optTitle = SectionTitle(right, "Options", btn2, -16)

  -- Container anchored ONLY under Options; centered; natural height
  local minimapContainer = CreateFrame("Frame", nil, right)
  minimapContainer:SetPoint("TOP", optTitle, "BOTTOM", 0, -10)
  minimapContainer:SetPoint("CENTER", right, "CENTER", 0, 0)
  minimapContainer:SetWidth(320)
  minimapContainer:SetHeight(26)

  if MinimapBox and MinimapBox.AddMinimapCheckbox then
    local cb = MinimapBox.AddMinimapCheckbox(minimapContainer, PHP, S, 0, 0)
    if cb and cb.SetPoint then
      cb:ClearAllPoints()
      cb:SetPoint("CENTER", minimapContainer, "CENTER", 0, 0)
      if cb.Text and cb.Text.SetJustifyH then
        cb.Text:SetJustifyH("CENTER")
      end
    end
  end

  --------------------------------------------------------------------
  -- Footer credits
  --------------------------------------------------------------------
  local ver = (GetAddOnMetadata and GetAddOnMetadata(PHP.ADDON_NAME or "PaladinHolyPower", "Version"))
           or PHP.version or "2.0.0"

  local credits = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  credits:SetPoint("BOTTOM", parent, "BOTTOM", 0, footerPad)
  credits:SetJustifyH("CENTER")
  credits:SetTextColor(unpack(COL_MUTED))
  credits:SetText("Created by Fendoran  •  version " .. tostring(ver))
end

InfoTab = M
_G.InfoTab = M
return M