---@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 = "doitnow-v1.1"   -- 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 =
"|cffffd700The Do-it-now Planner is live!|r  Your smart, cross-zone to-do list for finishing meta achievements:\n\n"
.. B .. "|cffffffffNearest objectives first|r - grouped by objective; supports complex multi-waypoint guides and more.\n"
.. B .. "|cffffffffGuides & notes on every step|r - follow the guides attached to each objective (in the tooltip), or even Ctrl-click a waypoint to write your own note.\n"
.. B .. "|cffffffffOther zones, Dungeons & Raids|r - a quick glance at other nearby zones and which dungeons/raids you can run to work toward your metas.\n"
.. B .. "|cffffffffWorld Quest Tracker|r - see when needed world quests are up in your zone, along with an attached Theater Troupe timer.\n\n"
.. "|cffffd700Extras:|r\n"
.. B .. "Rares, Treasures & more notifier\n"
.. B .. "Better achievement filtering - show achievements with only 1, 2 or 3 objectives remaining, to quickly finish the ones you've made the most progress on.\n"
.. B .. "3D model viewer for Mount & Pet rewards\n\n"
.. "Take a look, and let me know how you like it on CurseForge - there may be bugs, as it's definitely in a beta stage.\n\n"
.. "Thanks!\n|cffffd700-Ckay|r"

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

    local f = CreateFrame("Frame", "MetaTrackerWhatsNew", UIParent, "BackdropTemplate")
    f:SetSize(500, 540)
    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)

    local body = f:CreateFontString(nil, "OVERLAY")
    body:SetFontObject(const.FONT_OBJECTS.NORMAL)
    body:SetPoint("TOPLEFT", 22, -46)
    body:SetPoint("TOPRIGHT", -22, -46)
    body:SetJustifyH("LEFT")
    body:SetJustifyV("TOP")
    body:SetSpacing(3)
    body:SetText(BODY)

    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
