<?php
		session_start();
		include('database.php');
		DB_connexion(); 
		error_reporting(0);

		//include("fusioncharts.php");
		
		$J1 = $_GET['J1'];
		$J2 = $_GET['J2'];
		
		//$J1 = 10;
		//$J2 = 7;
			
		//$J1 = "henrique";
		//$J2 = "fabien";
			
		$sql = "SELECT * FROM classement WHERE joueur = '$J1' ";
		$result = mysql_query($sql);		
		$data = mysql_fetch_assoc($result);
		$id_J1 = $data['id'];
	
		$sql = "SELECT * FROM classement WHERE joueur = '$J2' ";
		$result = mysql_query($sql);		
		$data = mysql_fetch_assoc($result);
		$id_J2 = $data['id'];
	
		//echo $J1 ."  " .$J2 ."<br>";
		//echo $id_J1 ."  " .$id_J2;
		
		
?>

<html>
<head>
<title>Essai FusionCharts</title>
<!--
<script language="javascript" src="fusioncharts.js"></script>
-->
<script language="JavaScript" src="Charts/chartembed.js"></script>

</head>
<body>

<center>
<h4>Comparatif</h4>

<?php
						
			$tab_asso_J1 = array();
			$tab_asso_J2 = array();
			
			// Joueur 1
			$query = "SELECT * FROM evolution WHERE playerID = '$id_J1' ORDER by date asc";
			$result = mysql_query($query);
			$num = mysql_num_rows($result);
			if ($num != 0) 
			{
				$cpt = 0;
				while ($row = mysql_fetch_array($result))
				{
						$label = $row['date']; 
						$data_J1[$cpt][1] = $label;
						$time_J1[$cpt] = $label;
						
						$value= $row["rating"];
						$data_J1[$cpt][2] = $value;
						
						$tab_asso_J1[$label] = $value;  
						
						$J1_time[$cpt] = $label;
						$J1_score[$cpt] = $value;
						
						$cpt = $cpt + 1;
				}
			
			}	
			
			// Joueur 2
			$query = "SELECT * FROM evolution WHERE playerID = '$id_J2' ORDER by date asc";
			$result = mysql_query($query);
			$num = mysql_num_rows($result);
			if ($num != 0) 
			{
				$cpt = 0;
				while ($row = mysql_fetch_array($result))
				{
						$label = $row['date']; 
						$data_J2[$cpt][1] = $label;
						$time_J2[$cpt] = $label;
						
						$value= $row["rating"];
						$data_J2[$cpt][2] = $value;
						
						$tab_asso_J2[$label] = $value;  
						
						$J2_time[$cpt] = $label;
						$J2_score[$cpt] = $value;

						$cpt = $cpt + 1;
				}
			
			}		
		
		
			// score min et max + ou - 50
			$score_merge = array_merge($J1_score, $J2_score);
			$score_unique = array_unique($score_merge);
			$score_re_index = array_values($score_unique);
			$ymin = min($score_re_index);
			$ymax = max($score_re_index);
		
			$ymin = $ymin - 50;	
			$ymax = $ymax + 50;	

			$time_merge = array_merge($time_J1, $time_J2);
			sort($time_merge);
			$time_unique = array_unique($time_merge);
			$time_re_index = array_values($time_unique);
			
			for ($i = 0; $i < count($time_re_index); $i++)
			{
				$jour = date('d/m/Y', $time_re_index[$i]);
				$arrdata[$i][1] = $jour;
			}
			
			
			for ($i = 0; $i < count($time_re_index); $i++)
			{
				$element = $time_re_index[$i];
				if ( array_search($element, $J1_time) !== false)
				{
					$cle = array_search($element, $J1_time);
					$score = $J1_score[$cle];
					$arrdata[$i][2] = $score;
				}
				else
				{
					$arrdata[$i][2] = $score;		
				}
			}
			
			
			for ($i = 0; $i < count($time_re_index); $i++)
			{
				$element = $time_re_index[$i];
				if ( array_search($element, $J2_time) !== false)
				{
					$cle = array_search($element, $J2_time);
					$score = $J2_score[$cle];
					$arrdata[$i][3] = $score;
				}
				else
				{
					$arrdata[$i][3] = $score;
				}
			}
			
			
			$file= fopen("pipo.xml", "w");
//			$_xml="<chart caption='Evolution du classement' xAxisName='Parties' yAxisName='Niveau ELO' decimals='0' 
//			formatNumberScale='0' formatNumber='0' showValues='0' yAxisMinValue='$ymin' yAxisMaxValue='$ymax' >";
		
//			$_xml="<chart caption='Evolution du classement' xAxisName='Parties' yAxisName='Niveau ELO' decimals='0' 
//			formatNumberScale='0' formatNumber='0' showValues='0' yAxisMinValue='".$graph_min."' 
//			yAxisMaxValue='".$graph_max."' >";
			
			$_xml="<chart caption='Evolution du classement' xAxisName='Jour de la partie' yAxisName='Niveau ELO' yAxisMinValue='".$ymin."' 
			yAxisMaxValue='".$ymax."' formatNumber='0' formatnumberscale='0' rotateNames='1' rotatevalues='0' >"; 
	
			$label = "";
			
			$_xml .= "<categories>";
			for ($i = 0; $i < count($time_re_index); $i++) 
			{
				$_xml .= "<category label='" . $arrdata[$i][1]  . "' />";				
				$_xml .= "\r\n";
			}
			$_xml .= "</categories>";	
	
			//$J1 = "moi";
			$_xml .= "<dataset seriesname='" . $J1 . "' >";				
			for ($i = 0; $i < count($time_re_index); $i++)
			{
				$_xml .= "<set value='" . $arrdata[$i][2] . "' />";				
				$_xml .= "\r\n";
			}
			$_xml .= "</dataset>";	
	
			//$J2 = "toi";
			$_xml .= "<dataset seriesname='" . $J2 . "' >";				
			for ($i = 0; $i < count($time_re_index); $i++)
			{
				$_xml .= "<set value='" . $arrdata[$i][3] . "' />";				
				$_xml .= "\r\n";
			}
			$_xml .= "</dataset>";	

			$_xml .="</chart>";

			fwrite($file, $_xml);
			fclose($file);

?>			
	
			<div id="chartdiv" align="center"	></div>

<?php			
			echo ("
						<script type=\"text/javascript\">
						var myChart = new FusionCharts(\"Charts/ScrollLine2D.swf\", \"myChartId\", \"900\", \"550\", \"0\", \"0\");
						myChart.setDataURL(escape(\"pipo.xml?currTime=\" + getTimeForURL()));
						myChart.render(\"chartdiv\");
						function getTimeForURL(){
						var dt = new Date();
						var strOutput = \"\";
						strOutput = dt.getHours() + \"_\" + dt.getMinutes() + \"_\" + dt.getSeconds() + \"_\" + dt.getMilliseconds();
						return strOutput;
						}
						</script>"
				);
			
			echo "<br>";
			echo "Nombre de parties :" .count($time_re_index);
	
?>

</center>
</body>
</html>