-- ========================================================
-- RNTools - Large Widescreen Cinematic Center-Top Animation
-- Double-Gate Rendering Edition (100% Loading Screen Proof)
-- Optimized specifically for WoW 3.3.5a Legacy Client
-- ========================================================

local SplashData = {
    Tic = 0,
    DurationIn = 1.0,   -- Perfect 1-second smooth fade in
    DurationHold = 6.0, -- Extended 6-second presentation for module logs
    DurationOut = 1.8,  -- Smooth skyward drift fade out (1.8 seconds)
    HasPlayed = false,
    
    -- --- ADVANCED ENGINE STATE GATE ---
    IsEngineReady = false,
    SafetyBuffer = 0
}

-- Target baseline positioning parameters (Anchored to the TOP center of the screen)
local BASE_Y_OFFSET = -20 -- 20px from top of screen
local FLOAT_DISTANCE = 120 -- How many pixels upward it drifts into the sky during fade out

-- Complete list of modules with the ".lua" extensions removed
local MODULES_LIST = {
    "BiS_Tab",
    "Master_Looter",
    "Spammer",
    "Auto_Response",
    "AutoInvite",
    "MS_Changes",
    "RaidBrowser"
}

-- Create the master frame container
local SplashFrame = CreateFrame("Frame", "RNToolsSplashFrame", UIParent)

-- Large widescreen cinematic dimensions (540 width by 304 height)
SplashFrame:SetSize(380, 214) 
SplashFrame:SetPoint("TOP", UIParent, "TOP", 0, BASE_Y_OFFSET) -- CHANGED TO TOP ANCHOR POINT
SplashFrame:SetAlpha(0)   
SplashFrame:SetScale(0.7)  
SplashFrame:Hide()

local texture = SplashFrame:CreateTexture(nil, "BACKGROUND")
texture:SetAllPoints(SplashFrame)
texture:SetTexture("Interface\\AddOns\\RNTools\\Textures\\Splash")

-- ========================================================
-- ADVANCED TEXT LAYOUT & EFFECTS SETUP
-- ========================================================

-- Dark semi-transparent text backdrop frame to enhance text readability
local TextBackdrop = CreateFrame("Frame", nil, SplashFrame)
TextBackdrop:SetSize(450, 70) 
TextBackdrop:SetPoint("BOTTOM", SplashFrame, "BOTTOM", 0, -45)

-- UI Backdrop mapping pattern compatible natively with 3.3.5a
TextBackdrop:SetBackdrop({
    bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    tile = true, tileSize = 16, edgeSize = 12,
    insets = { left = 3, right = 3, top = 3, bottom = 3 }
})
TextBackdrop:SetBackdropColor(0, 0, 0, 0.75) 
TextBackdrop:SetBackdropBorderColor(0.2, 0.6, 1, 0.4) 

-- FLASH GLOW TEXT (Renders behind primary text to simulate an energy aura)
local glowText = TextBackdrop:CreateFontString(nil, "BACKGROUND", "GameFontNormalLarge")
glowText:SetPoint("TOP", TextBackdrop, "TOP", 0, -12)
glowText:SetTextColor(0, 0.8, 1, 0.5) 

-- THE PRIMARY TEXT OVERLAY (Renders sharply on top)
local mainText = TextBackdrop:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
mainText:SetPoint("TOP", TextBackdrop, "TOP", 0, -12)

-- THE YOUTUBE SUB PROMO TEXT
local subText = TextBackdrop:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
subText:SetPoint("BOTTOM", TextBackdrop, "BOTTOM", 0, 10)
subText:SetTextColor(1, 0.2, 0.2, 0.9) 
subText:SetText("YouTube: www.youtube.com/@RealNibbaWarmane")

-- ========================================================
-- RUNTIME TICKER & PROCESSING ENGINE
-- ========================================================

