local sizeX, sizeY = term.getSize()
os.unloadAPI("sensorsUI")
os.loadAPI("/rom/apis/sensorsUI")
os.unloadAPI("sensors")
os.loadAPI("/rom/apis/sensors")
os.unloadAPI("sensorsData")
os.loadAPI("/rom/apis/sensorsData")

local arg = {...};



--[[
	Dashboard Parameters
]]--
-- sensor name
local sensor= arg[1] or "Sensor";
-- dashboard title
local title="Reactor Dashboard";

-- refresh timer
local refresh=2
-- probe name
--local probe="Reactor";


local side = sensors.getController()
local monside="none",mon ; --= sensorsUI.getMonitor();
local targets = sensors.getAvailableTargetsforProbe(side,sensor,"Reactor");
local target = targets[1]
local targets_mfsu = sensors.getAvailableTargetsforProbe(side,sensor,"EUStorage");
--[[
if #targets_mfsu>0 then
	sensorsUI.printPaged ("[Probe:"..p.. "] [Available Targets:"..#targets_mfsu.."]");
	for i,v in ipairs(targets_mfsu) do
		sensorsUI.printPaged ("  "..v)
	end
end
]]--







-- main program

if monside~="none" then
	sensorsUI.writeAt(1,sizeY,"Press Spacebar to exit")
	mon.setTextScale(1);
	term.redirect(mon);
end

local done=false;
local e = sensorsData.getProbe("ic2","reactor");
local eus =sensorsData.getProbe("ic2","eustorage");
local reactor = sensors.getSensorReadingAsDict(side,sensor,target,"Reactor")
local content = sensors.getSensorReadingAsDict(side,sensor,target,"ReactorContent")
--for i,v in pairs(content) do
--	print(v)
--end


local mfsu={};

for ind,tg in ipairs(targets_mfsu) do
	mfsu[ind] = sensors.getSensorReadingAsDict(side,sensor,targets_mfsu[ind],"EUStorage")
end
--local mfsu1 = sensors.getSensorReadingAsDict(side,sensor,targets_mfsu[1],"EUStorage")
local itemMap = sensors.getItemsInfo({"Coolant Cell","Uranium Cell","Integrated Heat Disperser","Integrated Reactor Plating","Ice","Water Bucket","Bucket"},content);



local timer1=os.startTimer(refresh)
local count=1;
local  progress={"-","\\","|","/"};
term.clear()
sensorsUI.writeAt(1,1,title)
if monside=="none" then
	sensorsUI.writeAt(1,sizeY,"Press Spacebar to exit")
end
local rate1=0;
local rate2=0;
local water_prev=itemMap["Water Bucket"].qty or 0;
local ice_prev=itemMap["Ice"].qty or 0;

while not done do
	sensorsUI.hr(2)
	sensorsUI.hRadio(sizeX-23,1,"Chambers",reactor[e.size.val],{1,2,3,4,5,6});
	sensorsUI.writeAt(1,3,e.energy.label.." : "..reactor[e.energy.val]);
	sensorsUI.hBar(1,4,10,e.heat.label,reactor[e.heat.val],reactor[e.size.val]*1000+10000);
	sensorsUI.vBar(sizeX-8,sizeY-1,12,"Fuel",itemMap["Uranium Cell"].qty*10000-itemMap["Uranium Cell"].dmg,itemMap["Uranium Cell"].qty*10000);
	
	sensorsUI.writeAt(1,6,"Reactor Components")
	sensorsUI.hBar(1,7,10,"Coolant Cells",itemMap["Coolant Cell"].qty*10000-itemMap["Coolant Cell"].dmg,itemMap["Coolant Cell"].qty*10000);
	sensorsUI.hBar(1,8,10,"Heat Dispersers",itemMap["Integrated Heat Disperser"].qty*10000-itemMap["Integrated Heat Disperser"].dmg,itemMap["Integrated Heat Disperser"].qty*10000);
	sensorsUI.hBar(1,9,10,"Reactor Plating",itemMap["Integrated Reactor Plating"].qty*10000-itemMap["Integrated Reactor Plating"].dmg,itemMap["Integrated Reactor Plating"].qty*10000);
	-- consumables
	local freeSlots=(reactor[e.size.val] +3 ) *6 - itemMap.total;
	-- totalSlots = itemMap.total
	-- empty buckets:  itemMap["Bucket"].qty
	-- water buckets:  itemMap["Water Bucket"].qty
	
	-- calc rate
	rate1 = itemMap["Water Bucket"].qty - water_prev ;
	water_prev = itemMap["Water Bucket"].qty;
	rate2 = itemMap["Ice"].qty - ice_prev;
	ice_prev=itemMap["Ice"].qty;
	
	sensorsUI.writeAt(1,10,"Water       :"..itemMap["Water Bucket"].qty.. "  ["..rate1.."/"..refresh.."sec]");
	sensorsUI.writeAt(1,11,"Ice         :"..itemMap["Ice"].qty.." ["..rate2.."/"..refresh.."sec]");

	--sensorsUI.writeAt(1,12,rate1.."  Empty: [Slots:"..freeSlots.."] [Buckets:"..itemMap["Bucket"].qty.."]")
	
	
	sensorsUI.writeAt(1,14,"EU Storage")
	for ind,tg in ipairs(targets_mfsu) do
		sensorsUI.hBar(1,14+ind,10,"MFSU-"..ind,mfsu[ind][eus.storage.val],mfsu[ind][eus.storage.maxval]);
	end

	
	evt,k = os.pullEvent()
	if evt=="timer" and k == timer1 then
		timer1=os.startTimer(refresh)
		-- update readings
		reactor = sensors.getSensorReadingAsDict(side,sensor,target,"Reactor")
		content = sensors.getSensorReadingAsDict(side,sensor,target,"ReactorContent")
		itemMap = sensors.getItemsInfo({"Coolant Cell","Uranium Cell","Integrated Heat Disperser","Integrated Reactor Plating","Ice","Water Bucket","Bucket"},content);
		for ind,tg in ipairs(targets_mfsu) do
			mfsu[ind] = sensors.getSensorReadingAsDict(side,sensor,targets_mfsu[ind],"EUStorage")
		end
		
	elseif evt=="key" and (k == 28 or k ==57) then
		done=true
	end
	
	sensorsUI.writeAt(string.len(title)+2,1,progress[count])
	count = count+1;
	if count>3 then count=1 end

end
term.restore();
term.setCursorPos(1,sizeY)

