// Fusion Research Driver - Music Module - URL Utilites
//
// Copyright 2013-2021 Fusion Research, Inc.
// by Alexa Jade Fanti
//
// Comments:
//   This provides utilities for creating URLs for making HTTP requests of the music server.
//
// Depends: 
//   fusion_debugging.js
//   fusion_stringutils.js
//   fusion_sys_config.js
//   fusion_serverdiscovery.js
//
//System.Print("Fusion Driver - Music Module - URL Utilites loaded...\r\n");


//*************** START  Utilities ***************
// This utility looks up the server's address and builds the "start" of all server URLs. 
// They all look like : http://serveraddress:9720/... We do this so we don't have to lookup 
// the Server Address and TCP Config Port every time we create a URL because they are stored 
// in the RTI driver configuration (specified in ConfigSettings.xml) and we are always going 
// to read them from there on demand in case they are dynamically changable.
//
// Parameters - None (this reads from the RTI Driver Configuration)
//
// Returns - A partial URL ready for further processing (or an empty string).
//
// Note: The result does NOT end in a URL path (/) character.
//
function GetURL_ServerPrefix()
{
	var addr = OperationalServerIP();
	if (addr == "") {
		DebugURL("Prefix can't be determined. Server IP Address invalid (please use xxx.xxx.xxx.xxx)", g_DEBUG_LEVEL_ERROR);
		return "";
	}
	return "http://" + addr + ":9720";
}


function GetURL_ServerRequestLibrary(action, mac, path, start, count, search)
{
	var url = GetURL_ServerPrefix();
	if (url == "") return "";
	
    url = url + "/oms/api/1/control/lms";

	if (action === undefined) {
		DebugURL("GetURL_ServerRequestLibrary 'action' is required", g_DEBUG_LEVEL_ERROR);
		return "";
	}
	try { start = parseInt(start); } catch(err) { start = 0; }
	try { count = parseInt(count); } catch(err) { count = -1; }
	if (search === undefined) search = "";

	url = url + "?action=" + action;
	if (mac != "")    { url = url + "&mac=" + mac; }
	if (path != "")   { url = url + "&path=" + encodeURIComponent(path); }
	if (start >= 0)   { url = url + "&start=" + start; }
	if (count >= -1)  { url = url + "&count=" + count; }
	if (search != "") { url = url + "&search=" + encodeURIComponent(search); }

	return url;
}

function GetURL_ServerRequestEx(action, mac, path, param1)
{
	var url = GetURL_ServerPrefix();
	if (url == "") return "";

    url = url + "/oms/api/1/control/lms";

	if (action === undefined) {
		DebugURL("GetURL_ServerRequestLibrary 'action' is required", g_DEBUG_LEVEL_ERROR);
		return "";
	}

	url = url + "?action=" + action;
	if (mac != "")    { url = url + "&mac=" + mac; }
	if (path != "")   { url = url + "&path=" + encodeURIComponent(path); }

	param1 = String(param1);
	if (param1 != "") url = url + "&param1=" + encodeURIComponent(param1); 

	return url;
}

function GetURL_ServerRequestFavorite(action, favoriteName, favoriteUrl, favoriteArtUrl, favoriteID, favoriteParent, favoriteIndex)
{
	var url = GetURL_ServerPrefix();
	if (url == "") return "";

    url = url + "/oms/api/1/control/lms";

	if (action === undefined) {
		DebugURL("GetURL_ServerRequestLibrary 'action' is required", g_DEBUG_LEVEL_ERROR);
		return "";
	}

	url = url + "?action=" + action;
	if (favoriteName != "")   { url = url + "&fav_name="   + favoriteName; }
	if (favoriteUrl != "")    { url = url + "&fav_url="    + favoriteUrl; }
	if (favoriteArtUrl != "") { url = url + "&fav_arturl=" + favoriteArtUrl; }
	if (favoriteID != "")     { url = url + "&fav_id="     + favoriteID; }
	if (favoriteParent != "") { url = url + "&fav_parent=" + favoriteParent; }
	if (favoriteIndex != "")  { url = url + "&fav_index="  + favoriteIndex; }

	return url;
}

function GetURL_ServerRequestPlaylist(action, playlistID, param1)
{
	var url = GetURL_ServerPrefix();
	if (url == "") return "";

    url = url + "/oms/api/1/control/lms";

	if (action === undefined) {
		DebugURL("GetURL_ServerRequestPlaylist 'action' is required", g_DEBUG_LEVEL_ERROR);
		return "";
	}

	url = url + "?action=" + action;
	if (playlistID != "") { url = url + "&playlist_id="     + playlistID; }
	param1 = String(param1);
	if (param1 != "") url = url + "&param1=" + encodeURIComponent(param1); 
	
	return url;
}

// Ask the server (if necessary) for a complete Art URL from an unknown art URL...
function GetURL_CompleteArt(value)
{
	// If the URL is partial (incomplete) we need to complete it!
	if (value !== undefined) {
		if ((value.length > 0) && 
		    (value.toLowerCase().substring(0, 7) !== "http://") &&
			(value.toLowerCase().substring(0, 8) !== "https://")) {
			value = GetURL_ServerRequestEx("get_art", "", value, "");
			DebugArt("URL is incomplete. Modifying... URL = " + value, g_DEBUG_LEVEL_TRACE);
		}
	}
	return value;
}











/*
// This is a simple function to fetch a URL. It only calls the built in "System.GetURL" 
// function, but we have abstracted it to a function here for ease of loggin when necessary.
//
// Parameters
//		url - the complete URL (i.e. http://192.168.1.100:4742/get/this/page) to fetch.
//
// Return - The data returned by the server.
//
function FetchURL(url)
{
	if (url == undefined) return "";
	if (url == "") return;

	try {
		return System.GetURL(url);
	} catch (err) {
		// if (g_debug) System.Print("FetchURL : ERROR = " + err + "\r\n");
		return "";
	}
}



//*************** START  Utilities ***************
// This utility looks up the server's address and config port and builds the "start" of all server 
// URLs. They all look like : http://serveraddress:configport/... We do this so we don't have to 
// lookup the Server Address and TCP Config Port every time we create a URL because they are stored 
// in the RTI driver configuration (specified in ConfigSettings.xml) and we are always going to read 
// them from there on demand in case they are dynamically changable.
//
// Parameters - None (this reads from the RTI Driver Configuration)
//
// Returns - A partial URL ready for further processing (or an empty string).
//
// Note: The result does NOT end in a URL path (/) character.
//
*/