genie = {};

genie.StartGenieServices = function(){
    genie.currentUser = {};
    genie.config = {};
    genie.contentId = {};
    genie.currentContent = {};

      genieServiceBridge.init(function(){
          genie.onGenieServiceInit();
      });
};

genie.onGenieServiceInit = function(){
    genie.config = getUrlParameter('config');
    console.log("config", genie.config);

    genie.contentId = getUrlParameter('contentId');
    genie.getCurrentUser();
    genie.getContent(genie.contentId);
};

genie.getCurrentUser = function(){
    genieservice.getCurrentUser()
      .then(function(result) {
          if (result.status == "success") {
            if (result.data.uid) {
              genie.currentUser = result;
              console.log("currentUser", genie.currentUser);
            } else {
              console.log('INVALID_USER');
            }
          } else {
            console.log('INVALID_USER');
          }
        })
        .catch(function(err) {
          console.log("Failed - getCurrentUser", err);
      });
};

genie.getContent = function(contentId){
    genieservice.getContent(contentId)
        .then(function(result) {
          console.log("Success - getContent", result);
          genie.currentContent = {};
          genie.currentContent.metadata = result;
          genie.startTelemetry();
      })
        .catch(function(err){
          console.log("Failed", err);
      })
};

genie.startTelemetry = function(){
  var gameInfo = {}
  gameInfo.id = genie.currentContent.metadata.localData.identifier;
  gameInfo.ver = genie.currentContent.metadata.localData.pkgVersion || "1";

  TelemetryService.init(gameInfo, genie.currentUser).then(function() {
    console.log("TelemetryService is ready..");
    TelemetryService.start(gameInfo);
  });
};

genie.addScore = function(questionId,subject,level,data) {
      scored = TelemetryService.assess(questionId, subject, level, data);
	  successOrFailed = TelemetryService.assessEnd(scored, data);
      console.log(successOrFailed);
}

genie.addTouch = function(elementId){
      TelemetryService.interact('TOUCH', elementId, "", "");
};

genie.getGenieReadyData = function(pass, score){
    return {"pass": pass,"score": score, "resvalues":[],"length":0,"exlength":0,"params":[],"qindex":"","uri":""};
};

/*genie.gameStart = function(contentId, version){

};

genie.gameEnd = function(){

};*/


function getUrlParameter(sParam) {
 var sPageURL = decodeURIComponent(window.location.search.substring(1)),
     sURLVariables = sPageURL.split('&'),
     sParameterName,
     i;
 for (i = 0; i < sURLVariables.length; i++) {
     sParameterName = sURLVariables[i].split('=');

     if (sParameterName[0] === sParam) {
         return sParameterName[1] === undefined ? true : sParameterName[1];
     }
 }
}
