local AC = _G.ArenaCore
if not AC then return end

AC.UpdateAlert = AC.UpdateAlert or {}
local UA = AC.UpdateAlert

UA.VERSION = 3

local function Clamp01(v)
    if v < 0 then return 0 end
    if v > 1 then return 1 end
    return v
end

local function ToHexColor(r, g, b)
    r = math.floor(Clamp01(r) * 255 + 0.5)
    g = math.floor(Clamp01(g) * 255 + 0.5)
    b = math.floor(Clamp01(b) * 255 + 0.5)
    return string.format("%02x%02x%02x", r, g, b)
end

local function GradientText(text, startColor, endColor)
    if type(text) ~= "string" or text == "" then
        return ""
    end

    local sr, sg, sb = startColor[1], startColor[2], startColor[3]
    local er, eg, eb = endColor[1], endColor[2], endColor[3]

    local out = {}
    local len = #text
    if len <= 1 then
        return "|cff" .. ToHexColor(sr, sg, sb) .. text .. "|r"
    end

    for i = 1, len do
        local ch = text:sub(i, i)
        local t = (i - 1) / (len - 1)
        local r = sr + (er - sr) * t
        local g = sg + (eg - sg) * t
        local b = sb + (eb - sb) * t
        out[#out + 1] = "|cff" .. ToHexColor(r, g, b) .. ch
    end

    out[#out + 1] = "|r"
    return table.concat(out)
end

local function EnsureGlobalDB()
    if not _G.ArenaCoreDB then
        _G.ArenaCoreDB = {}
    end
    _G.ArenaCoreDB.global = _G.ArenaCoreDB.global or {}
end

local function GetSeenVersion()
    EnsureGlobalDB()
    return _G.ArenaCoreDB.global.updateAlertSeenVersion
end

local function SetSeenVersion(version)
    EnsureGlobalDB()
    _G.ArenaCoreDB.global.updateAlertSeenVersion = version
end

function UA:Create()
    if self.frame then
        return self.frame
    end

    local f = CreateFrame("Frame", "ArenaCore_UpdateAlert", UIParent)
    f:SetAllPoints(UIParent)
    f:SetFrameStrata("FULLSCREEN_DIALOG")
    f:SetFrameLevel(999)
    f:Hide()
    f:SetAlpha(0)
    f:EnableMouse(false)
    f:EnableKeyboard(false)

    local bg = AC:CreateFlatTexture(f, "BACKGROUND", 0, {0, 0, 0, 1}, 1)
    bg:SetAllPoints()
    bg:SetAlpha(0.85)
    f.bg = bg

    local center = CreateFrame("Frame", nil, f)
    center:SetSize(760, 360)
    center:SetPoint("CENTER", 0, 0)

    local logo = center:CreateTexture(nil, "ARTWORK")
    logo:SetTexture("Interface/AddOns/ArenaCore/Media/Textures/aclogo.tga")
    logo:SetSize(110, 110)
    logo:SetPoint("TOP", 0, -10)
    f.logo = logo

    local title = center:CreateFontString(nil, "OVERLAY")
    title:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 44, "OUTLINE")
    title:SetTextColor(1, 1, 1, 1)
    title:SetPoint("TOP", logo, "BOTTOM", 0, -6)
    title:SetText(GradientText("ARENA CORE", {0.545, 0.271, 1.000}, {1.000, 0.478, 0.094}))
    f.title = title

    local msg0 = center:CreateFontString(nil, "OVERLAY")
    msg0:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 15, "")
    msg0:SetTextColor(0.2, 0.8, 0.2, 1)
    msg0:SetWidth(720)
    msg0:SetJustifyH("CENTER")
    msg0:SetJustifyV("MIDDLE")
    msg0:SetSpacing(5)
    msg0:SetPoint("TOP", title, "BOTTOM", 0, -18)
    msg0:SetText("(4/13/2026) UPDATE NOTICE: New/Improved visual changes to Class Icons, Racials and Trinkets, in terms of matching pixels/positions for all 3 frames with pixel perfect accuracy. You will likely need to adjust these items BEFORE going into arena! Thanks to Jaywazz for pointing this out. I am so sorry for making you guys adjust settings, but this change was needed for long term health and growth of Arena Core.")
    f.msg0 = msg0

    local msg = center:CreateFontString(nil, "OVERLAY")
    msg:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 16, "")
    msg:SetTextColor(1, 1, 1, 1)
    msg:SetWidth(720)
    msg:SetJustifyH("CENTER")
    msg:SetJustifyV("MIDDLE")
    msg:SetSpacing(5)
    msg:SetPoint("TOP", msg0, "BOTTOM", 0, -10)
    msg:SetText("(3/4/2026) ALERT: Arena Core has had a complete overhaul, you WILL need to likely make adjustments and fine tune your settings. Please do this before entering arena!")
    f.msg = msg

    local msg2 = center:CreateFontString(nil, "OVERLAY")
    msg2:SetFont(AC.FONT_PATH or "Interface/AddOns/ArenaCore/Media/Fonts/arenacore.ttf", 15, "")
    msg2:SetTextColor(1, 1, 1, 1)
    msg2:SetWidth(720)
    msg2:SetJustifyH("CENTER")
    msg2:SetJustifyV("MIDDLE")
    msg2:SetSpacing(5)
    msg2:SetPoint("TOP", msg, "BOTTOM", 0, -10)
    msg2:SetText("NOTICE: There may be small bugs as the entire addon was changed and with Midnight here.. please report issues in the Discord, I will actively be monitoring and updating as needed.")
    f.msg2 = msg2

    local btnRow = CreateFrame("Frame", nil, f)
    btnRow:SetSize(520, 40)
    btnRow:SetPoint("TOP", msg2, "BOTTOM", 0, -22)

    local function Close()
        SetSeenVersion(self.VERSION)
        self:Hide()
    end

    local btn1 = AC:CreateProfileStyleButton(btnRow, 240, 36, "I Read The Alert", "primary", 11)
    btn1:SetPoint("LEFT", 0, 0)
    btn1:SetScript("OnClick", Close)

    local btn2 = AC:CreateProfileStyleButton(btnRow, 240, 36, "I Did NOT Read The Alert", "primary", 11)
    btn2:SetPoint("RIGHT", 0, 0)
    btn2:SetScript("OnClick", Close)

    f:SetScript("OnKeyDown", function() end)
    f:SetPropagateKeyboardInput(false)

    local fadeIn = f:CreateAnimationGroup()
    local a1 = fadeIn:CreateAnimation("Alpha")
    a1:SetFromAlpha(0)
    a1:SetToAlpha(1)
    a1:SetDuration(0.35)
    a1:SetSmoothing("OUT")
    f.fadeIn = fadeIn

    local fadeOut = f:CreateAnimationGroup()
    local a2 = fadeOut:CreateAnimation("Alpha")
    a2:SetFromAlpha(1)
    a2:SetToAlpha(0)
    a2:SetDuration(0.25)
    a2:SetSmoothing("OUT")
    fadeOut:SetScript("OnFinished", function()
        f:EnableMouse(false)
        f:EnableKeyboard(false)
        f:SetAlpha(0)
        f:Hide()
    end)
    f.fadeOut = fadeOut

    self.frame = f
    return f
end

function UA:Show(force)
    local f = self:Create()
    if not f then return end

    if not force and GetSeenVersion() == self.VERSION then
        return
    end

    f:Show()
    f.fadeOut:Stop()

    f:EnableMouse(true)
    f:EnableKeyboard(true)
    f:SetPropagateKeyboardInput(false)

    f:SetAlpha(1)
    f.fadeIn:Stop()
    f.fadeIn:Play()

    C_Timer.After(0.1, function()
        if f and f:IsShown() and f:GetAlpha() <= 0 then
            f:SetAlpha(1)
        end
    end)
end

function UA:Hide()
    local f = self.frame
    if not f or not f:IsShown() then
        return
    end

    f:EnableMouse(false)
    f:EnableKeyboard(false)
    f.fadeIn:Stop()
    f.fadeOut:Stop()
    f.fadeOut:Play()
end

function UA:MaybeShowOnLoginReload()
    if GetSeenVersion() == self.VERSION then
        return
    end

    if InCombatLockdown and InCombatLockdown() then
        C_Timer.After(1.0, function()
            UA:MaybeShowOnLoginReload()
        end)
        return
    end

    self:Show(false)
end
