/**** CONFIG ****/
var pinginterval = 5; //fait un appel serveur toute les N minutes pour maintenir la session
var scormVersion = "2004";//"1.2";
var debug = false;
/******************/
var startTime;

//init scorm data at launch
function InitSCORM()
{

	pingInt = setInterval(doPing, 60000*pinginterval);
	
	//alert("initScorm called !");
	startTime = new Date();
	pipwerks.SCORM.version = scormVersion;
	var itycom_success_results = pipwerks.SCORM.init();
	
	
	if (itycom_success_results == true )
	{
		//commit();
	} else if(debug) {
		console.log("Impossible de communiquer avec le LMS.");
	}
	else
	{
		console.log("What's wrong ! can't init");
	}
}

//set a scorm value
function setValue(field,value)
{
	alert("set value "+field+"="+value);
	pipwerks.SCORM.set(field,value);
	//pipwerks.SCORM.save();
}


function commit()
{
	console.log(" -*-> making commit ...");
	saveSCORM().then(function(bbool){
		console.log(" -*-> commit done ! "+bbool);
		gameInstance.SendMessage('SCORMManager', 'LMSResponse',"commit done ! "+true);
	});
	
}

async function saveSCORM()
{
	return pipwerks.SCORM.save();
}

function setVariable(field,value)
{
	pipwerks.SCORM.set(field,value);	
}


function getVariable(field)
{
	return pipwerks.SCORM.get(field);
}

//get a scorm value
function getValue(field)
{
	//alert("get value "+field);
	//alert("scorm return : "+pipwerks.SCORM.get(field));
	
	return pipwerks.SCORM.get(field);
}

//used to defined session time
function getTime()
{
	var endTime = new Date();
	var diff = endTime - startTime;
	var msec = diff;
	var hh = Math.floor(msec / 1000 / 60 / 60);
	msec -= hh * 1000 * 60 * 60;
	var mm = Math.floor(msec / 1000 / 60);
	msec -= mm * 1000 * 60;
	var ss = Math.floor(msec / 1000);
	msec -= ss * 1000;
	
	var sessionTime = "PT"+hh+"H"+mm+"M"+ss+"S"; //PT[hh]H[mm]M[ss]S, ex : PT1H25M32S
	if(scormVersion=="1.2")
	{
		formatted_h=("000" + hh).slice(-4);
		formatted_m=("0" + mm).slice(-2);
		formatted_s=("0" + ss).slice(-2);
		sessionTime=formatted_h+":"+formatted_m+":"+formatted_s;
	}
	return sessionTime;
}

//used to set the time
function SetTime()
{
	var sessionTime = getTime();
	if(scormVersion=="1.2")
	pipwerks.SCORM.set("cmi.core.session_time", sessionTime+""); 
	else
	pipwerks.SCORM.set("cmi.session_time", sessionTime+""); //scorm 2004
	
}




function Terminate() 
{
	pipwerks.SCORM.quit();
}

function SubmitScore(score)
{
	if(scormVersion=="1.2")
	{
		if (score == 100)
	   {
		pipwerks.SCORM.set("cmi.core.lesson_status", "passed");
	   }
	
	pipwerks.SCORM.set("cmi.core.score.raw", score);
	pipwerks.SCORM.set("cmi.core.score.min", 0);
	pipwerks.SCORM.set("cmi.core.score.max", 100);
		
	}
	else
	{
	
	    if (score == 100)
	    {
			alert("score = 100");
		 pipwerks.SCORM.set("cmi.success_status", "passed");
		 pipwerks.SCORM.set("cmi.completion_status", "completed");
	    }
	
    	pipwerks.SCORM.set("cmi.score.raw", score);
    	pipwerks.SCORM.set("cmi.score.scaled", score / 100);
	    pipwerks.SCORM.set("cmi.score.min", 0);
	    pipwerks.SCORM.set("cmi.score.max", 100);
	}
	
}


function SubmitSuspendedData(suspendedData)
{
	pipwerks.SCORM.set("cmi.suspend_data", suspendedData);
}

function getSuspendedData()
{
	var suspend_data=pipwerks.SCORM.get("cmi.suspend_data");
	gameInstance.SendMessage('SCORMManager', 'updateSuspendedDataFromBrowser',suspend_data);
}

function MoreClick(link)
{
	window.open(link, '_blank');
}

//used to ping LMS
function doPing()
{
	pipwerks.SCORM.get("cmi.completion_status");
}
