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

© Copyright 2019 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
--[[---------------------------------------------------------------------------------------]]
-- Comments start with two dashes
--[[
Mulitline comments
start with two dashes and two left brackets.
They end with two right brackets. ]]

require("defines");

--[[
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 title = "Input Box Test"

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
--
local MyEngineNo;
print("setup : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug());
print(string.format("Run Time = %.2f seconds", RunTime()));
--[[---------------------------------------------------------------------------------------]]
--[==[
-- this test shows that empty indexes into a table with numeric indexes,
-- count when using the # (or luaL_len()) function
local t = {}
t[1] = 1
t[2] = 2
t[4] = 3
print(#t)	-- prints "4"
function tablelength(T)
   local count = 0
   for _ in pairs(T) do count = count + 1 end
   return count
end
print(tablelength(t))	-- prints "3"
]==]
--[[---------------------------------------------------------------------------------------]]


--[[--------------------------------------------]]
-- read Input
local EngineNameList = {};	-- table to hold all of the Engine numbers and Names
local EngineNameIndex = 1;
local EngineNameCount = 1;

EngineNameList[1] = "0 None";	-- use Index 1 for "None"
EngineNameCount = EngineNameCount + 1;

for iEng = 1, 99 do	-- check all possible engines on one TIU
	if (Exists(iEng)) then		
		local Name, PS;
		Name, PS = GetName(iEng);
		print(iEng .. " " .. Name .. " PS" .. PS);
		EngineNameList[iEng + 1] = iEng .. " " .. Name .. " PS" .. PS;
		if (Engine == iEng) then
			EngineNameIndex = EngineNameCount;
			print("EngineNameIndex = " .. EngineNameIndex);
			end;
		EngineNameCount = EngineNameCount + 1;
		end
	end
--
-- Ask the dispatcher for the engine number
--
--print("Select Engine");
local ResponseLine = InputDropDown(title, "Select Engine ", EngineNameIndex, EngineNameList);
print("ResponseLine = ", ResponseLine);	-- should print the engine selected
local typ = type(ResponseLine);
print("Type ", typ);

if (typ ~= "string" and typ ~= "boolean" and typ ~= "nil") then
	return Stop("InputDropDown did not return a string, boolean or nil");
	end;
if (typ == "boolean" and ResponseLine == false) then
	print("InputDropDown Canceled");
	ResponseLine = "InputDropDown Canceled";
	else
	if (ResponseLine ~= nil and ResponseLine ~= "") then	-- if the result is a string, then use first number in the string, otherwise, ignore it
		-- pull first integer found in the string
		local EngNo = string.match(ResponseLine, "%d+");
		if (EngNo ~= nil) then
			MyEngineNo = tonumber(EngNo);
			else
			return Stop("Invalid Engine Number");
			end;
		else
		if (typ == "nil") then
			-- nil returned only if parameters are invalid
			return Stop("Invalid Parameters to InputDropDown");
			else
			MyEngineNo = 1;
			end;
		end;	
	end;
--[[--------------------------------------------]]
print("Engine : " .. ResponseLine);
--[[---------------------------------------------------------------------------------------]]
-- read Input
ResponseLine = InputBox("Input Box", "Prompt", 25);
print("ResponseLine = ", ResponseLine);
typ = type(ResponseLine);
if (typ ~= "string" and typ ~= "nil") then
	return Stop("InputBox did not return a string or nil"); 	-- InputBox cannot return a boolean
	end;
if (typ ~= "nil") then
	print("Input Box Result : " .. ResponseLine);
	-- if user presses OK or CANCEL, the return value is 25
	if (ResponseLine ~= "25") then
		printc(clRed, [[Expected input is "25" unless you changed it]]);
		end;
	else
	-- nil returned only if parameters are invalid
	return Stop("Invalid Parameters to InputBox");
	end

--[[---------------------------------------------------------------------------------------]]
-- read Input
ResponseLine = InputQuery("Input Query", "Prompt", "Text Default Input");
print("ResponseLine = ", ResponseLine);
typ = type(ResponseLine);
if (typ ~= "string" and typ ~= "boolean" and typ ~= "nil") then
	return Stop("InputQuery did not return a string, boolean or nil");
	end;
if (typ == "boolean") then
	if (ResponseLine == false) then
		print("Input Query Canceled");
		end
	else
	if (typ == "nil") then
		-- nil returned only if parameters are invalid
		return Stop("Invalid Parameters to InputQuery");
		else
		if (typ == "string") then
			print("Input Query Result : " .. ResponseLine);
			if (ResponseLine ~= "Text Default Input") then
				printc(clRed, [[Expected input is "Text Default Input" unless you changed it]]);
				end;
			end
		end
	end
--[[---------------------------------------------------------------------------------------]]
return Stop(title .. " Complete");	-- end the script
end
--[[---------------------------------------------------------------------------------------]]
function tick(Engine, TIU)

return false;	-- false=stop calling tick(), true=continue to tick
end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
--
print(title .. " cleanup : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug());

return true;
end

