local dodgeActive = false
local dodgeTimer = 0
local dodgeDuration = 0
local canAcceptInput = false
local waitingForSpaceRelease = false
local dodgeSuccess = false
local inputDelay = 0.5

local flashTimer = 0
local flashInterval = 0.1
local flashing = false

function onCreate()
    makeLuaText("spaceDodgeText", "PRESS SPACE", 0, 0, 0)
    setTextFont("spaceDodgeText", "warning.ttf")
    setTextSize("spaceDodgeText", 96) -- Bigger size
    setTextColor("spaceDodgeText", 'FF0000') -- RED
    setTextAlignment("spaceDodgeText", "center")
    setObjectCamera("spaceDodgeText", "hud")
    setProperty("spaceDodgeText.visible", false)
    addLuaText("spaceDodgeText")
end

function centerDodgeText()
    local centerX = (screenWidth / 2) - (getTextWidth("spaceDodgeText") / 2)
    local centerY = (screenHeight / 2) - 48 -- Adjusted for new size
    setProperty("spaceDodgeText.x", centerX)
    setProperty("spaceDodgeText.y", centerY)
end

function onEvent(name, value1, value2)
    if name == "spaceDodge" then
        local botplay = getProperty('cpuControlled')
        if botplay then return end

        if value1 == "on" then
            if dodgeActive then return end

            dodgeActive = true
            dodgeSuccess = false
            dodgeTimer = 0
            flashTimer = 0
            canAcceptInput = false
            waitingForSpaceRelease = false
            dodgeDuration = tonumber(value2) or 3
            flashing = true

            setProperty("spaceDodgeText.visible", true)
            centerDodgeText()

            if getPropertyFromClass('flixel.FlxG', 'keys.pressed.SPACE') then
                waitingForSpaceRelease = true
            end

        elseif value1 == "off" then
            dodgeActive = false
            dodgeSuccess = false
            canAcceptInput = false
            waitingForSpaceRelease = false
            flashing = false
            setProperty("spaceDodgeText.visible", false)
        end
    end
end

function onUpdate(elapsed)
    if not dodgeActive then return end

    dodgeTimer = dodgeTimer + elapsed
    flashTimer = flashTimer + elapsed

    -- Flashing text visibility
    if flashing and flashTimer >= flashInterval then
        setProperty("spaceDodgeText.visible", not getProperty("spaceDodgeText.visible"))
        flashTimer = 0
    end

    -- Input activation delay
    if dodgeTimer >= inputDelay then
        if waitingForSpaceRelease then
            if not getPropertyFromClass('flixel.FlxG', 'keys.pressed.SPACE') then
                waitingForSpaceRelease = false
                canAcceptInput = true
            end
        else
            canAcceptInput = true
        end
    end

    -- Handle input
    if canAcceptInput and not dodgeSuccess then
        if getPropertyFromClass('flixel.FlxG', 'keys.justPressed.SPACE') then
            dodgeSuccess = true
            dodgeActive = false
            flashing = false
            setProperty("spaceDodgeText.visible", false)
            characterPlayAnim('dad', 'hey', true)
            playDodgeAnimation()
            return
        end
    end

    -- Handle failure
    if dodgeTimer >= dodgeDuration and not dodgeSuccess then
        dodgeActive = false
        flashing = false
        setProperty("spaceDodgeText.visible", false)
        characterPlayAnim('dad', 'hey', true)
        playHurtAnimation()
        local currentHealth = getProperty('health')
        setProperty('health', currentHealth * 0)
    end
end

function playDodgeAnimation()
    characterPlayAnim('boyfriend', 'dodge', true)
end

function playHurtAnimation()
    characterPlayAnim('boyfriend', 'hurt', true)
end
