--[[
    UI/ShareWindow.lua

    A copyable text box, and the reason this addon needs one.

    The catalogue will never be complete. A zone released last week has no wiki
    page and no published coordinates, so for those creatures the player who
    walked past one is the only survey that exists -- and this addon has been
    quietly recording exactly that: creature id, name, family, classification,
    the map and the spot they were standing on.

    None of it could leave their machine. The addon never contacts a website,
    by design, so the only way that field work reaches the shipped data is if
    the player can select it and paste it somewhere themselves.

    So this window does one thing: shows the text, selects it, and gets out of
    the way. No submit button, no endpoint, nothing sent anywhere.
]]

local _, ns = ...

ns.UI = ns.UI or {}

local ShareWindow = ns.NewModule("ShareWindow")
ns.UI.ShareWindow = ShareWindow

local frame

local function Build()
    local Theme = ns.UI.Theme

    frame = CreateFrame("Frame", "ShinyHunterShareFrame", UIParent)
    frame:SetSize(560, 420)
    frame:SetPoint("CENTER")
    frame:SetFrameStrata("DIALOG")
    frame:SetMovable(true)
    frame:EnableMouse(true)
    frame:RegisterForDrag("LeftButton")
    frame:SetScript("OnDragStart", frame.StartMoving)
    frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
    frame:Hide()

    local backdrop = Theme:CreatePanel(frame)
    backdrop:SetAllPoints()

    frame.Title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    frame.Title:SetPoint("TOPLEFT", 18, -16)
    frame.Title:SetTextColor(1.00, 0.82, 0.20)

    frame.Intro = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    frame.Intro:SetPoint("TOPLEFT", frame.Title, "BOTTOMLEFT", 0, -6)
    frame.Intro:SetPoint("RIGHT", frame, "RIGHT", -18, 0)
    frame.Intro:SetJustifyH("LEFT")
    frame.Intro:SetSpacing(2)

    local close = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", -4, -4)

    local scroll = CreateFrame("ScrollFrame", "ShinyHunterShareScroll", frame,
        "UIPanelScrollFrameTemplate")
    scroll:SetPoint("TOPLEFT", 18, -78)
    scroll:SetPoint("BOTTOMRIGHT", -34, 44)

    -- Multi-line, and never editable in spirit: the player may type into it,
    -- but nothing here is read back. It is a box that holds text still so it
    -- can be selected.
    local box = CreateFrame("EditBox", nil, scroll)
    box:SetMultiLine(true)
    box:SetAutoFocus(false)
    box:SetFontObject(ChatFontNormal)
    box:SetWidth(490)
    box:SetScript("OnEscapePressed", function() frame:Hide() end)
    scroll:SetScrollChild(box)
    frame.Box = box

    frame.Hint = frame:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
    frame.Hint:SetPoint("BOTTOMLEFT", 18, 18)
    frame.Hint:SetPoint("RIGHT", frame, "RIGHT", -18, 0)
    frame.Hint:SetJustifyH("LEFT")

    tinsert(UISpecialFrames, "ShinyHunterShareFrame")
    return frame
end

--- Shows the window with `text` selected and ready to copy.
function ShareWindow:Show(text, intro)
    if not frame then Build() end

    frame.Title:SetText(ns.L["SHARE_TITLE"])
    frame.Intro:SetText(intro or ns.L["SHARE_INTRO"])
    frame.Hint:SetText(ns.L["SHARE_HINT"])
    frame.Box:SetText(text or "")
    frame:Show()

    -- Selected on open, because the whole point is one keypress to copy.
    frame.Box:SetFocus()
    frame.Box:HighlightText()
end

function ShareWindow:Hide()
    if frame then frame:Hide() end
end
