--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2021 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
title = "Master Signals Test"
require([[defines]]);
require([[functions]]);

local Count = 0;
-- These labels appear on each of the tabs for the Function Buttons
Tab01Label = "J";
Tab02Label = "K";
Tab03Label = "L";

ErrCount = 0;
--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	print("setup() Running");
	ErrCount = 0;
	print("Start Test @ ", RunTime());
	print("Showing the Layout Window");
	ShowLayout();
	--
	DeleteAllSignals();	-- clear the window
	
	CreateSignal(1, WB, ABSOLUTE_HEAD_BLOCK, 0, 0, "QB", AsDark, Hd3Light, 0);
	CreateSignal(2, EB, APPROACH_TO_HEAD_BLOCK, 100, 100, "BB", AsClear, Hd3Light, 11);
	
	Asp = GetAspect(2);
	print("Signal 2 Aspect (2) = ", Asp);
	if (Asp ~= AsClear) then ErrCount = ErrCount + 1; end;
	Asp = GetAspect(1);
	print("Signal 1 Aspect (0) = ", Asp);
	if (Asp ~= AsDark) then ErrCount = ErrCount + 1; end;	

	nam = GetSignalName(1);
	print("Signal 1 Name (QB) = ", nam);
	if (nam ~= "QB") then ErrCount = ErrCount + 1; end;
	
	nam, num = GetSignalName(22);
	print("Signal 22 Name (**) = ", nam);
	if (nam ~= "**") then ErrCount = ErrCount + 1; end;
	print("Signal 22 Number Plate (0) = ", num);
	if (num ~= 0) then ErrCount = ErrCount + 1; end;

	--print(ErrCount .. " Error(s)");
	
	Sleep(1);
	Aspect = SetAspect(1, AsApproach);
	if (Aspect ~= AsApproach) then ErrCount = ErrCount + 1; end;
	Sleep(1);
	Aspect = SetAspect(1, AsStopSignal);
	if (Aspect ~= AsStopSignal) then ErrCount = ErrCount + 1; end	
	Sleep(1);

	--print(ErrCount .. " Error(s)");
	
	DeleteSignal(2);	-- delete signal 2
	Sleep(3);
	CreateSignal(2, WB, PERMISSIVE_IN_OPEN_COUNTRY, 200, 200, "CC", AsApproach, 2, 3456);	-- recreate signal 2, one row down
	nam, num, ttype = GetSignalName(2);
	print("Signal 2 Name (CC) = ", nam);	
	if (nam ~= "CC") then ErrCount = ErrCount + 1; end;
	print("Signal 2 Number Plate (3456) = ", num);	
	if (num ~= 3456) then ErrCount = ErrCount + 1; end;
	print("Signal 2 type (", PERMISSIVE_IN_OPEN_COUNTRY, ") = ", ttype);	
	if (ttype ~= PERMISSIVE_IN_OPEN_COUNTRY) then ErrCount = ErrCount + 1; end;	
	Asp = GetAspect(2);
	print("Aspect 2 (", AsApproach, ") = ", Asp);
	if (Asp ~= AsApproach) then ErrCount = ErrCount + 1; end;

	-- test approach lighting
	CreateSignal(4, WB, PERMISSIVE_IN_OPEN_COUNTRY, 100, 200, "DA", -AsClear, 2, 33);	-- create signal 4, @ midpoint
	nam, num, ttype = GetSignalName(4);
	print("Signal 4 Name (DA) = ", nam);	
	if (nam ~= "DA") then ErrCount = ErrCount + 1; end;
	print("Signal 4 Number Plate (33) = ", num);	
	if (num ~= 33) then ErrCount = ErrCount + 1; end;
	print("Signal 4 type (", PERMISSIVE_IN_OPEN_COUNTRY, ") = ", ttype);	
	if (ttype ~= PERMISSIVE_IN_OPEN_COUNTRY) then ErrCount = ErrCount + 1; end;	
	Asp = GetAspect(4);
	print("Aspect 4 (", AsClear, ") = ", Asp);
	if (Asp ~= AsClear) then ErrCount = ErrCount + 1; end;

	print("Visually check that signal 4 is dark");
	
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
	-- Always call Sleep(X) at least once in this function if it returns true;
	print("loop() Running");
	Sleep(1);

	print("Almost Done...");
	return Stop("DONE");
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button or the Stop() function is called
	print("cleanup() Running");
	if (ErrCount == 0) then
		print("No errors");
		else
		print(ErrCount .. " Error(s)");
		end;	
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]

