// Fusion Research Driver - Common Module - System Variable Tools
//
// Copyright 2021 Fusion Research, Inc.
// by Alexa Jade Fanti
//
// Comments:
//   This file provides tools for interacting with system variables.
//   It supports error reporting and special variants for booleans.
//
// Depends: 
//   fusion_stringutils.js
//   fusion_debugging.js
//   fusion_url.js   - for cover art URL resolution
//
//System.Print("Fusion Driver - Common Module - System Variable Tools loaded...\r\n");


// This writes a value to a system variable.
//
// Parameters:
//		name   - The name of the variable to write the value to.
//		value  - The value to place in the variable.
//      option - See RTI documentation...
//
function SysVar_Write(name, value, option)
{
	// Verify Parameters first...
	// If they didn't specify the name of the parameter, it's a critical error!
	if (name === undefined) {
		Debug_SysVars("SysVar_Write parameter 'name' is undefined!", g_DEBUG_LEVEL_CRITICAL);
		return false; // I can't do anything without the name of the variable!
	}
	if (value === undefined) {
		Debug_SysVars("SysVar_Write parameter 'value' is undefined!", g_DEBUG_LEVEL_CRITICAL);
		return false; // I can't do anything without the value to write!
	}

	// Read the variable
	try {
		if (option === undefined) {
			Debug_SysVars("SysVar_Write about to set '" + name + "' to '" + value + "'", g_DEBUG_LEVEL_TRACE);
			if (SystemVars.Write(name, value)) {
				Debug_SysVars("SysVar_Write set '" + name + "' to '" + value + "'", g_DEBUG_LEVEL_INFO);
				return true;
			} else {
				Debug_SysVars("SysVar_Write failed to set '" + name + "' to '" + value + "'", g_DEBUG_LEVEL_ERROR);
				return false;
			}
		} else {
			Debug_SysVars("SysVar_Write about to set '" + name + "' to '" + value + "' (option = " + option + ")", g_DEBUG_LEVEL_TRACE);
			if (SystemVars.Write(name, value, option)) {
				Debug_SysVars("SysVar_Write set '" + name + "' to '" + value + "' (option = " + option + ")", g_DEBUG_LEVEL_INFO);
				return true;
			} else {
				Debug_SysVars("SysVar_Write failed to set '" + name + "' to '" + value + "' (option = " + option + ")", g_DEBUG_LEVEL_ERROR);
				return false;
			}
		}
	} catch (err) {
		// I have no idea if (or when) this could be thrown!
		Debug_SysVars("SysVar_Write failed to set '" + name + "' to '" + value + "' with exception (" + err + ")", g_DEBUG_LEVEL_ERROR);
		return false;
	}
}


// This writes a boolean value to a system variable.
//
// Parameters:
//		name   - The name of the variable to write the value to.
//		value  - The boolean value to place in the variable.
function SysVar_WriteBool(name, value)
{
	return SysVar_Write(name, parseBool(value), 'BOOLEAN');
}


// This writes an integer value to a system variable.
//
// Parameters:
//		name   - The name of the variable to write the value to.
//		value  - The boolean value to place in the variable.
function SysVar_WriteInt(name, value)
{
	value = parseInt(value, 10);
	if (isNaN(value)) value = 0;
	return SysVar_Write(name, value);
}

// Depricated... we should NEVER need this!
// Instead we "System.ConvertFromUTF8" a UTF8 string coming in and then just display normally!
//function SysVar_WriteUTF8(name, value, option)
//{
//	try {
//		var utf8 = System.ConvertFromUTF8(value);
//		return SysVar_Write(name, utf8, option);
//	} catch (err) {
//		return SysVar_Write(name, value, option);
//	}
//}

function SysVar_WriteArt(name, value)
{
	Debug_SysVars("SysVar_WriteArt setting '" + name + "' to " + value, g_DEBUG_LEVEL_TRACE);

	var original_value = value; // preserve for later assessment!
	var options = 'IMGURL';

	// Could I force the load using additional (different) options?

	DebugArt("URL = " + value, g_DEBUG_LEVEL_TRACE);

	// If the URL is partial (incomplete) we need to complete it!
	value = GetURL_CompleteArt(value);
	// If we detect this is "now playing" we need to force the image component to reload...
	if (original_value.includes('music/current/cover.jpg')) {
		value = value + "&random=" + System.GetRandomInteger(10000, 99999);
		DebugArt("URL is for Now Playing. Modifying (to force reload)... URL = " + value, g_DEBUG_LEVEL_TRACE);
	}

	Debug_SysVars("SysVar_WriteArt set '" + name + "' to " + value, g_DEBUG_LEVEL_TRACE);
	return SysVar_Write(name, value, options);
}

function SysVar_Read(name)
{
	Debug_SysVars("SysVar_Read reading '" + name + "'", g_DEBUG_LEVEL_TRACE);
	var value = SystemVars.Read(name);
	if (value == null) {
		Debug_SysVars("SysVar_Read '" + name + "' is NULL", g_DEBUG_LEVEL_WARN);
	} else {
		Debug_SysVars("SysVar_Read '" + name + "' = '" + value + "'", g_DEBUG_LEVEL_INFO);
		return value;
	}
}

function SysVar_ReadStr(name, defaultValue)
{
	var value = SysVar_Read(name);
	if (value == null) {
		if (defaultValue === undefined) {
			Debug_SysVars("SysVar_ReadStr '" + name + "' cannot return defaultValue (it's undefined) so returning empty string", g_DEBUG_LEVEL_WARN);
			return "";
		} else {
			Debug_SysVars("SysVar_ReadStr '" + name + "' returning default of '" + defaultValue + "'", g_DEBUG_LEVEL_INFO);
			return defaultValue;
		}
	} else return value;
}

function SysVar_ReadInt(name, defaultValue)
{
	var value = SysVar_Read(name);
	if (value == null) {
		defaultValue = parseInt(defaultValue, 10);
		if (isNaN(defaultValue))  {
			Debug_SysVars("SysVar_ReadInt '" + name + "' cannot return defaultValue (it's not a number) so returning zero", g_DEBUG_LEVEL_WARN);
			return 0;
		} else {
			Debug_SysVars("SysVar_ReadInt '" + name + "' returning default of " + defaultValue, g_DEBUG_LEVEL_INFO);
			return defaultValue;
		}
	} else return value;
}

function SysVar_ReadBool(name, defaultValue)
{
	var value = SysVar_Read(name);
	if (value == null) {
		defaultValue = parseBool(defaultValue, false);
		if (defaultValue) {
			Debug_SysVars("SysVar_ReadBool '" + name + "' returning default of TRUE", g_DEBUG_LEVEL_INFO);
			return true;
		} else {
			Debug_SysVars("SysVar_ReadBool '" + name + "' returning default of FALSE", g_DEBUG_LEVEL_INFO);
			return false;
		}
	} else return value;
}
