-- Main LEDs



function AlarmFlash()
	if aflash then return end
	aflash = true
	while alarmpiezo do
		trans.AlarmLED.BrickColor = BrickColor.new("Really red")
		wait(0.1)
		trans.AlarmLED.BrickColor = BrickColor.new("Mid gray")
		wait(0.1)
	end	
	aflash = false
	if #system.ActiveAlarms:GetChildren() > 0 then trans.AlarmLED.BrickColor = BrickColor.new("Really red") end
end

function TroFlash()
	if tflash then return end
	tflash = true
	while tropiezo do
		trans.TroubleLED.BrickColor = BrickColor.new("Deep orange")
		wait(0.1)
		trans.TroubleLED.BrickColor = BrickColor.new("Mid gray")
		wait(0.1)
	end
	tflash = false
	if #system.Troubles:GetChildren() > 0 then trans.TroubleLED.BrickColor = BrickColor.new("Deep orange") end
end

function ResetLED()
	if system.Reset.Value == false then return end
	trans.Sounder.T3.Disabled = true
	trans.Sounder.TR.Disabled = true	
	alarmpiezo = false
	tropiezo = false
	while system.Reset.Value == true do
	trans.AlarmLED.BrickColor = BrickColor.new("Really red")
	trans.TroubleLED.BrickColor = BrickColor.new("Deep orange")		
	trans.SilencedLED.BrickColor = BrickColor.new("Deep orange")		
		wait()
	end
	trans.AlarmLED.BrickColor = BrickColor.new("Mid gray")
	trans.TroubleLED.BrickColor = BrickColor.new("Mid gray")
	trans.SilencedLED.BrickColor = BrickColor.new("Mid gray")
	CheckForAlarmAck()
	CheckForTroAck()
end

system.Reset.Changed:connect(ResetLED)

function CheckForAlarmAck()
	local af = system.ActiveAlarms:GetChildren()
	local aack = true
	for i = 1, #af do
		if af[i]:findFirstChild("Ack")==nil then aack = false end
	end
	if aack then
		alarmpiezo = false
	else
		alarmpiezo = true		
	end
	if alarmpiezo then
		trans.Sounder.T3.Disabled = false
		trans.Sounder.TR.Disabled = true
	elseif tropiezo then
		trans.Sounder.T3.Disabled = true
		trans.Sounder.TR.Disabled = false
	elseif alarmpiezo == false and tropiezo == false then		
		trans.Sounder.T3.Disabled = true
		trans.Sounder.TR.Disabled = true		
	end
	AlarmFlash()
	TroFlash()
end

function CheckForTroAck()
	local tf = system.Troubles:GetChildren()
	local tack = true
	for i = 1, #tf do
		if tf[i]:findFirstChild("Ack")==nil then tack = false end
	end
	if tack then
		tropiezo = false
	else
		tropiezo = true		
	end
	if alarmpiezo then
		trans.Sounder.T3.Disabled = false
		trans.Sounder.TR.Disabled = true
	elseif tropiezo then
		trans.Sounder.T3.Disabled = true
		trans.Sounder.TR.Disabled = false
	elseif alarmpiezo == false and tropiezo == false then		
		trans.Sounder.T3.Disabled = true
		trans.Sounder.TR.Disabled = true		
	end
	AlarmFlash()
	TroFlash()
	if #tf==0 then trans.TroubleLED.BrickColor = BrickColor.new("Mid gray") end
end

function NewAlarm(na)
	alarmpiezo = true
	na.ChildAdded:connect(function() CheckForAlarmAck() end)
	if alarmpiezo then
		trans.Sounder.T3.Disabled = false
		trans.Sounder.TR.Disabled = true
	elseif tropiezo then
		trans.Sounder.T3.Disabled = true
		trans.Sounder.TR.Disabled = false
	elseif alarmpiezo == false and tropiezo == false then		
		trans.Sounder.T3.Disabled = true
		trans.Sounder.TR.Disabled = true		
	end
	AlarmFlash()
	TroFlash()
end

system.ActiveAlarms.ChildAdded:connect(NewAlarm)

system.ActiveAlarms.ChildRemoved:connect(CheckForAlarmAck)

function NewTrouble(nt)
	tropiezo = true
	nt.ChildAdded:connect(function() CheckForTroAck() end)
	if alarmpiezo then
		trans.Sounder.T3.Disabled = false
		trans.Sounder.TR.Disabled = true
	elseif tropiezo then
		trans.Sounder.T3.Disabled = true
		trans.Sounder.TR.Disabled = false
	elseif alarmpiezo == false and tropiezo == false then		
		trans.Sounder.T3.Disabled = true
		trans.Sounder.TR.Disabled = true		
	end
	AlarmFlash()
	TroFlash()
end

system.Troubles.ChildAdded:connect(NewTrouble)

system.Troubles.ChildRemoved:connect(CheckForTroAck)

system.Silence.Changed:connect(function()
	if system.Silence.Value == true then
		trans.SilencedLED.BrickColor = BrickColor.new("Deep orange")	
	else
		trans.SilencedLED.BrickColor = BrickColor.new("Mid gray")			
	end
end)