function love.load()
    images = {}

    for imageIndex, image in ipairs({
        1, 2, 3, 4, 5, 6, 7, 8,
        'uncovered', 'covered_highlighted', 'covered',
        'flower', 'flag', 'question',
    }) do
        images[image] = love.graphics.newImage('images/'..image..'.png')
    end

    cellSize = 18

    gridXCount = 19
    gridYCount = 14

    grid = {}

    for y = 1, gridYCount do
        grid[y] = {}
        for x = 1, gridXCount do
            grid[y][x] = {
                flower = false,
            }
        end
    end
end

function love.update()
    selectedX = math.floor(love.mouse.getX() / cellSize) + 1
    selectedY = math.floor(love.mouse.getY() / cellSize) + 1

    if selectedX > gridXCount then
        selectedX = gridXCount
    end

    if selectedY > gridYCount then
        selectedY = gridYCount
    end
end

function love.mousereleased(mouseX, mouseY, button)
    if button == 2 then
        -- Temporary
        grid[selectedY][selectedX].flower =
            not grid[selectedY][selectedX].flower
    end
end

function love.draw()
    for y = 1, gridYCount do
        for x = 1, gridXCount do
            local function drawCell(image, x, y)
                love.graphics.draw(
                    image,
                    (x - 1) * cellSize, (y - 1) * cellSize
                )
            end

            if x == selectedX and y == selectedY then
                if love.mouse.isDown(1) then
                    drawCell(images.uncovered, x, y)
                else
                    drawCell(images.covered_highlighted, x, y)
                end
            else
                drawCell(images.covered, x, y)
            end

            if grid[y][x].flower then
                drawCell(images.flower, x, y)
            end
        end
    end
end