// Fusion Research Driver - Common Module - System Event Tools
//
// Copyright 2021 Fusion Research, Inc.
// by Alexa Jade Fanti
//
// Comments:
//   This file provides tools for signaling events to the system.
//
//   Note: This depends on ALL events specific to a player being defined 
//   in the SystemEvents.xml in a very specific way. They MUST be tagged 
//   as follows:
//      SysEvent_Player?_<name>
//   
//   Where the ? is a player number (zero based index) and the <name> is 
//   the name of the event. For example:
//      <event name="Play"   tag="SysEvent_Player0_TransportPlay" />
//      <event name="Pause"  tag="SysEvent_Player1_TransportPause" />
//
//  This way we can construct the full name from just the partial name
//  and the player index!
//
// Depends: 
//   fusion_stringutils.js
//   fusion_debugging.js
//
//System.Print("Fusion Driver - Common Module - System Event Tools loaded...\r\n");


// This signals a system event (any)
//
// Parameters:
//      name - The name of the event to signal
//
function SysEvent_Signal(event)
{
	// Verify Parameters first...
	if (event === undefined) {
		Debug_SysEvents("SysEvent_Signal parameter 'event' is undefined!", g_DEBUG_LEVEL_CRITICAL);
		return false; // I can't do anything without the event name!
	}
	if (event == "") {
		Debug_SysEvents("SysEvent_Signal parameter 'event' is empty!", g_DEBUG_LEVEL_CRITICAL);
		return false;
	}

	try {
		Debug_SysEvents("SysEvent_Signal Signaling '" + event + "'...", g_DEBUG_LEVEL_TRACE);
		System.SignalEvent(event);
		Debug_SysEvents("SysEvent_Signal Event '" + event + "' was signaled", g_DEBUG_LEVEL_INFO);
	} catch(err) {
		Debug_SysEvents("SysEvent_Signal Unable to signal '" + event + "'. Error = " + err, g_DEBUG_LEVEL_ERROR);
	}
}


// This signals a system event specific to a player
//
// Parameters:
//      name        - The name of the event to signal (without the "SysEvent_Player#_" prefix!)
//      playerIndex - The zero based index of the player as defined by the integrator!
//
function SysEvent_Signal_Player(name, playerIndex)
{
	// Verify Parameters first...
	if (name === undefined) {
		Debug_SysEvents("SysEvent_Signal_Player parameter 'name' is undefined!", g_DEBUG_LEVEL_CRITICAL);
		return false; // I can't do anything without the event name!
	}
	if (name == "") {
		Debug_SysEvents("SysEvent_Signal_Player parameter 'name' is empty!", g_DEBUG_LEVEL_CRITICAL);
		return false;
	}

	if (playerIndex === undefined) {
		Debug_SysEvents("SysEvent_Signal_Player parameter 'playerIndex' is undefined!", g_DEBUG_LEVEL_CRITICAL);
		return false; // I can't do anything without the player index!
	}
	playerIndex = parseInt(playerIndex, 10);
	if (isNaN(playerIndex)) {
		Debug_SysEvents("SysEvent_Signal_Player parameter 'playerIndex' is not a number!", g_DEBUG_LEVEL_CRITICAL);
		return false; // I can't do anything without the player index!
	}
	
	SysEvent_Signal("SysEvent_Player" + playerIndex + "_" + name);
}

