local ADDON_NAME, ns = ...

local CopyDialog = {}
ns.CopyDialog = CopyDialog

local frame

local HINTS = {
    macro = "Press Ctrl+C to copy, then paste into a macro.",
    url = "Press Ctrl+C to copy, then paste into your browser.",
}

local function Build()
    frame = CreateFrame("Frame", "RatedTrackerFieldManualCopyFrame", UIParent, "BackdropTemplate")
    frame:SetSize(460, 260)
    frame:SetPoint("CENTER")
    frame:SetFrameStrata("FULLSCREEN_DIALOG")
    frame:SetMovable(true)
    frame:EnableMouse(true)
    frame:RegisterForDrag("LeftButton")
    frame:SetScript("OnDragStart", frame.StartMoving)
    frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
    frame:SetBackdrop({
        bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background-Dark",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        tile = true, tileSize = 32, edgeSize = 16,
        insets = { left = 4, right = 4, top = 4, bottom = 4 },
    })
    frame:SetBackdropColor(0.1, 0.1, 0.1, 0.97)
    frame:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)

    local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    title:SetPoint("TOP", frame, "TOP", 0, -16)
    title:SetText(ns.GREEN .. "Copy Macro" .. ns.RESET)
    frame.title = title

    local hint = frame:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
    hint:SetPoint("TOP", title, "BOTTOM", 0, -4)
    hint:SetText(HINTS.macro)
    frame.hint = hint

    local close = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -4, -4)
    close:SetScript("OnClick", function() frame:Hide() end)

    local scroll = CreateFrame("ScrollFrame", "RatedTrackerFieldManualCopyScroll", frame, "UIPanelScrollFrameTemplate")
    scroll:SetPoint("TOPLEFT", frame, "TOPLEFT", 16, -52)
    scroll:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -32, 44)

    local edit = CreateFrame("EditBox", nil, scroll)
    edit:SetMultiLine(true)
    edit:SetFontObject("ChatFontNormal")
    edit:SetWidth(390)
    edit:SetAutoFocus(false)
    edit:SetScript("OnEscapePressed", function() frame:Hide() end)
    edit:SetScript("OnEditFocusGained", function(self) self:HighlightText() end)
    scroll:SetScrollChild(edit)
    frame.edit = edit

    local selectBtn = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
    selectBtn:SetSize(120, 22)
    selectBtn:SetPoint("BOTTOM", frame, "BOTTOM", 0, 14)
    selectBtn:SetText("Select All")
    selectBtn:SetScript("OnClick", function()
        edit:SetFocus()
        edit:HighlightText()
    end)

    tinsert(UISpecialFrames, "RatedTrackerFieldManualCopyFrame")
end

-- mode: "macro" (default) or "url" - changes the paste hint.
function CopyDialog:Open(titleText, text, mode)
    if not frame then Build() end
    local isUrl = mode == "url"
    frame.title:SetText(ns.GREEN .. (titleText or (isUrl and "Copy Link" or "Copy Macro")) .. ns.RESET)
    frame.hint:SetText(HINTS[mode] or HINTS.macro)
    frame.edit:SetText(text or "")
    frame:Show()
    frame.edit:SetFocus()
    frame.edit:HighlightText()
end
