setFont("res/font.ttf")

menuLabels = {
    "Start"
}

-- there is much vibecode lmao
local currentMenuState = 0 -- 0 means start, 1 means main menu, 2 means chapter select
local selectedItem = 1

local showCharacter = false
local characterAlpha = 5
local characterX = getScreenWidth() - getScreenWidth() / 4.5
local characterTargetX = getScreenWidth() - getScreenWidth() / 3.5
local characterSpeed = 400
local character = loadTexture("res/self.png")
local characterAnimatingOut = false
local characterAnimatingIn = false
local isStartPressed = false
local isVisible = true

local isBlinking = false
local blinkDurationTotal = 1.6 -- 2 seconds
local blinkElapsedTime = 0

local waveEffects = {}
local waveTimer = 0
local waveInterval = 1.0
local maxWaves = 1

local r11 = loadTexture("res/r11logo.png")
loadBackground("res/backgrounds/TT_BG0.PNG", 0)
local start = loadTexture("res/misc/start.png")

loadMusic("res/music/menu.mp3")
playMusic()
loadAnimationUI("res/uifx/snow", "snow", 0.060)
playAnimationUI()
drawBackground(getScreenWidth()/2, getScreenHeight()/2, 2.1, 0)

function addWaveEffect(x, y, scale)
    table.insert(waveEffects, {
        x = x,
        y = y,
        scale = scale,
        alpha = 255,
        scaleSpeed = 0.5,
        fadeSpeed = 2.5
    })
end

