local stands={"jds_tileentityarmorstand_0","iron_1","jds_tileentityarmorstand_2"}
local standsRed={"left","front","right"}
local monitors={"monitor_14","top","monitor_15"}
local mfe="bottom"
local mfegrabs={"south","east","north"}

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},
--BackPacks
[30209]={name="Electric Jetpack",power=true,slot=2},
[30034]={name="Energypack",power=true,slot=2},
[30035]={name="Adv.Batpack",power=true,slot=2},
[30180]={name="Batpack",power=true,slot=2},
--NanoSaber
[30149]={name="Nano Saber",power=true},
--Nightvision
[30078]={name="Nightvision Googles",power=true},
--ic2 Tools
[30119]={name="Electric Hoe",power=true},
[30124]={name="Electric Treetap",power=true},
[30140]={name="Electric Wrench",power=true},
[30208]={name="Mining Laser",power=true},
[30219]={name="OV Scanner",power=true},
[30220]={name="OD Scanner",power=true},
[30233]={name="Chainsaw",power=true},
[30234]={name="Diamond Drill",power=true},
[30235]={name="Mining Drill",power=true},
[30244]={name="Iridium Drill",power=true},
[30076]={name="Obscurator",power=true},
--battery
[30242]={name="RE-Battery",power=true}, --Will not charge completly empty ones
[30036]={name="Advanced RE-Battery",power=true},
[30241]={name="Energy Clystal",power=true}, --Will not charge completly empty ones
--[[ --needs mfsu -- written for refference -- will use costum list
--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},
--battery
[30240]={name="Lapotron Clystal",power=true}, --Will not charge completly empty ones
--]]
}

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 tar.dmg<=1 or not tar
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
