local _, addonTable = ...
local WipedQoL = addonTable.WipedQoL
local TRANSLATE = addonTable.TRANSLATE or function(msg) return msg end

local changelogFrame

local changelogEntries = {
    "Version 1.0.5",
    "",
    "- Legacy item selling window on vendor",
    "- Add your own Sounds to the addon",
    "- Back and forward navigation in the achievement window",
    "- Combat potion macro updater",
    "- Custom addon accent colors",
    "- Skin for raid loot history",
    "- Expanded queue indicator customization",
    "_______________________________________________",
    "Version 1.0.4",
    "",
    "- Weekly Task Reminder (Todo-List)",
    "- Skin for queue indicator",
    "- Add spells to healing macro (e.g. phial for rogue or exhil for hunter)",
    "- Mover for popups (loot, roll, achievement, etc.)",
    "_______________________________________________",
    "Version 1.0.3",
    "",
    "- UI overhaul",
    "- Mover for Loot, Roll and Achievement frames",
    "- Sound randomizer on group death",
    "_______________________________________________",    
    "Version 1.0.2",
    "",
    "- A lot of smol bugfixes",
    "- Play sound on successful keystone",
    "- Weird party animation when looting a mount",
    "_______________________________________________",
    "Version 1.0.1",
    "",
    "- Auto-Roll in legacy content",
    "- Enable / Disable combat potions in macros",
    "- Healing macro with action bar drag support",
    "- Play some sounds on some events",
    "_______________________________________________",
    "Version 1.0.0",
    "",
    "- Initial WipedQoL release",
}

local function BuildChangelogText()
    local localizedEntries = {}
    for index, entry in ipairs(changelogEntries) do
        localizedEntries[index] = TRANSLATE(entry)
    end

    return table.concat(localizedEntries, "\n")
end

function WipedQoL:GetChangelogText()
    return BuildChangelogText()
end

local function SkinFrame(frame)
    local UI = addonTable.UI
    if UI and UI.SkinFrame then
        UI.SkinFrame(frame)
    elseif addonTable.SkinFrame then
        addonTable.SkinFrame(frame)
    end
end

local function SkinCloseButton(button)
    local UI = addonTable.UI
    if UI and UI.SkinCloseButton then
        UI.SkinCloseButton(button)
    elseif addonTable.SkinCloseButton then
        addonTable.SkinCloseButton(button)
    elseif addonTable.SkinButton then
        addonTable.SkinButton(button)
    end
end

local function SkinScrollBar(scrollBar)
    local UI = addonTable.UI
    if UI and UI.SkinScrollBar then
        UI.SkinScrollBar(scrollBar)
    elseif addonTable.SkinScrollBar then
        addonTable.SkinScrollBar(scrollBar)
    end
end

function WipedQoL:CreateChangelogFrame(parent)
    if changelogFrame then
        return changelogFrame
    end

    changelogFrame = CreateFrame("Frame", "WipedQoLChangelogFrame", UIParent, "BackdropTemplate")
    changelogFrame:SetSize(500, 360)
    changelogFrame:SetPoint("CENTER", parent or UIParent, "CENTER", 0, 0)
    changelogFrame:SetFrameStrata("DIALOG")
    changelogFrame:SetFrameLevel(200)
    changelogFrame:SetMovable(true)
    changelogFrame:EnableMouse(true)
    changelogFrame:RegisterForDrag("LeftButton")
    changelogFrame:SetClampedToScreen(true)
    changelogFrame:SetScript("OnDragStart", changelogFrame.StartMoving)
    changelogFrame:SetScript("OnDragStop", changelogFrame.StopMovingOrSizing)
    changelogFrame:Hide()

    SkinFrame(changelogFrame)

    local title = changelogFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    title:SetPoint("TOPLEFT", changelogFrame, "TOPLEFT", 16, -14)
    title:SetText(TRANSLATE("Changelog"))
    title:SetTextColor(1, 1, 1, 0.95)
    if addonTable.ApplyFont then
        addonTable.ApplyFont(title, 16, "OUTLINE")
    end

    local closeButton = CreateFrame("Button", nil, changelogFrame, "UIPanelCloseButton")
    closeButton:SetPoint("TOPRIGHT", changelogFrame, "TOPRIGHT", -4, -4)
    SkinCloseButton(closeButton)

    local scrollFrame = CreateFrame("ScrollFrame", nil, changelogFrame, "UIPanelScrollFrameTemplate")
    scrollFrame:SetPoint("TOPLEFT", changelogFrame, "TOPLEFT", 16, -42)
    scrollFrame:SetPoint("BOTTOMRIGHT", changelogFrame, "BOTTOMRIGHT", -32, 16)

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

    local text = content:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    text:SetPoint("TOPLEFT", content, "TOPLEFT", 0, 0)
    text:SetPoint("TOPRIGHT", content, "TOPRIGHT", 0, 0)
    text:SetJustifyH("LEFT")
    text:SetJustifyV("TOP")
    text:SetTextColor(0.86, 0.86, 0.86, 1)
    text:SetSpacing(4)
    text:SetNonSpaceWrap(true)
    text:SetWordWrap(true)
    text:SetText(BuildChangelogText())
    if addonTable.ApplyFont then
        addonTable.ApplyFont(text, 13, "")
    end

    local function RefreshContentSize()
        local width = scrollFrame:GetWidth()
        if width and width > 0 then
            text:SetWidth(width - 8)
            content:SetWidth(width - 8)
        else
            text:SetWidth(440)
            content:SetWidth(440)
        end

        content:SetHeight((text:GetStringHeight() or 0) + 10)
    end

    content:SetScript("OnSizeChanged", RefreshContentSize)
    scrollFrame:SetScript("OnSizeChanged", RefreshContentSize)
    RefreshContentSize()

    if scrollFrame.ScrollBar then
        SkinScrollBar(scrollFrame.ScrollBar)
    end

    changelogFrame.Text = text
    changelogFrame.Content = content
    changelogFrame.ScrollFrame = scrollFrame

    if UISpecialFrames then
        table.insert(UISpecialFrames, "WipedQoLChangelogFrame")
    end

    return changelogFrame
end

function WipedQoL:ToggleChangelogFrame(parent)
    local frame = self:CreateChangelogFrame(parent)
    if frame:IsShown() then
        frame:Hide()
        return
    end

    frame:Show()
    frame:ClearAllPoints()
    frame:SetPoint("CENTER", parent or UIParent, "CENTER", 0, 0)
end
