-- Flappy Bird Clone using GameEngine

local vector2 = require("vector2")
local utils = require("utils")

-- Game Variables
local bird = {
    position = vector2.new(100, 200),
    width = 30,
    height = 30,
    velocity = 0,
    jumpStrength = -8
}

local gravity = 0.5
local pipes = {}
local pipeWidth = 60
local pipeGap = 120
local pipeSpeed = 2
local score = 0
local gameRunning = true
local gameStarted = false
local screenWidth = GameEngine:getWidth()
local screenHeight = GameEngine:getHeight()

-- Function to spawn new pipes
local function spawnPipe()
    --GameEngine:messageBox(tostring(screenHeight - pipeGap - 50))
    local pipeHeight = math.random(50, screenHeight - pipeGap - 50)
    --local pipeHeight =50
    table.insert(pipes, { x = screenWidth, y = pipeHeight })
end

function setup_window()
    GameEngine:setTitle("BreakOut")
    GameEngine:setWidth(800)
    GameEngine:setHeight(600)
    GameEngine:setFrameRate(60)
end

--- the set_keylist function
--- @return string
function set_keylist()
    return "R "
end

--- the start function
--- @return nil
function start()
    screenWidth = GameEngine:getWidth()
    screenHeight = GameEngine:getHeight()

    -- print(GameEngine:getRequest("https://dummyjson.com/c/3029-d29f-4014-9fb4"))
end

-- Update game state
function update()
    if not gameRunning then return end

    -- Apply gravity
    bird.velocity = bird.velocity + gravity
    bird.position.y = bird.position.y + bird.velocity

    -- Jumping mechanic
    if GameEngine:isKeyDown(" ") then
        bird.velocity = bird.jumpStrength
    end

    -- Move pipes
    for i = #pipes, 1, -1 do
        pipes[i].x = pipes[i].x - pipeSpeed

        -- Remove off-screen pipes
        if pipes[i].x + pipeWidth < 0 then
            table.remove(pipes, i)
            score = score + 1
        end
    end

    -- Collision detection
    for _, pipe in ipairs(pipes) do
        if bird.position.x < pipe.x + pipeWidth and bird.position.x + bird.width > pipe.x then
            if bird.position.y < pipe.y or bird.position.y + bird.height > pipe.y + pipeGap then
                gameRunning = false
            end
        end
    end

    -- Check if bird hits ground or ceiling
    if bird.position.y + bird.height >= screenHeight or bird.position.y <= 0 then
        gameRunning = false
    end

    -- Spawn pipes periodically
    if #pipes == 0 or pipes[#pipes].x < screenWidth - 200 then
        spawnPipe()
    end
end

-- Draw game elements
function draw()
    -- Clear screen
    GameEngine:fillScreen(Color.new(135, 206, 250)) -- Sky Blue background

    -- Draw bird
    GameEngine:setColor(Color.new(255, 255, 0)) -- Yellow
    GameEngine:fillOval(bird.position.x, bird.position.y, bird.width, bird.height)

    -- Draw pipes
    GameEngine:setColor(Color.new(0, 255, 0)) -- Green
    for _, pipe in ipairs(pipes) do
        GameEngine:fillRect(pipe.x, 0, pipeWidth, pipe.y)
        GameEngine:fillRect(pipe.x, pipe.y + pipeGap, pipeWidth, screenHeight - pipe.y - pipeGap)
    end

    -- Draw score
    GameEngine:setColor(Color.new(255, 255, 255)) -- White
    GameEngine:drawText(tostring(score), 10, 10)

    -- Game over message
    if not gameRunning then
        GameEngine:drawText("Game Over! Press R to restart", screenWidth / 2 - 80, screenHeight / 2)
    end
end

-- Restart function
function restart()
    bird.position.y = 200
    bird.velocity = 0
    pipes = {}
    score = 0
    gameRunning = true
end
