local camera = require("camera")
local clicks = require("clicks")
local shrines = require("shrines")

local player = {}

player.position = {}
player.position.x = 0
player.position.y = 0

player.target = {}
player.target.x = 0
player.target.y = 0

local blocks

local music

local time = 0

fortunes = {
    "the most horrible thing that's ever happened to you";
    "tiny, flying somethings";
    "heart BEAT";
    "the slash of icons";
    "eyrie";
    "to be what you are";
    "to be what you are not";
    "best above the clouds";
    "storm feet of the earth";
    "blind bastille";
    "VERMILLION";
    "stibnite stab-mine-eyes";
    "alloy";
    "amalgam";
    "dendrite";
    "sing with your eyes";
    "not lost";
    "i am lost";
    "be a duck";
    "peat and prayers";
}

local function movement_allowed(x, y)
    for _, block in ipairs(blocks) do
        local collide_x = x > block.x and x < block.x + block.width
        local collide_y = y > block.y and y < block.y + block.height

        if collide_x and collide_y then
            return false
        end
    end

    return true
end

function love.load()
    blocks = require("blocks")
    blocks.shader = love.graphics.newShader([[
        extern number time;

        vec4 effect(vec4 color, Image texture, vec2 texture_coordinates, vec2 screen_coordinates) {
            return color + 0.2 * sin(time + screen_coordinates.x / 200) * vec4(0.2, 0, 0, 0);
        }
    ]])

    music = love.audio.newSource("Evening Fall - Harp.mp3")
    music:setLooping(true)
    music:play()
end

function love.keypressed(key, isrepeat)
    if key == "escape" then
        love.event.quit()
    end
end

function love.mousepressed(x, y, button)
    if button == "l" then
        x = x - love.graphics.getWidth() / 2
        y = y - love.graphics.getHeight() / 2
        x, y = camera:transform_point(x, y)

        if movement_allowed(x, y) then
            camera:focus(x, y)
            clicks:add(x, y)

            player.target.x = x
            player.target.y = y

            if love.math.random() > 4 / 5 then
                local new_title = fortunes[math.floor(love.math.random() * #fortunes) + 1]
                love.window.setTitle(new_title)
            end
        end
    end
end

function love.update(dt)
    time = time + dt

    camera:update(dt)
    clicks:update(dt)

    player.position.x = player.position.x + (player.target.x - player.position.x) * dt
    player.position.y = player.position.y + (player.target.y - player.position.y) * dt

    shrines:detect_player(player)
    shrines:update(dt)
end

function love.draw()
    love.graphics.setColor(0x05, 0x05, 0x05)
    for x = -10, 10 do
        for y = -10, 10 do
            love.graphics.rectangle("fill", x * love.graphics.getWidth() / 10 - 25, y * love.graphics.getHeight() / 10 - 25 + (time % 5) * love.graphics.getHeight() / 10, 50, 50)
        end
    end

    love.graphics.translate(love.graphics.getWidth() / 2, love.graphics.getHeight() / 2)

    love.graphics.push()
    love.graphics.scale(0.25, 0.25)
    camera:transform()

    love.graphics.pop()

    love.graphics.push()
    camera:transform()

    blocks.shader:send("time", time)
    love.graphics.setShader(blocks.shader)
    for _, block in ipairs(blocks) do
        love.graphics.setColor(0x0a, 0x0a, 0x0a)
        love.graphics.rectangle("fill", block.x, block.y, block.width, block.height)
    end

    love.graphics.setShader()
    shrines:draw()

    love.graphics.setColor(0xF1, 0xDD, 0xCF)
    love.graphics.circle("fill", player.position.x, player.position.y, 4)

    love.graphics.pop()

    camera:transform()
    clicks:draw()
end
