<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- using jquery -->

<?php
// Keep in mind we need the following : 1) The content.json of the content file	located in "h5p/content/id/"
//										2) The h5p.json file that the content comes with (using it to get info of the libraries we need
//										3) The libraries folders(the ones the content comes with) located in "h5p/libraries/"

// Editor not done yet

$path = 'h5p/content';
$id = $_POST['id']; // change this to the id of the folder you're storing your content to
$json_file = $path . "/" . $id . '/h5p.json';

$string = file_get_contents($json_file);

$json = json_decode($string,true);
$mainlibrary = $json['mainLibrary'];

$key = 0; // we need the index of the main library in the h5p.json file
$countforeach = 0;

foreach($json['preloadedDependencies'] as $jsonfor){
  if(strcmp ($jsonfor['machineName'], $mainlibrary) == 0){
    $key = $countforeach;
  }
  $countforeach ++;
}
//var_dump($key);

$dep_js = array(); //array for storing preloaded js
$dep_css = array(); //array for storing preloaded css
$dep_dep = array(); //array for storing preloaded dependencies

$libs = array();
$i = 0;
foreach($json["preloadedDependencies"] as $lib){ // loading every dependency library into an array
	$libs[$i] = $lib['machineName'] . "-" . $lib['majorVersion'] . "." . $lib['minorVersion'];
	// echo "<br>" . $libs[$i];
	$lib_path = "h5p/libraries/" . $libs[$i] . "/" . 'library.json';
	$lib_file = file_get_contents($lib_path);
	$lib_dec = json_decode($lib_file,true);
	$lib_path = "h5p/libraries/" . $libs[$i];
	if(isset($lib_dec['preloadedJs'])){
		foreach($lib_dec['preloadedJs'] as $js){
			$dep_js[] = $lib_path . "/" . $js['path'];
		}	
	}
	if(isset($lib_dec['preloadedCss'])){
		foreach($lib_dec['preloadedCss'] as $css){
			$dep_css[] = $lib_path . "/" . $css['path'];
		}
	}
	if(isset($lib_dec['preloadedDependencies'])){
		foreach($lib_dec['preloadedDependencies'] as $dep){
			$dep_dep[] = $dep;
		}
	}
	$i++;
}

$dep = array();
foreach($dep_dep as $i){
		$dep[] = $i['machineName'] . "-" . $i['majorVersion'] . "." . $i['minorVersion'];
}

 // var_dump($dep);
//check to see if we missed a library
$search = count(array_intersect($dep, $libs)) == count($dep);
 // var_dump($search); //true if we have all the libraries we need
 // var_dump($dep_js); //the js files we need
 // var_dump($dep_css);//the css files we need
 // var_dump($libs); //the dependency libraries
?>

<Link rel="stylesheet" type="text/css" href="h5p-php-library/styles/h5p.css" /> <!-- needed for every h5p content -->
<?php
foreach($dep_css as $css){ //loading the css
	echo '<Link rel="stylesheet" type="text/css" href ="' . $css . '" />';
	echo "\n";
}

?>
<!-- needed for every h5p file -->
<script src="h5p-php-library/js/jquery.js"></script> 
<script src="h5p-php-library/js/h5p.js"></script>
<script src="h5p-php-library/js/h5p-event-dispatcher.js"></script>
<script src="h5p-php-library/js/h5p-x-api.js"></script>
<script src="h5p-php-library/js/h5p-x-api-event.js"></script>
<script src="h5p-php-library/js/h5p-action-bar.js"></script>
<script src="h5p-php-library/js/h5p-content-type.js"></script>


<?php

$cmp = "h5p/libraries/H5P.JoubelUI";
// var_dump($cmp);
$mainjs = array(); // saving the main library js
$joubeljs = array(); // saving the joubelUI js
$count = count($dep_js);
for($i=0; $i < $count; $i++){
  if  (strncmp ($cmp , $dep_js[$i] , $count ) == 0){
   $joubeljs[] = $dep_js[$i];
   unset($dep_js[$i]);
  }
}

// var_dump($joubeljs);

foreach($joubeljs as $js){
  echo "\n";
  echo '<script src="' . $js . '"></script>';
}
$dep_js = array_reverse(array_reverse($dep_js)); // "reseting" the array so NULL keys will be filled
// var_dump($dep_js);

$count = count($dep_js);
$cmp = "h5p/libraries/" . $mainlibrary;
$cmpcount = strlen($cmp) - 1;
for($i = 0; $i < $count; $i++){
  if  (strncmp ($cmp , $dep_js[$i] , $cmpcount ) == 0){
    $mainjs[] = $dep_js[$i];
    unset($dep_js[$i]);
  }

}