function EventLoop()

    -- Draw logo (common for all states)
    local scale = 1.25
    local logoX = getScreenWidth()/2 - (getTextureWidth(r11) * scale)/2
    local logoY = getScreenHeight()/3 - (getTextureHeight(r11) * scale)/2 - 60
    drawTextureEx(r11, logoX, logoY, 0.0, scale)
    
    local deltaTime = getDeltaTime()
    
    local textSize = 40
    local nonselectedColor = {r = 161, g = 161, b = 161, a = 255}
    local selectedColor = {r = 255, g = 255, b = 255, a = 255}
    
    -- Handle key presses for menu navigation
    if isKeyPressed(264) then -- Down arrow
        playSfx("res/sfx/003.WAV")
        selectedItem = selectedItem + 1
        if selectedItem > #menuLabels then
            selectedItem = 1
        end
    elseif isKeyPressed(265) then -- Up arrow
        playSfx("res/sfx/003.WAV")
        selectedItem = selectedItem - 1
        if selectedItem < 1 then
            selectedItem = #menuLabels
        end
    end
    
    -- Start screen
    if currentMenuState == 0 then
        if not isBlinking then
            local startScale = 2.0
            local startWidth = getTextureWidth(start) * startScale
            local startHeight = getTextureHeight(start) * startScale
            local startX = math.floor(getScreenWidth() / 2 - startWidth / 2)
            local startY = math.floor(getScreenHeight() - getScreenHeight()/3 - 50)
            
            -- Рисуем изображение вместо текста
            drawTextureEx(start, startX, startY, 0.0, startScale)

            if isKeyPressed(257) or isMouseButtonPressed(0) then -- Enter key
                playSfx("res/sfx/004.WAV")
                isBlinking = true
                blinkElapsedTime = 0
            end
        else
            -- Blink logic
            blinkElapsedTime = blinkElapsedTime + deltaTime

            -- Calculate visibility using small intervals
            local blinkInterval = 0.06 -- 200ms per blink
            isVisible = (math.floor(blinkElapsedTime / blinkInterval) % 2) == 0

            -- Draw blinking "Start" image instead of text
            local startScale = 2.0
            local startWidth = getTextureWidth(start) * startScale
            local startHeight = getTextureHeight(start) * startScale
            local startX = math.floor(getScreenWidth() / 2 - startWidth / 2)
            local startY = math.floor(getScreenHeight() - getScreenHeight()/3 - 50)

            if isVisible then
                -- Рисуем изображение с нормальной прозрачностью
                drawTextureEx(start, startX, startY, 0.0, startScale, {r = 255, g = 255, b = 255, a = 255})
            else
                -- Рисуем изображение с пониженной прозрачностью (имитация мерцания)
                drawTextureEx(start, startX, startY, 0.0, startScale, {r = 255, g = 255, b = 255, a = 100})
            end

            -- After 2 seconds, proceed to main menu
            if blinkElapsedTime >= blinkDurationTotal then
                isBlinking = false
                currentMenuState = 1
                menuLabels = {"New Game", "Exit"}
                selectedItem = 1
                showCharacter = false
                characterAlpha = 5
                characterX = getScreenWidth() - getScreenWidth() / 4.5
                waveEffects = {}
            end
        end
    end
    
    -- Main menu
    if currentMenuState == 1 then
        for index, text in ipairs(menuLabels) do
            local color = (index == selectedItem) and selectedColor or nonselectedColor
            
            local textWidth = math.floor(measureTextX(text, textSize))
            local x = math.floor(getScreenWidth() / 2 - textWidth / 2)
            local y = math.floor(getScreenHeight() - getScreenHeight()/3 - 50 + (index - 1) * 60)
            
            drawText(text, x, y, textSize, color)
        end
        
        -- Escape key returns to start screen
        if isKeyPressed(256)  or isMouseButtonPressed(1) then
            currentMenuState = 0
            menuLabels = {"Start"}
            selectedItem = 1
            showCharacter = false
            characterAlpha = 5
            characterX = getScreenWidth() - getScreenWidth() / 4.5
            waveEffects = {}
        end
        
        -- Enter key selects menu option
        if isKeyPressed(257)  or isMouseButtonPressed(0) then
            playSfx("res/sfx/006.WAV")
            if selectedItem == 1 then -- New Game
                characterAlpha = 5
                characterX = getScreenWidth() - getScreenWidth() / 4.5
                characterTargetX = getScreenWidth() - getScreenWidth() / 3.5
                characterSpeed = 400
                currentMenuState = 2
                menuLabels = {"Self chapter"}
                selectedItem = 1
                showCharacter = true
                characterAnimatingOut = false
                characterAnimatingIn = true
                waveEffects = {}
                waveTimer = 0
            elseif selectedItem == 2 then -- Exit
                setGameState(3)
            end
        end
    end
    
    -- Chapter select screen
    if currentMenuState == 2 then
        -- Draw character if shown
        if showCharacter then
            if characterAnimatingOut then
                characterAlpha = characterAlpha - 255 * deltaTime * 4
                characterX = characterX + characterSpeed * deltaTime
                
                if characterAlpha <= 5 or characterX >= getScreenWidth() - getScreenWidth() / 4.5 then
                    characterAlpha = 5
                    characterX = getScreenWidth() - getScreenWidth() / 4.5
                    showCharacter = false
                    characterAnimatingOut = false
                    currentMenuState = 1
                    menuLabels = {"New Game", "Exit"}
                    selectedItem = 1
                    waveEffects = {}
                end
            else
                characterAlpha = characterAlpha + (255 - 90) * deltaTime * 2
                if characterAlpha > 255 then characterAlpha = 255 end
                
                if characterX > characterTargetX then
                    characterX = characterX - characterSpeed * deltaTime
                    if characterX < characterTargetX then characterX = characterTargetX end
                end
                
                if characterAlpha > 200 and #waveEffects < maxWaves then
                    waveTimer = waveTimer + deltaTime
                    if waveTimer >= waveInterval then
                        waveTimer = 0
                        addWaveEffect(characterX, getScreenHeight()/2 - getTextureHeight(character), 2.0)
                    end
                end
            end
            
            for i = #waveEffects, 1, -1 do
                local wave = waveEffects[i]
                wave.scale = wave.scale + wave.scaleSpeed * deltaTime
                wave.alpha = wave.alpha - wave.fadeSpeed * deltaTime * 255
                
                if wave.alpha <= 0 then
                    table.remove(waveEffects, i)
                else
                    local waveColor = {r = 100, g = 255, b = 100, a = math.floor(wave.alpha)}
                    drawTextureEx(character, wave.x, wave.y, 0.0, wave.scale, waveColor)
                end
            end
            
            local charColor = {r = 100, g = 255, b = 100, a = math.floor(characterAlpha)}
            drawTextureEx(character, characterX, getScreenHeight()/2 - getTextureHeight(character), 0.0, 2.0, charColor)
        end
        
        -- Draw chapter select menu
        for index, text in ipairs(menuLabels) do
            local color = (index == selectedItem) and selectedColor or nonselectedColor
            
            local textWidth = math.floor(measureTextX(text, textSize))
            local x = math.floor(getScreenWidth() / 2 - textWidth / 2)
            local y = math.floor(getScreenHeight() - getScreenHeight()/3 - 50 + (index - 1) * 60)
            
            drawText(text, x, y, textSize, color)
        end
        
        -- Enter key starts selected chapter
        if isKeyPressed(257)  or isMouseButtonPressed(0) then
            if selectedItem == 1 then -- Self chapter
                playSfx("res/sfx/006.WAV")
                stopDrawBackground(0)
                currentTime = getTime()
                while getTime() - currentTime < 0.4 do
                    
                end
                unloadBackground(0)
                unloadTexture(r11)
                unloadTexture(character)
                stopAnimationUI()
                unloadAnimationUI()
                setGameState(2)
            end
        end
        
        -- Escape key returns to main menu with animation
        if isKeyPressed(256)  or isMouseButtonPressed(1) then
            if showCharacter and not characterAnimatingOut then
                characterAnimatingOut = true
                playSfx("res/sfx/003.WAV")
            end
        end
    end
    
end
