-- 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.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