$dep_js = array_reverse(array_reverse($dep_js));

//var_dump($joubeljs);

//var_dump(array_reverse($dep_js));
$dep_js = array_reverse(array_reverse($dep_js));  // "reseting" the array so NULL keys will be filled
//var_dump($dep_js); 

foreach($dep_js as $js){ //load the js files
	echo "\n";
	echo '<script src="' . $js . '"></script>';
}

foreach($mainjs as $js){ //load the js files
  echo "\n";
  echo '<script src="' . $js . '"></script>';
}

?>

<script>
  // creating the window.H5PIntegration DOM

window.H5PIntegration = {
  "baseUrl": "localhost", 
  "url": "", 
  "postUserStatistics": false, 
  
  "ajaxPath": "",     
  "ajax": {
 
    "setFinished": "/interactive-contents/123/results/new", 

    "contentUserData": "/interactive-contents/:contentId/user-data?data_type=:dataType&subContentId=:subContentId"
  },
 
  "saveFreq": false, 
  
  "user": { 
    "name": "User Name",
    "mail": "user@mysite.com"
  },
  
  "siteUrl": "localhost", 
  "l10n": { 
    "H5P": { 
      "fullscreen": "Fullscreen",
      "disableFullscreen": "Disable fullscreen",
      "download": "Download",
      "copyrights": "Rights of use",
      "embed": "Embed",
      "size": "Size",
      "showAdvanced": "Show advanced",
      "hideAdvanced": "Hide advanced",
      "advancedHelp": "Include this script on your website if you want dynamic sizing of the embedded content:",
      "copyrightInformation": "Rights of use",
      "close": "Close",
      "title": "Title",
      "author": "Author",
      "year": "Year",
      "source": "Source",
      "license": "License",
      "thumbnail": "Thumbnail",
      "noCopyrights": "No copyright information available for this content.",
      "downloadDescription": "Download this content as a H5P file.",
      "copyrightsDescription": "View copyright information for this content.",
      "embedDescription": "View the embed code for this content.",
      "h5pDescription": "Visit h5p.test.schulcloud-thueringen.de to check out more cool content.",
      "contentChanged": "This content has changed since you last used it.",
      "startingOver": "You'll be starting over.",
      "by": "by",
      "showMore": "Show more",
      "showLess": "Show less",
      "subLevel": "Sublevel"
    } 
  },
  "loadedJs": [], 
  "loadedCss": [],
  "core": { 
    "scripts": [], 
    "styles": []
  }
};


//using ajax/jquery to get the external json file,mainlibraryname
var content; //we need the content here
var library; //we need the main library name : Name majorversion.minorVersion
var fullscreen; //we need to know whether or not we have fullscreen
var id = <?php echo $id ?>;
var url ="h5p/content/" + id + "/content.json";
$.ajax({
    'async': false,
    'global': false,
    'url': url,
    'dataType': "json",
    'success': function (data) {
        content = data;
    }
});

var key = <?php echo $key ?>;

url = "h5p/content/" + id + "/h5p.json";
$.ajax({
    'async': false,
    'global': false,
    'url': url,
    'dataType': "json",
    'success': function (data) {
        library = data.preloadedDependencies[key];
    }
});

var librarywithoutspace = library['machineName']+"-"+library['majorVersion']+"."+library['minorVersion'];

library = library['machineName']+" "+library['majorVersion']+"."+library['minorVersion'];


url = "h5p/libraries/" + librarywithoutspace + "/library.json";

$.ajax({
    'async': false,
    'global': false,
    'url': url,
    'dataType': "json",
    'success': function (data) {
        fullscreen = data;
    }
});

fullscreen = fullscreen.fullscreen;




//loading the content settings


window.H5PIntegration.contents ={
  'cid-0': {
  "library": library, 
  "jsonContent": JSON.stringify(content),
  "fullScreen": fullscreen, 
  "exportUrl": "https:\\www.google.gr",
  "embedCode": "this is embed code(not done yet)",
  "resizeCode": "<script src=\"h5p-php-library/js/h5p-resizer.js\" charset=\"UTF-8\"><\/script>",
  "mainId": 0,
  "url": "this isn't esential",
  "title": "Example?",
  "contentUserData": {
    0: { 
      'state' : false 
    }
  },
  "displayOptions": {
    "frame": true, 
    "export": true, 
    "embed": true, 
    "copyright": true, 
    "icon": true 
  },"core": {
    "scripts": [
    ],
    "styles": [
	  ]
}
}
};

</script>
<body>
  <!-- loading the content -->
<div class="h5p-content" data-content-id="0"></div>
</body>

</html>