--[[
In which groups to sort
- hybrids drones
- crap drones: drones with suboptimal specs
- top working drones: top specs + longest life
- top breeding drones: top specs + shortest life

- canonical pairs: on top working queen + drone stack per species

- ignoble princesses/queens
- pristine princesses/queens shortest
- pristine princesses/queens longest
- pristine princesses/queens rest

attributes:

temperatureTolerance: BOTH_3
effect: apiculture.effect.exploration
tolerantFlyer: false
fertility: 4
flowering: gui.maximum
humidityTolerance: BOTH_3
caveDwelling: true
lifespan: Longest
territory: 15,13,15
flowerProvider: Flowers


]]--

ser = textutils.serialize
tw, th = term.getSize()

function k2t(tab)
    local keyset={}
    local n=0

    for k,v in pairs(tab) do
      n=n+1
      keyset[n]=k
    end
    return keyset
end

function print_table(t)
    n = 1
    for k,v in pairs(t) do
        print(tostring(k)..': '..tostring(v))
        n = n + 1
        if n > th - 2 then
          print('press enter to continue,,,')
          read()
          n = 1
        end
    end    
end

function analyze_bees()
    local c = peripheral.wrap('bottom')
    local a = peripheral.wrap('top')
    local push_qty = 0
    local push_count = 0
    local is = c.getInventorySize()
    
    local analyzer_space = 0;
    for s = 1, 7 do
        bee_stack = a.getStackInSlot(s)
        if not bee_stack then
            analyzer_space = analyzer_space + 1
        end
    end
    print('There are '..analyzer_space..' input slots free in the analyzer')

    if analyzer_space == 0 then
        return 0
    end
    
    for i = 1,is do
        s = c.getStackInSlot(i)
        if s and s.beeInfo == nil then
            print('Item in slot '..i..' is not a bee?')
        elseif s and s.beeInfo.isAnalyzed == false then
            print('Trying to push bee in slot '..i)
            push_ok = c.pushItem('up', i, 64)
            if push_ok then
                push_qty = push_qty + push_ok 
                push_count = push_count + 1 
                turtle.dropUp()
                if push_count == analyzer_space then
                    return push_qty
                end       
            end
        end
    end
    return push_qty
end

function empty_analyzer()
    while turtle.suckUp() do
        print('Trying to transfer bee from analyzer...')
        while not turtle.drop() do
            print('Transfer chest full, waiting...')
            os.sleep(5.0)
        end
    end
end

function empty_chest()
    local c = peripheral.wrap('bottom')
    local a = peripheral.wrap('top')
    local is = c.getInventorySize()
    
    for i = 1,is do
        s = c.getStackInSlot(i)
        if s and s.beeInfo == nil then
            print('Item in slot '..i..' is not a bee?')
        elseif s and s.beeInfo.isAnalyzed == true then
            print('Trying to transfer bee in slot '..i)
            push_ok = c.pushItem('up', i, 64)
            while not turtle.drop() do
                print('Transfer chest full, waiting...')
                os.sleep(5.0)
            end
        end
    end
end

    
while true do
    empty_analyzer()
    empty_chest()
    aq = analyze_bees()
    print('Pushed '..aq..' bees into the analyzer')
    os.sleep(60.0)
end