// Fusion Research Driver - Common Module - Miscelaneous
//
// Copyright 2021 Fusion Research, Inc.
// by Alexa Jade Fanti
//
// Comments:
//   This file provides miscelaneous functions I didn't know where else to put.
//
// Depends: 
//   fusion_stringutils.js
//
//System.Print("Fusion Driver - Common Module - Miscelaneous loaded...\r\n");


// Read a value and determin if it's TRUE or FALSE.
// This assumes everything is FALSE exce[pt a few very specific things.
// As opposed to the Javascript function/object Boolean()...
function parseBool(value)
{
	if (value === undefined) return false;
	var str = value.toString();
	
	if (str.toLowerCase() == "true") return true;
	if (str.toLowerCase() == "yes") return true;
	if (str.toLowerCase() == "on") return true;
	if (str.toLowerCase() == "1") return true;
	return false;
}

// Somethings when we "split" things we get back from RTI we get problematic results...
// Arrays with empty strings in them (probably because they return a space instead of an empty string)
// To address that we can use this function to strip the resulting array down to only valid (not null, undefined or empty string values)
function CopyArray_RemoveNullUndefEmptyString(array)
{
	var result = new Array();
	var x = 0;
	while (x < array.length) {
		if ((array[x] !== null) && (array[x] !== undefined) && (array[x] !== '')) {
			result.push(array[x]);
		}
		x++;
	}
	return result;
}

// This is the best way to print an array?
function arrayToCSV(array)
{
	var result = '';
	var x = 0;
	while (x < array.length) {
		if (array[x] === null) {
			result = result + 'NULL';
		}
		if (array[x] === undefined) {
			result = result + 'UNDEFINED';
		}
		if (array[x] === '') {
			result = result + 'EMPTY_STRING';
		}
		result = result + array[x];
		if (x < array.length -1) {
			result = result + ',';
		}
		x++;
	}
	return result;
}




// This takes a time (in seconds) and formats it to be a pretty time 
// (mm:ss or h:mm:ss) for display to a user.
//
// Parameters:
//		seconds - the number of seconds
//
function SecondsToPrettyString_HMMSS(seconds)
{
	if (seconds === undefined) return "";
	
	try {
		var value = parseInt(seconds, 10);
		if (isNaN(value)) {
			DebugGeneral("SecondsToPrettyString_HMMSS parameter 'seconds' is not a number ('" + seconds + "'). Using 0!", g_DEBUG_LEVEL_WARN);
			value = 0;
		}
		if (value <= 0) return "";

		// multiply by 1000 because Date() requires miliseconds
		var date = new Date(value * 1000);
		var hh = date.getHours();
		var mm = date.getMinutes();
		var ss = date.getSeconds();
		
		var result = "";
		if (hh > 0) {
			result += hh + ':';
			if (mm >= 10) result += mm + ':'; else result += '0' + mm + ':';
			if (ss < 10) result += "0" + ss; else result += ss;
		} else {
			result += mm + ':';
			if (ss < 10) result += "0" + ss; else result += ss;
		}
		return result;
	} catch(err) {
		return "";
	}
}


    // System.Print("HMMSS undefined = " + SecondsToPrettyString_HMMSS());
    // System.Print("HMMSS ass = " + SecondsToPrettyString_HMMSS('ass'));
    // System.Print("HMMSS 0 = " + SecondsToPrettyString_HMMSS(0));
    // System.Print("HMMSS '0' = " + SecondsToPrettyString_HMMSS('0'));
    // System.Print("HMMSS 45 = " + SecondsToPrettyString_HMMSS(45));
    // System.Print("HMMSS '45' = " + SecondsToPrettyString_HMMSS('45'));
    // System.Print("HMMSS 250 = " + SecondsToPrettyString_HMMSS(250));
    // System.Print("HMMSS '250' = " + SecondsToPrettyString_HMMSS('250'));
    // System.Print("HMMSS 1450 = " + SecondsToPrettyString_HMMSS(1450));
    // System.Print("HMMSS '1450' = " + SecondsToPrettyString_HMMSS('1450'));
    // System.Print("HMMSS 3750 = " + SecondsToPrettyString_HMMSS(3750));
    // System.Print("HMMSS '3750' = " + SecondsToPrettyString_HMMSS('3750'));
    // System.Print("HMMSS 4950 = " + SecondsToPrettyString_HMMSS(4950));
    // System.Print("HMMSS '4950' = " + SecondsToPrettyString_HMMSS('4950'));

