-- 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.40 - Added Voidstorm, Eversong Woods, Harandar, Isle of Quel'Danas, Zul'Aman zones. Added Vidious and Ziadan NPC triggers.",
    "v 1.359 - Starts Timers From Other Addons.",
    "v 1.358 - Changed Some File Names.",
    "v 1.357 - Clicking Share On A Timer Announces Time Remaining Zone Name And ShardID.",
    "v 1.356 - When Timer Frame Is Closed, Doesnt Show Up On Reload.",
    "v 1.355 - Fixed Some Messages.",
    "v 1.354 - Made Changes To Who Window.",
    "v 1.353 - Added Who Button For Leaders To Tell Who Has Addon Loaded.",
    "v 1.352 - Timer Start For Guild Member When Your Not In Party Or Raid.",
    "v 1.351 - Was Showing when People Joind And Left Inastance Group, Shouldn't Show At All Now.",
    "v 1.350 - Should Actually Be Fixed, No More Spam In BGs.",
    "v 1.349 - When A Timer Is Started, It Is Also Started For Guild Members At The Bottom.",
    "v 1.348 - Not In Party Or Raid, Should No Longer Show Up In Instance Group.",
    "v 1.347 - Added /sat To Share All Timers In Group. (It'll Share 56 Timers Instantly, So Idk If You Need It.)",
    "v 1.346 - When A Timer Is Forced To Restart, It Restarts Or Starts Timers For Guild Members Also.",
    "v 1.345 - Changed Timers Startpoints, New Timers Should Accurately Display 'Old' Timer Startpoints.",
    "v 1.344 - If Leader Doesn't Have WarSupplyCrates, Rotation Shows Own Top 6 Timers.",
    "v 1.343 - When Draging And Dropping Timers Time Still Keeps Counting.",
    "v 1.342 - Added Last Time A Timer Was Forced To Restart Next To The Down Button On Each Timer.",
    "v 1.341 - Added Print Coordinats To Next Zone Listed On Rotation. And Settings Option To Disable.",
    "v 1.340 - Clears Tom Tom After Collecting Conquest From War Supply Chest.",
    "v 1.339 - Start Group Has Pre Filled Copy Box For Easier Starting Groups.",
    "v 1.338 - When Someone Enters Zone And Mouses Over ShardID, IF Someone Has Timer In Group For ShardID It Is Moved To Top And Instantly Shared In Order To Update Rotation Instantly.",
    "v 1.337 - Fixed Auto Crate Down Announcements.",
    "v 1.336 - Added ShardID To Rotation Window",
    "v 1.335 - Added LFG Shortcut To Minimap Icon, Fixed Rotation From Leader When Scrolling. Changed Some Buttons.",
    "v 1.334 - Fixed A Rotation Window Error.",
    "v 1.333 - Rotation Stays Open On Reload If Opened Before Reload (If Moved To Different Position).",
    "v 1.329 - Added Conquest Icon Setting, If You Don't Like It For Some Reason.",
    "v 1.328 - Timers Now Drag And Drop, Click And Hold Timer Number Or Shard Number To Move.",
    "v 1.327 - Fixed Conquest Counter, Doesn't Reset When Spending Conquest.",
    "v 1.326 - When Join Group Rotatation Timers Start In Timer Window.",
    "v 1.325 - Announces Crate Automatically.",
    "v 1.324 - 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("WarSupplyCrates 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