// Fusion Research Driver - Common Module - Debugging
//
// Copyright 2021 Fusion Research, Inc.
// by Alexa Jade Fanti
//
// Comments:
//   This file is used to handle debugging of drivers.
//
//   It depends on the driver configuration (ConfigSettings.xml).
//   In the driver's configuration we have debug settings which set "levels" 
//   of debugging for each type of thing we'd like to debug.
//   
//   We define different debugging levels and types of things to debug
//   and use these settings to determine what gets output.
//
// Depends: fusion_stringutils.js
//
//System.Print("Fusion Driver -Common Module - Debugging loaded...\r\n");


//
//  Global Debugging Variables
//

// These are the levels of reporting required by the user/driver 
// configuration for each type of debugging we support
var g_DEBUG_GENERAL         = Config.Get("Debug_General");
var g_DEBUG_HTTP            = Config.Get("Debug_HTTP");
var g_DEBUG_KEYBOARD        = Config.Get("Debug_Keyboard")
var g_DEBUG_FEEDBACK        = Config.Get("Debug_Feedback");
var g_DEBUG_URL             = Config.Get("Debug_URL");
var g_DEBUG_ART             = Config.Get("Debug_Art");
var g_DEBUG_FAVORITE_TOOL   = Config.Get("Debug_FavoriteTool");
var g_DEBUG_SYNC_TOOL       = Config.Get("Debug_SyncTool");
var g_DEBUG_VIEW_PLAYER_MAP = Config.Get("Debug_ViewPlayerMap")
var g_DEBUG_SERVER          = Config.Get("Debug_Server")
var g_DEBUG_SERVER_DISCOVER = Config.Get("Debug_ServerDiscovery")
var g_DEBUG_LIBRARY         = Config.Get("Debug_Library")
var g_DEBUG_QUEUE           = Config.Get("Debug_Queue")
var g_DEBUG_QUEUE_PROGRESS  = Config.Get("Debug_QueueProgress")
var g_DEBUG_PERFORMANCE     = Config.Get("Debug_Performance")

var g_DEBUG_SYS_CONFIG    = Config.Get("Debug_SysConfig");
var g_DEBUG_SYS_FUNCTIONS = Config.Get("Debug_SysFuncs");
var g_DEBUG_SYS_VARIABLES = Config.Get("Debug_SysVars");
var g_DEBUG_SYS_EVENTS    = Config.Get("Debug_SysEvents");

// These are used in ALL debugging calls to indicate the level of the debug report!
var g_DEBUG_LEVEL_CRITICAL = 0;
var g_DEBUG_LEVEL_ERROR    = 1;
var g_DEBUG_LEVEL_WARN     = 2;
var g_DEBUG_LEVEL_INFO     = 3;
var g_DEBUG_LEVEL_TRACE    = 4;
// Note: These are CONSTANTS (even though we can't dclare them as such)!!!

// We'd like to report the debug level in our output, so this helps with that.
function  DebugLevel_AsString(level)
{
	switch(level) {
		case g_DEBUG_LEVEL_CRITICAL:
			return "CRITICAL";
			break;
		case g_DEBUG_LEVEL_ERROR:
			return "ERROR";
			break;
		case g_DEBUG_LEVEL_WARN:
			return "WARNING";
			break;
		case g_DEBUG_LEVEL_INFO:
			return "INFO";
			break;
		case g_DEBUG_LEVEL_TRACE:
			return "TRACE";
			break;
		default: return "UNKNOWN";
	}
}

// These are the debugging functions for each of the types of debugging we are required to do.
// Note: They require both a message AND a debug level from one of the constants defined above!
function  InternalDebug(kind, message, debug_level)
{
	if (kind === undefined) return;
	if (message === undefined) return;
	if (debug_level === undefined) return;
	
	if (message.includes("\r") || message.includes("\n")) {
		System.PrintMultiline(kind + ' ' + DebugLevel_AsString(debug_level) + ': ' + message + "\r\n");
	} else {
		System.Print(kind + ' ' + DebugLevel_AsString(debug_level) + ': ' + message + "\r\n");
	}
}

function  DebugGeneral(message, debug_level)
{
	if (debug_level <= g_DEBUG_GENERAL) InternalDebug('General', message, debug_level);
}

function  DebugHTTP(message, debug_level)
{
	if (debug_level <= g_DEBUG_HTTP) InternalDebug('Http', message, debug_level);
}

function  DebugKeyboard(message, debug_level)
{
	if (debug_level <= g_DEBUG_KEYBOARD) InternalDebug('Keyboard', message, debug_level);
}

function  DebugFeedback(message, debug_level)
{
	if (debug_level <= g_DEBUG_FEEDBACK) InternalDebug('Feedback', message, debug_level);
}

function  DebugURL(message, debug_level)
{
	if (debug_level <= g_DEBUG_URL) InternalDebug('Url', message, debug_level);
}

function  DebugArt(message, debug_level)
{
	if (debug_level <= g_DEBUG_ART) InternalDebug('Art', message, debug_level);
}

function  DebugFavTool(message, debug_level)
{
	if (debug_level <= g_DEBUG_FAVORITE_TOOL) InternalDebug('Favorite Tool', message, debug_level);
}

function  DebugSyncTool(message, debug_level)
{
	if (debug_level <= g_DEBUG_SYNC_TOOL) InternalDebug('Sync Tool', message, debug_level);
}

function  DebugViewPlayerMap(message, debug_level)
{
	if (debug_level <= g_DEBUG_VIEW_PLAYER_MAP) InternalDebug('View-Player Map', message, debug_level);
}

function  DebugServer(message, debug_level)
{
	if (debug_level <= g_DEBUG_SERVER) InternalDebug('Server', message, debug_level);
}

function  DebugServerDiscovery(message, debug_level)
{
	if (debug_level <= g_DEBUG_SERVER_DISCOVER) InternalDebug('Server Discovery', message, debug_level);
}

function  DebugLibrary(message, debug_level)
{
	if (debug_level <= g_DEBUG_LIBRARY) InternalDebug('Library', message, debug_level);
}

function  DebugQueue(message, debug_level)
{
	if (debug_level <= g_DEBUG_QUEUE) InternalDebug('Queue', message, debug_level);
}

function  DebugQueueProgress(message, debug_level)
{
	if (debug_level <= g_DEBUG_QUEUE_PROGRESS) InternalDebug('Queue Progress', message, debug_level);
}

function  DebugPerformance(message, debug_level)
{
	if (debug_level <= g_DEBUG_PERFORMANCE) InternalDebug('Performance', message, debug_level);
}

function  Debug_SysConfig(message, debug_level)
{
	if (debug_level <= g_DEBUG_SYS_CONFIG) InternalDebug('SysConfig', message, debug_level);
}


function  Debug_SysFuncs(message, debug_level)
{
	if (debug_level <= g_DEBUG_SYS_FUNCTIONS) InternalDebug('SysFunc', message, debug_level);
}

function  Debug_SysVars(message, debug_level)
{
	if (debug_level <= g_DEBUG_SYS_VARIABLES) InternalDebug('SysVar', message, debug_level);
}

function  Debug_SysEvents(message, debug_level)
{
	if (debug_level <= g_DEBUG_SYS_EVENTS) InternalDebug('SysEvent', message, debug_level);
}
