-- Player Interface (peripheral++)
local pi=peripheral.find("playerInterfacePPP")
local chat=peripheral.find("chatBox")
-- config coffre
pi.setOutputSide("NORTH")
local joueur='Adsl_Houba'
local moi=pi.getPlayerInv(joueur)

local items={}
-- Function chargement d'une variable sauvegarder
function charge(nom)
	file=fs.open(nom,"r")
	tmp=file.readAll()
	file.close()
	return tmp
end
-- Function de sauvegarde d'une variable
function memoW(nom,info)
	file=fs.open(nom,"w")
	file.write(info)
	file.close()
end
items=textutils.unserialize(charge("items"))

-- attente reponse joueur
function attente()
	while true do
		local event, player, arg = os.pullEvent("command") -- \
		if player==joueur then
			if arg[1]=='o' or  arg[1]=='n' or arg[1]=='q' then
				return arg[1]
			end
		end
	end
end
function viderItem(iName)
	for iv=0, 39 do
		local info=moi.getStackInSlot(iv)
		if type(info)=='table' then
			if iName==info.name then 
				moi.retrieveFromSlot(iv,info.amount)
			end
		end
	end
end



function vide()
	local listeQuestion={}
	local texteQuestion=''
	for iv=0, 39 do
		local info=moi.getStackInSlot(iv)
		if type(info)=='table' then
			if type(items[info.name])=='nil' then
				if type(listeQuestion[info.name])=='nil' then
					listeQuestion[info.name]=info.displayName
					if texteQuestion=='' then 
						texteQuestion=info.displayName
					else 
						texteQuestion=texteQuestion..', '..info.displayName
					end
				end
			elseif items[info.name]==false then
				moi.retrieveFromSlot(iv,info.amount)
			end
		end
	end
	if texteQuestion~='' then
		chat.tell(joueur,'Garder tous '..texteQuestion.. ' ? \o => OUI \n => NON \q => Question',9999,false,'Inventaire')
		texte=attente()
		if texte=='o' then
			table.foreach(listeQuestion, function(iName,data)
				items[iName]=true				
			end)				
			memoW("items",textutils.serialize(items))
		elseif texte=='n' then
			table.foreach(listeQuestion, function(iName,data)
				items[iName]=false
				viderItem(iName)
			end)				
			memoW("items",textutils.serialize(items))
		else
			table.foreach(listeQuestion, function(iName,nomFr)
				chat.tell(joueur,'Garder '..nomFr.. ' ? \o => OUI \n => NON',9999,false,'Inventaire')
				texte=attente()		
				if texte=='o' then
					items[iName]=true
				else 
					items[iName]=false
					viderItem(iName)
				end
				memoW("items",textutils.serialize(items))
			end)
		end
		
	end -- texteQuestion~=''
	chat.tell(joueur,'Inventaire clean',9999,false,'Inventaire')
end

while true do
	local event, player, arg = os.pullEvent("command") -- \vide
	if player==joueur then
		if arg[1]=='vide' then
			vide()
		elseif arg[1]=='ckoi' then
			local info=moi.getStackInSlot(arg[2]-1)
			chat.tell(joueur,arg[2]..' c\'est '..info.displayName.. ' CODE : '..info.name..' CONFIG : '..tostring(items[info.name]),9999,false,'Inventaire')
		elseif arg[1]=='garder' then
			local info=moi.getStackInSlot(arg[2]-1)
			chat.tell(joueur,'On garde '..info.displayName..' '..arg[2],9999,false,'Inventaire')
			items[info.name]=true
			memoW("items",textutils.serialize(items))
		elseif arg[1]=='ranger' then
			local info=moi.getStackInSlot(arg[2]-1)
			chat.tell(joueur,'On raznge '..info.displayName..' '..arg[2],9999,false,'Inventaire')
			items[info.name]=false
			memoW("items",textutils.serialize(items))
		end
	end
end