---@class AddonPrivate
local Private = select(2, ...)

---@class WhatsNew
-- One-time "what's new" popup + a single auto-open of the Do-it-now planner so the
-- player sees it in action. Keyed to CURRENT (bump the string to re-announce); the
-- key is stored in SavedVariables so it fires exactly once, never on later logins.
local wn = {}
Private.WhatsNew = wn

local const = Private.constants
local CURRENT = "smartfeatures-v3"   -- bump this to show a new announcement

local FrameBackdrop = {
    bgFile   = "interface\\achievementframe\\ui-guildachievement-parchment",
    edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
    edgeSize = 16,
    insets   = { left = 4, right = 4, top = 4, bottom = 4 },
}

local B = "|cff00ff00\226\128\162|r  "   -- green bullet
local BODY =
"|cffffd700New Update!!|r\n\n"
.. "|cff66ff66Fixed:|r\n"
.. B .. "Text spacing on tooltips\n"
.. B .. "Optimised Quest & Storyline grabs \226\128\148 preventing lag in some zones\n"
.. B .. "Some icon-grabbing logic\n\n"
.. "|cffffd700Added:|r\n"
.. B .. "|cffffffffAuto setting|r |cffaaaaaa(in Filters)|r \226\128\148 automatically sets your waypoint to the closest objective\n"
.. B .. "|cffffffffAuto setting \226\128\148 instances only|r |cffaaaaaa(in Filters)|r \226\128\148 same as above, but only for instanced content\n"
.. B .. "|cffffffffPanel size setting|r |cffaaaaaa(in Filters)|r for readability\n"
.. B .. "|cffffffffBold text setting|r |cffaaaaaa(in Filters)|r for readability\n"
.. B .. "|cffffffffSub-waypoints for Auto mode|r \226\128\148 see the recommended waypoint first for each objective, even when they're all bunched into one objective (like the Feline Figurines achievement). For example, cave entrance locations, etc.\n"
.. B .. "|cffffffffCollapse|r the \"Other zones on this continent\" list\n"
.. B .. "|cffffffffLocked storylines listing|r \226\128\148 the required storylines you'll need later, before they unlock\n"
.. B .. "|cffffffffAuto open / close|r |cffaaaaaa(right-click the Minimap icon)|r \226\128\148 opens/closes the planner as you enter & leave Meta-tracked continents\n\n"
.. "|cffff9d5aPlease report any bugs! I go zone-by-zone by hand to check and optimise each unique achievement, so bear with the odd quirk \226\128\148 one shoe does NOT fit all when it comes to achievements... at all!|r\n\n"
.. "|cffffd700Spread the word \226\128\148 the #1 Meta Achievement Tracker is in town!!!|r"

function wn:Show()
    if self.frame then self.frame:Show(); return end

    local f = CreateFrame("Frame", "MetaTrackerWhatsNew", UIParent, "BackdropTemplate")
    f:SetSize(520, 560)
    f:SetPoint("CENTER", 0, 40)
    f:SetBackdrop(FrameBackdrop)
    f:SetBackdropBorderColor(1, 1, 1, 1)
    f:SetFrameStrata("FULLSCREEN_DIALOG")
    f:SetToplevel(true)
    f:EnableMouse(true)
    f:SetMovable(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", f.StopMovingOrSizing)

    local titleBar = f:CreateTexture(nil, "ARTWORK")
    titleBar:SetTexture("Interface\\AchievementFrame\\UI-Achievement-Borders")
    titleBar:SetPoint("TOPLEFT", 10, -10)
    titleBar:SetPoint("TOPRIGHT", -10, -10)
    titleBar:SetHeight(26)
    titleBar:SetVertexColor(1, 1, 1, 0.85)
    titleBar:SetTexCoord(0, 0.9765625, 0.66015625, 0.73828125)

    local title = f:CreateFontString(nil, "OVERLAY")
    title:SetFontObject(const.FONT_OBJECTS.HEADING)
    title:SetPoint("TOP", titleBar, "TOP", 0, -4)
    title:SetText(const.ADDON_NAME .. " \226\128\148 What's New")

    local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", 2, 2)

    -- Scroll the body so any amount of release-notes content fits without clipping.
    local scroll = CreateFrame("ScrollFrame", "MetaTrackerWhatsNewScroll", f, "UIPanelScrollFrameTemplate")
    scroll:SetPoint("TOPLEFT", 22, -44)
    scroll:SetPoint("BOTTOMRIGHT", -32, 50)

    local content = CreateFrame("Frame", nil, scroll)
    content:SetSize(440, 10)
    scroll:SetScrollChild(content)

    local body = content:CreateFontString(nil, "OVERLAY")
    body:SetFontObject(const.FONT_OBJECTS.NORMAL)
    body:SetPoint("TOPLEFT", 0, 0)
    body:SetWidth(440)
    body:SetJustifyH("LEFT")
    body:SetJustifyV("TOP")
    body:SetSpacing(4)
    body:SetText(BODY)
    -- size the scroll child to the wrapped text so the scrollbar range is correct
    content:SetHeight((body:GetStringHeight() or 400) + 12)

    local ok = CreateFrame("Button", nil, f, "UIPanelButtonTemplate")
    ok:SetSize(130, 24)
    ok:SetPoint("BOTTOM", 0, 16)
    ok:SetText("Awesome!")
    ok:SetScript("OnClick", function() f:Hide() end)

    self.frame = f
end

-- Fire once if this announcement hasn't been shown yet.
function wn:Maybe()
    local db = Private.Settings and Private.Settings.db
    if not db then return end
    if db._announce == CURRENT then return end
    db._announce = CURRENT               -- mark shown BEFORE, so a Lua error can't re-loop it
    self:Show()
    if Private.RouteHud then Private.RouteHud:ShowPreview() end
end
