-- Changelog.lua (Updated for BlackBox Addon with Animation)
-- This version uses the version number from TimerFrame.lua and scrolls like a rockstar

local changelogEntries = {
    "v 1.330 - When New Shard Is Populated It Looks For Timers With Same Shard And Moves It To The Top For Everyone.",
    "v 1.323 - Added Shard Log Click On ShardID To Open, Rotation Window Moovable Can Be Left Open.",
    "v 1.322 - Updated Rotation Window.",
    "v 1.321 - Added ShardID Log To ShardID Button.",
    "v 1.320 - Removed TomTom Not Loaded Message.",
    "v 1.319 - Rotation Window Added, Left Click Scrolling Text. And Throttle ShardID Announce.",
    "v 1.318 - Updated TopPoints",
    "v 1.317 - Changed Some Tooltips.",
    "v 1.316 - Fixed 'Slam Central Station'.",
    "v 1.315 - ShardID Doesn't Announce Dornogal.",
    "v 1.314 - Added Right Click For Scrolling Speed.",
    "v 1.313 - Fixed Conquest Bug.",
    "v 1.312 - Announces ShardID When Entering A Zone, Or Reloading.",
    "v 1.311 - Fixed Timers Not Setting New Start Point When Restarted.",
    "v 1.310 - Should Fixed Some Timers Restarting On Restart.",
    "v 1.309 - Spread Out Button Added To Icon Menu.",
    "v 1.308 - Timers Red Under 3 Minutes.",
    "v 1.307 - Added Changelog.",
    "v 1.306 - Rotation Shows Leaders Top 6 Timers, 1 Per Zone.",    
}
local function GetAddonVersion()
    local frame = _G["TimerFrame"]
    if frame and frame.versionText then
        return frame.versionText:GetText()
    end
    return changelogEntries[1] or "v ???"
end

local function CreateChangelogFrame()
    local frame = CreateFrame("Frame", "BlackBoxChangelogFrame", UIParent, "BasicFrameTemplateWithInset")
    frame:SetSize(300, 400)
    frame:SetPoint("CENTER")
    frame:SetMovable(true)
    frame:EnableMouse(true)
    frame:RegisterForDrag("LeftButton")
    frame:SetScript("OnDragStart", frame.StartMoving)
    frame:SetScript("OnDragStop", frame.StopMovingOrSizing)

    frame.title = frame:CreateFontString(nil, "OVERLAY")
    frame.title:SetFontObject("GameFontHighlight")
    frame.title:SetPoint("LEFT", frame.TitleBg, "LEFT", 5, 0)
    frame.title:SetText("BlackBox Changelog")

    local scrollFrame = CreateFrame("ScrollFrame", nil, frame, "UIPanelScrollFrameTemplate")
    scrollFrame:SetPoint("TOPLEFT", 10, -30)
    scrollFrame:SetPoint("BOTTOMRIGHT", -30, 10)

    local content = CreateFrame("Frame", nil, scrollFrame)
    content:SetSize(260, 2000)
    scrollFrame:SetScrollChild(content)

    local changelogText = content:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    changelogText:SetPoint("TOP", 0, -scrollFrame:GetHeight() - 5 * 13)
    changelogText:SetWidth(260)
    changelogText:SetJustifyH("CENTER")

    local changelogString = table.concat(changelogEntries, "\n")
    changelogText:SetText(changelogString)

    local textHeight = changelogText:GetStringHeight()
    content:SetHeight(textHeight)

    local animationGroup = changelogText:CreateAnimationGroup()
    local animation = animationGroup:CreateAnimation("Translation")
    animation:SetOffset(0, scrollFrame:GetHeight() + 5 * changelogText:GetLineHeight())
    animation:SetDuration(10)
    animation:SetSmoothing("OUT")
    animation:SetScript("OnFinished", function()
        changelogText:SetPoint("TOP", 0, 0)
    end)

    frame:Show()
    animationGroup:Play()

    return frame
end

local function ShowChangelogIfNeeded()
    BlackBoxDB = BlackBoxDB or {}
    BlackBoxDB.settings = BlackBoxDB.settings or {}

    local version = GetAddonVersion()
    if BlackBoxDB.settings.lastSeenVersion ~= version then
        local changelogFrame = CreateChangelogFrame()
        changelogFrame:Show()
        BlackBoxDB.settings.lastSeenVersion = version
        print("[BlackBox] Updated to version " .. version)
    end
end

local frame = CreateFrame("Frame")
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", function(self, event, arg1)
    if arg1 == "BlackBox" then
        ShowChangelogIfNeeded()
    end
end)

SLASH_BLACKBOXCHANGELOG1 = "/bbcl"
SlashCmdList["BLACKBOXCHANGELOG"] = function(msg)
    local changelogFrame = CreateChangelogFrame()
    changelogFrame:Show()
end