ecran=peripheral.wrap("back")
modem=peripheral.wrap("top")
chatbox=peripheral.wrap("left")


aucuneNouvelle=false

chatbox.say('reboot',9999,false,'Surveillance reacteur')

myWindow = window.create(ecran,26,1,4,1)
myWindow.setBackgroundColor(colors.black)
--myWindow.setColor(colors.yellow)

dernierTemps=10

modem.open(1)

function actu()
	while true do	
		local event, modemSide, senderChannel, 
		  replyChannel, message, senderDistance = os.pullEvent("modem_message")
		
		
		
		if message.input<message.output then
			ecran.setBackgroundColor(colors.red)
			pourc=-1*math.ceil(message.input/message.output*100)
			chatbox.say('Probleme d\'energie',9999,false,'Surveillance reacteur')
			aucuneNouvelle=false
		else
			ecran.setBackgroundColor(colors.green)
			pourc=math.ceil(message.output/message.input*100)
			if aucuneNouvelle then
				aucuneNouvelle=false
				chatbox.say('Retour ! '..pourc,9999,false,'Surveillance reacteur')
			end
		end
		
		
		
		ecran.clear()
		ecran.setCursorPos(1,2)
		ecran.write('Entrer :  '..math.ceil(message.input*4/1000)..'k RF/t')
		ecran.setCursorPos(1,3)
		ecran.write('Sortie :  '..math.ceil(message.output*4/1000)..'k RF/t '..pourc..'%')
		ecran.setCursorPos(1,4)	
		ecran.write('Stockage :  '..math.ceil((message.energy*4)/1000000000)..'G RF '..math.ceil(message.energy/message.maxEnergy*100)..'%')
		-- maxEnergy
		dernierTemps=10
		myWindow.clear()
		myWindow.setCursorPos(2,1)
		myWindow.write(dernierTemps)
		
	end
end
function timerC()
	while true do
		if dernierTemps>=0 then
			myWindow.clear()
			myWindow.setCursorPos(3,1)
			myWindow.write(dernierTemps)	
		elseif dernierTemps==-5 then
			chatbox.say('AUCUNE NOUVELLE DU REACTEUR !',9999,false,'Surveillance reacteur')
			chatbox.say('AUCUNE NOUVELLE DU REACTEUR !',9999,false,'Surveillance reacteur')
			aucuneNouvelle=true
		end
		dernierTemps=dernierTemps-1
		os.sleep(1)
	end
end
parallel.waitForAll(timerC,actu)