local addonName, ns = ...

------------------------------------------------------------
-- Copy popup (movable dialog for Ctrl+C)
------------------------------------------------------------

local copyFrame = CreateFrame("Frame", "HHCopyFrame", UIParent, "BackdropTemplate")
copyFrame:SetSize(420, 280)
copyFrame:SetPoint("CENTER")
copyFrame:SetFrameStrata("FULLSCREEN_DIALOG")
copyFrame:SetMovable(true)
copyFrame:EnableMouse(true)
copyFrame:RegisterForDrag("LeftButton")
copyFrame:SetScript("OnDragStart", copyFrame.StartMoving)
copyFrame:SetScript("OnDragStop", copyFrame.StopMovingOrSizing)
copyFrame:SetClampedToScreen(true)
copyFrame:Hide()

copyFrame:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    edgeSize = 14,
    insets = { left = 3, right = 3, top = 3, bottom = 3 },
})
copyFrame:SetBackdropColor(ns.Theme.BG.r, ns.Theme.BG.g, ns.Theme.BG.b, 0.97)
copyFrame:SetBackdropBorderColor(ns.Theme.BORDER:GetRGBA())
ns._copyFrame = copyFrame
ns.RegisterPopup(copyFrame)

local copyTitle = copyFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
copyTitle:SetPoint("TOP", 0, -10)
copyTitle:SetText("Copy Article Info  (Ctrl+C)")

local copyCloseBtn = ns.CreateThemedCloseButton(copyFrame)

local copyScroll = CreateFrame("ScrollFrame", "HHCopyScroll", copyFrame, "UIPanelScrollFrameTemplate")
copyScroll:SetPoint("TOPLEFT", 12, -30)
copyScroll:SetPoint("BOTTOMRIGHT", -30, 12)
-- Without this opt-in the template keeps its scrollbar -- arrows included
-- -- visible even at zero scroll range, so a one-line link showed dead
-- arrows. Set, the bar hides until an export is long enough to scroll.
copyScroll.scrollBarHideable = 1
ns._copyScroll = copyScroll

local copyBox = CreateFrame("EditBox", "HHCopyBox", copyScroll)
copyBox:SetMultiLine(true)
copyBox:SetAutoFocus(false)
copyBox:SetFontObject(ChatFontNormal)
copyBox:SetWidth(370)
copyBox:SetTextInsets(2, 2, 8, 8)
copyBox:SetScript("OnEscapePressed", function() copyFrame:Hide() end)
ns._copyBox = copyBox

copyScroll:SetScrollChild(copyBox)

-- The popup fits its text: an article link is one line, a shopping-list
-- export one line per entry. Chrome is the title strip, the bottom
-- padding and breathing room around the text; the old fixed height
-- becomes the cap, where the scroll frame takes over.
local CHROME_H = 62
local LINE_H = 14
local MAX_H = 280

function ns:ShowCopyPopup(text)
    local _, newlines = text:gsub("\n", "\n")
    copyFrame:SetHeight(math.min(MAX_H, CHROME_H + (1 + newlines) * LINE_H))
    copyBox:SetText(text)
    copyBox:HighlightText()
    ns.ShowPopup(copyFrame)
    copyBox:SetFocus()
end
