local modNuit='off'
local ecran=peripheral.wrap("monitor_5")
ecran.setTextScale(4)
ecran.setBackgroundColor(colors.green)
albert.reds("bottom",colors.green,false)
local encours=false
local config={
	debutNuit=19,
	finNuit=6,
	preTime={
		nuit=20,
		matin=5,
		soir=18,
		midi=12
	}
}

function calcul(debut,jusqua,letime)
	if debut>jusqua then
		print(letime..'>'..debut..' or '..letime..'<'..jusqua)
		return letime>debut or letime<jusqua  
	else 
		print(letime..'>'..debut..' and '..letime..'<'..jusqua)
		return letime>debut and letime<jusqua 
	end
end
function avanceTemps(jusqua,ignoreModNuit)
	-- securite une seul modification temporel
	if encours==false then
		encours=true
		
		-- Enregistrement du debut
		letime=os.time()
		debut=letime-0.1
		
		ecran.setBackgroundColor(colors.yellow)
		chatbox.say('J\'avance le temps jusqu\'a '..jusqua..'h')
		particule.couleur(255,200,0);
		albert.reds("bottom",colors.green,true)
		while ( calcul(debut,jusqua,letime) and (ignoreModNuit or modNuit=='off') ) do		
			os.sleep(1)
			letime=os.time()
		end
		albert.reds("bottom",colors.green,false)
		ecran.setBackgroundColor(colors.green)
		particule.couleur(255,0,0);
		encours=false
	else
		chatbox.say('Je modifie deja le temps')	
	end
end
function horloge()
	while true do
		ecran.clear()
		ecran.setCursorPos(1,1)
		local temp=os.time()
		local heure=math.floor(temp)
		local minute=math.floor((temp-heure)*60)
		if minute<10 then
			minute='0'..minute
		end
		if heure<10 then
			heure='0'..heure
		end
		ecran.write(heure..'h'..minute)
		os.sleep(1)
	end
end
function gestionTemps()
	while true do		
		if modNuit=='off' then
			letime=os.time()
			if letime>config.debutNuit or letime<config.finNuit then
				avanceTemps(config.finNuit,false)
			end
		end
		os.sleep(20)
	end
end
function nuitFc(args,player)
	if args[2]=='on' then
		modNuit='on'
		chatbox.say('Je laisserez la nuit')	
	elseif args[2]=='off' then
		modNuit='off'
		chatbox.say('Je passerai les nuits')	
	else
		if modNuit=='off' then
			chatbox.say('Je passe les nuits')	
		else
			chatbox.say('Je laisse les nuits')	
		end
	end
end
function timeFc(args,player)
	verChiffre=tonumber(args[2])
	if verChiffre==nil then
		if config.preTime[args[2]] then
			avanceTemps(config.preTime[args[2]],true)
		else
			liste=''
			table.foreach(config.preTime,function(nom,temp)
				liste=liste..' '..nom
			end)
			chatbox.say('Je ne connais pas ce moment de la journé, vous pouvez choisir : '..liste)	
		end
	else
		avanceTemps(verChiffre,true)
	end		
end
chatbox.add("nuit",nuitFc,"\\nuit on|off - Change la config pour autorisé la nuit")
chatbox.add("time",timeFc,"\\time 0|NOM - Je modifie le temps jusqu'au moment demander")


function init()
	parallel.waitForAll(gestionTemps,horloge)
end