--[[
Multi farm
]]--

wait_time = 1*60 -- wait N seconds in between runs
fuel_threshold = 5120 -- fuel from a stack of coal
gx = 14
gy = 14

tf_x = 12
tf_y = 12

function reset_run()
    
    tree_y = tx.state.placemarks.tf_home.y
    pumpkin_y = tx.state.placemarks.pf_home.y
    if tx.state.y == pumpkin_y then
        print('I am at the pumpkin farm level...')
        tx.goto_placemark('pf_elevator')
        tx.goto_placemark('tf_elevator')
        tx.goto_placemark('tf_home')
    elseif tx.state.y == tree_y then
        print('I am at the tree farm level...')
        tx.goto_placemark('tf_home')
    else 
        print('I am unsure about how to proceed... :(')
        print('I am at y '..tostring(tx.state.y)..', tree_y = '..tostring(tree_y)..', pumpkin_y = '..tostring(pumpkin_y))
        return false
    end
    return true
end

function refuel()
    local remaining = turtle.getFuelLevel()
    turtle.select(1)
    while remaining < fuel_threshold do
        turtle.suckUp()
        turtle.refuel(64)
    end
    remaining = turtle.getFuelLevel()
    print('Got '..remaining..' fuel remaining now')
end

function pre_pumpkin_run()
    if tx.state.placemarks.pf_home.y ~= tx.state.y then
        tx.goto_placemark('tf_elevator')
        tx.goto_placemark('pf_elevator')
    end
    tx.goto_placemark('pf_dropoff')
    for i=1,16 do
        ic = tx.getItemCount(i)
        if ic>0 then
            tx.select(i)
            tx.dropDown(i)
        end
    end
    tx.goto_placemark('pf_home')
    turtle.select(1)
    turtle.suckDown()
    turtle.dropDown(turtle.getItemCount(1)-1)
    print('Got pumpkin(s) in slot 1 now')
end

function post_pumpkin_run()
    -- if stuff in inv go to pf_dropoff safely
    --- drop off stuff except one pumkin
    -- go to pf home
    -- store pumkin
    
    tx.goto_placemark('pf_home')
    local have_junk = false
    for i=16,2,-1 do        
        tx.select(i)
        if tx.compareTo(1) then
            tx.dropDown(64)
        elseif tx.getItemCount(i) > 0 then
            have_junk = true
        end
    end
    tx.select(1)
    tx.dropDown(64)
    if have_junk then
        tx.goto_placemark('pf_dropoff')
        for i=2,16 do
            if tx.getItemCount(i) then
                tx.select(i)
                tx.dropDown(64)
            end
        end  
    end
end

function harvest_pumpkin()
    turtle.select(1)
    if turtle.compareDown(1) then
        turtle.digDown()
    end    
end

function pumpkin_run()
    pre_pumpkin_run()
    tx.goto_placemark('pf_start')
    tx.grid_move{gx=gx, gy=gy, hook=harvest_pumpkin}
    post_pumpkin_run()
end

function pre_tree_run()
    if tx.state.placemarks.tf_home.y ~= tx.state.y then
        tx.goto_placemark('pf_elevator')
        tx.goto_placemark('tf_elevator')
    end
    local at_dropoff = false
    for i=1,16 do
        ic = tx.getItemCount(i)
        if ic>0 then
            if not at_dropoff then
                tx.goto_placemark('tf_dropoff')
                at_dropoff = true
            end
            tx.select(i)
            tx.dropDown(ic)
        end
    end
    tx.goto_placemark('tf_home')
    refuel()
    -- get sapplings
    tx.select(1)
    tx.suckDown()   
    -- get logs
    tx.select(2)
    tx.suck()
    tx.drop(tx.getItemCount(2)-1)
end

function plant_sapling()
    tx.select(1)
    if tx.getItemCount(1) > 1 then
        tx.placeDown()
    end
    tx.suckDown()
end

function fell_tree()
    tx.select(2)
    if tx.compare() then
        print('detected tree, harvesting up')
        tx.placemark('bottom')
        --tx.select(2)
        while tx.compare() do
            tx.dig()
            --tx.select(1)
            if not tx.up() then break end
            --tx.select(2)
        end
        print('going back down')
        tx.goto_placemark('bottom')
        --tx.select(2)
        tx.down()
        tx.dig()

        if tx.getItemCount(1) > 1 then
            tx.select(1)
            tx.place()
        end       
        tx.up()
        if tx.getItemCount(1) > 1 then
            tx.select(1)
            tx.placeDown()
        end     
    else
        -- nothing to do
        return
    end
end

function post_tree_run()
    tx.goto_placemark('tf_home')
    local have_junk = false
    for i=16,3,-1 do        
        tx.select(i)
        if tx.compareTo(1) then
            tx.dropDown(64)
        elseif tx.compareTo(2) then
            tx.drop(64)
        elseif tx.getItemCount(i) > 0 then
            have_junk = true
        end
    end
    tx.select(2)
    tx.drop(64)         
    tx.select(1)
    tx.dropDown(64)
    refuel()
    print(have_junk)
    if have_junk then
        tx.goto_placemark('tf_dropoff')
        for i=3,16 do
            if tx.getItemCount(i) then
                tx.select(i)
                tx.dropDown(64)
            end
        end  
    end
end

function tree_run()
    pre_tree_run()
    tx.goto_placemark('tf_start')
    tx.grid_move{gx=tf_x, gy=tf_y, hook=plant_sapling, prefw_hook=fell_tree}
    post_tree_run()
end

function main()
    if tx.gps_init() and reset_run() then 
        while true do
            tree_run()
            pumpkin_run()
            --os.sleep(wait_time)
        end
    else
        print('failed to init')
        return
    end
end

--post_tree_run()
main()