local stands={"front"}
local standsRed={"front"}
local monitors={"monitor_16"}
local mfe="mfe_5"
local mfegrabs={"up"}

local armors={
["empty"]={name="Empty",power=false},
--Nanosuit
[30178]={name="NanoSuit Helmet",power=true,slot=1},
[30177]={name="NanoSuit Bodyarmor",power=true,slot=2},
[30176]={name="NanoSuit Leggings",power=true,slot=3},
[30175]={name="NanoSuit Boots",power=true,slot=4},
--Quantinum
[30174]={name="QuantinumSuit Helmet",power=true,slot=1},
[30173]={name="QuantinumSuit Bodyarmor",power=true,slot=2},
[30172]={name="QuantinumSuit Leggings",power=true,slot=3},
[30171]={name="QuantinumSuit Boots",power=true,slot=4},
}

local function save(A,B) local file = fs.open(tostring(A),"w") file.write(B) file.close() end
local function saveT(A,B) save(A,textutils.serialize(B)) end
local function saveTH(A,B) save(A,string.gsub(textutils.serialize(B),"},","},\r\n")) end
local function get(A) local file = fs.open(tostring(A),"r") if not file then return false end local data = file.readAll() file.close() if data then return data end end
local function getT(A) local data = get(A) if data then data = textutils.unserialize(data) end if data then return data end end

local manymeta={
["__index"]=function (tab, key)

	return function(...)
		local ret
		local tArgs={...}
		for i,k in ipairs(tab) do
			if k[key] then ret={k[key](unpack(tArgs))} end
			if #ret>0 then break end
		end
		return unpack(ret)
	end

end,
["__call"]=function (tab, ...)
	local ret={}
	local tArgs={...}
	if #tArgs==0 then return end
	local key=table.remove(tArgs,1)
	for i,k in ipairs(tab) do
		if k[key] then ret[i]={k[key](unpack(tArgs))} end
	end
	return ret
end

}

local function wrapAll()
for i,k in ipairs(stands) do
	stands[i]= peripheral.wrap(k)
	if not stands[i] then error("Wrong stand side "..tostring(k)) end
end
setmetatable(stands,manymeta)

for i,k in ipairs(monitors) do
	monitors[i]= peripheral.wrap(k)
	if not monitors[i] then error("Wrong monitor side "..tostring(k)) end
end
setmetatable(monitors,manymeta)

mfe=peripheral.wrap(mfe)
if not mfe then error("Wrong mfe side") end

end

local function charge(A,B)
while mfe.getStackInSlot(1) do sleep(1) end
mfe.pullItemIntoSlot(B,A,1,1)
local tar=mfe.getStackInSlot(1)
if not tar then return end 
saveT("charging",{tar.id,A,B})
print("Charging ",armors[tar.id]["name"])
if tar then
	repeat
	sleep(1)
	tar=mfe.getStackInSlot(1)
	until not tar or tar.dmg<=1
end
mfe.pushItemIntoSlot(B,1,1,A)
fs.delete("charging")
end

local function paint(A,B,C)
B.setBackgroundColor(A)
B.clear()
end

wrapAll()
print("Armory Charger Activated")
for i,k in pairs(standsRed) do redstone.setOutput(k,true) end

if mfe.getStackInSlot(1) and fs.exists("charging") then
local saved=getT("charging")
local tar=mfe.getStackInSlot(1)
if tar.id==saved[1] then mfe.pushItemIntoSlot(saved[3],1,1,saved[2]) fs.delete("charging") end
end 

paint(colors.yellow,monitors)
while true do
for l,k in pairs(stands) do
	for i=1,k.getInventorySize() do
		local item=k.getStackInSlot(i)
		if item then
			local armor=armors[item.id]
				if armor and armor.power and item.dmg>1 then 
					paint(colors.red,monitors[l])
					redstone.setOutput(standsRed[l],false)
					charge(i,mfegrabs[l])
					redstone.setOutput(standsRed[l],true)
				end
		end
	end
	paint(colors.lime,monitors[l])
end

sleep(5)
end
