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

© Copyright 2020 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 = "Occupancy Display"
require([[defines]]);
require([[functions]]);

--[[
When using this module, all variables (including functions!) must be declared through 
a regular assignment (even assigning nil will do) in a strict scope before being used 
anywhere or assigned to inside a nested scope.
https://github.com/lua-stdlib/strict
]]
local _ENV = require 'std.strict' (_G)	-- strict.lua will detect undeclared global variables
local _;
--
--[[---------------------------------------------------------------------------------------]]
--
local Layout4;
local MyTIUNo, MyEngineNo;
--
local LAYOUTFLAG = 100;
-- These labels appear on each of the tabs for the Function Buttons
Tab01Label = "Flags1";
Tab02Label = "Flags2";
Tab03Label = "Flags3";
--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
print("setup() Running");
-- Instrumented Layout 4
Layout4 = require([[Layout4]]);		-- All of the details about the Layout
-- an undirected weighted double vertex graph
MyTIUNo = Layout4.MASTER_TIU;
MyEngineNo = Engine;
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;
Sleep(4);
return true;
end
--]]
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
-- this function is called once when the user presses the [STOP] button or the Stop() function is called
print("Done");
return true;		-- false=cleanup failed, true=cleanup succeeded
end
--[[---------------------------------------------------------------------------------------]]
function ShowButton(Eng, TIU)
local flag;
printc(clGreen, "Occupancy Flags -------------------------");
for x = FIRST_BLOCK, LAST_BLOCK do
	flag = GetFlag(x);
	local FColor;
	if (flag == 0) then
		FColor = clBlack;
		else
		FColor = clRed;
		end;
	printc(FColor, BlockName[x] .. " = ", flag, " [" .. x .. "]")
	end;
return true;
end
Function01Name, Function01Label = ShowButton, "Show Flags";
--[[---------------------------------------------------------------------------------------]]
function InitButton(Eng, TIU)
print("Clear Occupancy Flags");
-- clear occupancy flags
local x;
for x = FIRST_BLOCK, LAST_BLOCK do
	OverrideOccupancy(x, 0);
	end;
print("Occupancy Flags cleared");
return true;
end
Function06Name, Function06Label = InitButton, "Clear Flags";
--[[---------------------------------------------------------------------------------------]]
function YButton()
local value = LAYOUTFLAG;
local block = YOUNGSTOWN_BLOCK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;
end
Function02Name, Function02Label = YButton, "Youngstown Block";
--[[---------------------------------------------------------------------------------------]]
function NCButton()
local value = LAYOUTFLAG;
local block = NEW_CASTLE_BLOCK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;
end
Function03Name, Function03Label = NCButton, "New Castle Block";
--[[---------------------------------------------------------------------------------------]]

function JPassButton()
local value = LAYOUTFLAG;
local block = J_L_PASSING_TRACK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;
end
Function04Name, Function04Label = JPassButton, "J&L Passing";
--[[---------------------------------------------------------------------------------------]]
function JThruButton()
local value = LAYOUTFLAG;
local block = J_L_THRU_TRACK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;
end
Function05Name, Function05Label = JThruButton, "J&L Thru";
--[[---------------------------------------------------------------------------------------]]

function SPassButton()
local value = LAYOUTFLAG;
local block = STRUTHERS_STATION_TRACK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;
end
Function07Name, Function07Label = SPassButton, "Struthers Station";
--[[---------------------------------------------------------------------------------------]]

function SThruButton()
local value = LAYOUTFLAG;
local block = STRUTHERS_THRU_TRACK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;
end
Function08Name, Function08Label = SThruButton, "Struthers Thru";
--[[---------------------------------------------------------------------------------------]]


function CPassButton()
local value = LAYOUTFLAG;
local block = COLLEGE_STATION_TRACK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;
end
Function09Name, Function09Label = CPassButton, "College Station";
--[[---------------------------------------------------------------------------------------]]

function CThruButton()
local value = LAYOUTFLAG;
local block = COLLEGE_THRU_TRACK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;
end
Function10Name, Function10Label = CThruButton, "College Thru";
--[[---------------------------------------------------------------------------------------]]

function McKRButton()
local value = LAYOUTFLAG;
local block = MCKEES_ROCKS_BLOCK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;
end
Function11Name, Function11Label = McKRButton, "McKees Rocks Block";
--[[---------------------------------------------------------------------------------------]]

function AButton()
local value = LAYOUTFLAG;
local block = ALIQUIPPA_BLOCK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;	
end
Function12Name, Function12Label = AButton, "Aliquippa Block";
--[[---------------------------------------------------------------------------------------]]

function BFButton()
local value = LAYOUTFLAG;
local block = BEAVER_FALLS_BLOCK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;	
end
Function13Name, Function13Label = BFButton, "Beaver Falls Block";
--[[---------------------------------------------------------------------------------------]]

function MRYButton()
local value = LAYOUTFLAG;
local block = MCKEES_ROCKS_REPAIR_SHOPS_YARD_BLOCK;
if (GetFlag(block) == 0) then
	print("Set " .. BlockName[block] .. " Occupancy");
	if (not TrySetFlag(block, value)) then
		print("Can't occupy " .. BlockName[block]);
		return true;
		end;
	print("Set occupancy flag on " .. BlockName[block] .. " to " .. value, " [" .. block .. "]");
	else
	SetFlag(block, 0);
	print("Clear occupancy flag on " .. BlockName[block]);
	end;
return true;	
end
Function14Name, Function14Label = MRYButton, "McKess Rocks Yard";
--[[---------------------------------------------------------------------------------------]]