local function Splash_OnUpdate(self, elapsed)
    local dt = elapsed or arg1 or 0
    
    -- ---- GATE 1: THE ACCELERATED LOADING SCREEN DETECTOR ----
    if not SplashData.IsEngineReady then
        if WorldFrame and WorldFrame:IsVisible() and UIParent:IsVisible() then
            SplashData.SafetyBuffer = SplashData.SafetyBuffer + dt
            if SplashData.SafetyBuffer >= 0.3 then
                SplashData.IsEngineReady = true
                self:SetAlpha(0)
                self:SetScale(0.7)
            end
        end
        return 
    end

    -- Update runtime track ticks once screen validation clearance is granted
    SplashData.Tic = SplashData.Tic + dt

    local tic = SplashData.Tic
    local fIn = SplashData.DurationIn
    local fHold = SplashData.DurationIn + SplashData.DurationHold
    local fOut = SplashData.DurationIn + SplashData.DurationHold + SplashData.DurationOut

    -- ---- TEXT PROCESSING LAYER ----
    local wave = math.sin(tic * 4) 
    local pulseAlpha = 0.3 + ((wave + 1) * 0.25)

    local currentText = "RealNibba's Raid Tools v1.2 (Beta)"
    if tic < fHold then
        local numModules = #MODULES_LIST
        local moduleIndex = math.floor((tic / fHold) * numModules) + 1
        moduleIndex = math.min(moduleIndex, numModules)
        
        local dotCount = math.floor((tic * 1.5) % 4) + 1 
        local dots = string.rep(".", dotCount)
        
        currentText = "|cff00ffffLoading Module:|r " .. MODULES_LIST[moduleIndex] .. " " .. dots
    else
        currentText = "|cff00ff00RN Tools initialized, Enjoy & Drop a sub on Youtube :]]|r"
    end

    mainText:SetText(currentText)
    glowText:SetText(currentText)
    glowText:SetAlpha(pulseAlpha)
    glowText:SetPoint("TOP", TextBackdrop, "TOP", wave * 1.0, -12)

    -- ---- POSITIONING & FADE MATRIX LAYER ----
    if tic < fIn then
        -- Phase 1: Slow 1-second fade in and scale up at the center-top position
        local progress = tic / fIn
        self:SetAlpha(progress)
        self:SetScale(0.7 + (progress * 0.3)) 
        self:SetPoint("TOP", UIParent, "TOP", 0, BASE_Y_OFFSET)

    elseif tic < fHold then
        -- Phase 2: Static display center-top position hold
        self:SetAlpha(1)
        self:SetScale(1.0)
        self:SetPoint("TOP", UIParent, "TOP", 0, BASE_Y_OFFSET)

    elseif tic < fOut then
        -- Phase 3: Float UP over the top of the screen while fading out smoothly towards the sky
        local progress = (tic - fHold) / SplashData.DurationOut
        
        self:SetAlpha(1 - progress)
        self:SetScale(1.0 - (progress * 0.1)) 
        
        local currentY = BASE_Y_OFFSET + (progress * FLOAT_DISTANCE)
        self:SetPoint("TOP", UIParent, "TOP", 0, currentY)

    else
        -- Phase 4: Animation completely finished. Unhook frames.
        self:Hide()
        self:SetScript("OnUpdate", nil)
        
        if DEFAULT_CHAT_FRAME then
            DEFAULT_CHAT_FRAME:AddMessage("|cff00ffffRNTools:|r Initialization sequence complete.")
        end
    end
end

-- ========================================================
-- CORE EVENT INITIALIZER TRIGGER
-- ========================================================
local TriggerFrame = CreateFrame("Frame")
TriggerFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
TriggerFrame:SetScript("OnEvent", function(self, event)
    if event ~= "PLAYER_ENTERING_WORLD" then return end
    -- Only show once per session - never on zone transitions or /reload
    if SplashData.HasPlayed then return end
    SplashData.HasPlayed = true

    SplashData.Tic = 0
    SplashData.SafetyBuffer = 0
    SplashData.IsEngineReady = false

    SplashFrame:Hide()
    SplashFrame:SetAlpha(0)
    SplashFrame:Show()
    SplashFrame:SetScript("OnUpdate", Splash_OnUpdate)
end)