local turn = require("turn")
local reset = require("reset")
local drop = require("drop")
local sensor = require("sensor")
local back = require("back")
local color = require("color")
local take = require("take")

prevTime = 0
tasks = {
    { main = take, delay = 2, context = {} },
    { main = turn, delay = 0, context = { idx = 1, up = true } },
    { main = sensor, delay = 0, context = { idx = 1 } },
    { main = color, delay = 0, context = { idx = 1 } },
    { main = drop, delay = 0.5, context = {} },
    { main = back, delay = 5, context = {} },
    { main = reset, delay = 0, context = {} }
}

curr = 1
graph = createGraph()
expandTo(graph, 6)

function tick()
    drawGraph(graph)
    if curr > #tasks then
        return
    end

    if curr > 1 and tasks[curr - 1].delay > (os.clock() - prevTime) then
        return
    end

    local res = tasks[curr].main(tasks[curr].context)
    if res == true then
        prevTime = os.clock()
        curr = curr + 1
    end
end

lastIdx = 999
function touch(idx)
    if lastIdx ~= 999 then
        connect(graph, idx, lastIdx, 4)
        lastIdx = 999
    else
        lastIdx = idx
    end
end
