os.loadAPI("fichier")

compass=peripheral.find('compass')
comptage={}
typeComptage={}
typeData={}
curs=0
index=0
plusPetitX=99999
plusPetitY=99999
for k,ligne in pairs(fichier.block) do
	if plusPetitX>ligne[1] then
		plusPetitX=ligne[1]
	end
	if plusPetitY>ligne[3] then
		plusPetitY=ligne[3]
	end
	if typeComptage[ligne[4]..':'..ligne[5]]==nil then
		curs=curs+1
		typeComptage[ligne[4]..':'..ligne[5]]=curs
		index=curs
		typeData[index]={name=ligne[4],metadata=ligne[5]}
		comptage[index]=0
	else
		index=typeComptage[ligne[4]..':'..ligne[5]]
	end
	comptage[index]=comptage[index]+1
end
if plusPetitX~=0 or plusPetitY~=0 then
	print("Decallage de "..plusPetitX.." "..plusPetitY)
	read()
end

term.clear();
term.setCursorPos(1,1)
term.write("Afficher les blocks ? Y/N")
test=read()
if test=="Y" then
	for index,qte in pairs(comptage) do
		term.clear();
		term.setCursorPos(1,1)
		term.write(typeData[index].name..' '..typeData[index].metadata)
		term.setCursorPos(1,2)
		stack=math.floor(qte/64)
		term.write(stack.." Stacks "..(qte-(stack*64)).." blocs")
		read()
	end
end

function reaproAdd(chest,index,qteManquante)
	chest.condenseItems()
	stacks=chest.getAllStacks()
	for i=1, #stacks do
		info=stacks[i].all()
		if typeData[index].name==info.id and typeData[index].metadata==info.dmg then						
			chest.pushItemIntoSlot("UP",i,qteManquante,index)
			return
		end
	end	
	term.clear();
	term.setCursorPos(1,1)
	term.write(typeData[index].name.." "..typeData[index].metadata.." PAS TROUVER "..qteManquante)
	read()
	reaproAdd(chest,index,qteManquante)
	return
end
function reapro()
	
	local chest=peripheral.wrap("bottom")
	for index,qte in pairs(comptage) do
		if qte~=0 then
			info=turtle.getItemDetail(index)
			if info~=nil then
				if typeData[index].name==info.name and typeData[index].metadata==info.damage then
					qteAct=turtle.getItemCount(index)		
				else
					term.clear();
					term.setCursorPos(1,1)
					term.write("INDEX "..index.." n'est pas "..typeData[index].name.." "..typeData[index].metadata)
					read()
					qteAct=0
				end					
			else
				qteAct=0		
			end
			
			if qteAct~=comptage[index] then
				if comptage[index]>64 then
					qteManquante=64-qteAct
				else
					qteManquante=comptage[index]-qteAct
				end
				if qteManquante~=0 then
					reaproAdd(chest,index,qteManquante)
				end
			end
		end
	end
	while turtle.getFuelLimit()~=turtle.getFuelLevel() do
		infof("Attente fuel")
		os.sleep(1)
	end
end
function infof(text)
	term.clear();
	term.setCursorPos(1,1)
	print(text)
end
reapro()

local bx, by, bz=gps.locate(5)

function suivant()
	slot=typeComptage[fichier.block[indexS][4]..':'..fichier.block[indexS][5]]
	if turtle.getItemCount(slot)==0 then
		infof("Retour en cours")
		deplacement(-1,0,-1,false)
		reapro()
		return false
	else
		infof("Deplacement"..(fichier.block[indexS][1]).." "..(fichier.block[indexS][2]).." "..(fichier.block[indexS][3]))
		deplacement(tonumber(fichier.block[indexS][1])-plusPetitX,tonumber(fichier.block[indexS][2])+1,tonumber(fichier.block[indexS][3])-plusPetitY,true)
		turtle.select(slot)
		turtle.placeDown()
		comptage[slot]=comptage[slot]-1
		return true
	end
end
function deplacement(ox,oy,oz,aller)
	rx=ox+bx+1
	ry=oy+by
	rz=oz+bz+1
	while true do
		local x, y, z=gps.locate(5)
		local orientation=compass.getFacing();
		if aller and y~=ry then
			if y<ry then
				turtle.up()
			else
				turtle.down()
			end
		elseif x~=rx then
			if rx>x then
				if orientation=="east" then
					turtle.forward()
				elseif orientation=="west" then
					turtle.back()
				else
					turtle.turnLeft()
				end
			else 
			    if orientation=="east" then
					turtle.back()
				elseif orientation=="west" then
					turtle.forward()
				else
					turtle.turnLeft()
				end
			end
		elseif z~=rz then
			if rz>z then
				if orientation=="south" then
					turtle.forward()
				elseif orientation=="north" then
					turtle.back()
				else
					turtle.turnLeft()
				end
			else 
			    if orientation=="south" then
					turtle.back()
				elseif orientation=="north" then
					turtle.forward()
				else
					turtle.turnLeft()
				end
			end
		elseif not(aller) and y~=ry then
			if y<ry then
				turtle.up()
			else
				turtle.down()
			end
		else
			return
		end
	end
end

yActuel=-1
xPrec=0
zPrec=0
indexS=-1
while not(#fichier.block==0) do
	if fichier.block[1][2]==yActuel then
		recherche=true
		indexR=1
		indexS=1
		meilleur=99999
		while recherche do
			calc=math.abs(fichier.block[indexR][1]-xPrec)+math.abs(fichier.block[indexR][3]-zPrec)
			if calc<meilleur then
				indexS=indexR
				meilleur=calc
			end			
			indexR=indexR+1
			if fichier.block[indexR][2]~=yActuel then
				recherche=false
			end
		end
	else
		indexS=1
	end
	yActuel=fichier.block[indexS][2]
	xPrec=fichier.block[indexS][1]
	zPrec=fichier.block[indexS][3]
	
	reussi=suivant()
	if reussi then
		table.remove(fichier.block,indexS)
	end
end
