<?php
require_once 'conf/link.php';
require_once '_gen_xml.php';

if(isset($_POST['player_name']) && (!empty($_POST['player_name'])))
{
	//echo "if isset";
	$player_name = $_POST['player_name'];

	$lst_assoc = (isset($_POST['lst_assoc'])) ? $_POST['lst_assoc'] : '';

	$res_x	= 	(isset($_POST['res_x'])) ? $_POST['res_x'] : '0';
	$res_y	= 	(isset($_POST['res_y'])) ? $_POST['res_y'] : '0';
	$x0		= 	(isset($_POST['x0'])) ? $_POST['x0'] : '0';
	$y0		= 	(isset($_POST['y0'])) ? $_POST['y0'] : '0';

	if(isset($_POST['time3']))
	{
		$time_inicio = explode(':', $_POST['time3']);
		$hora_inicio = $time_inicio[0];
		$min_inicio = $time_inicio[1];

	}
	else
	{
		$hora_inicio = '07';
		$min_inicio = '00';
	}

	if(isset($_POST['time4']))
	{
		$time_fin = explode(':', $_POST['time4']);
		$hora_fin = $time_fin[0];
		$min_fin = $time_fin[1];
	}
	else
	{
		$hora_fin = '21';
		$min_fin = '30';
	}




	if($lst_assoc != '')
	{
		$sql_lista = 'SELECT * FROM listas WHERE nombre_lista = "'.$lst_assoc.'"';

				$result_lista = mysqli_query($conexion, $sql_lista);
				while($row_lista = mysqli_fetch_array($result_lista))
				{
					$playlist_json = $row_lista['videos_id'];
				}

		//playlist_json : [{"id":"1","filename":"fibraOpica1152_20230125170804.mp4","thumbnail":"fibraOpica1152_20230125170804.jpg"},{"id":"7","filename":"02_20230127001836.mp4","thumbnail":"02_20230127001836.jpg"},{"id":"8","filename":"03_20230127001838.mp4","thumbnail":"03_20230127001838.jpg"}]

		
		$files = glob('Players/'.$player_name.'/Content/*'); //obtenemos todos los nombres de los ficheros
		
		foreach($files as $file){
			if(is_file($file))
			unlink($file); //elimino el fichero
		}

		$path = "Players/".$player_name."/Content/";
		$almacen = "almacen/";
		
		$playlist = json_decode($playlist_json, true);
		//print_r ($playlist) : Array ( [0] => Array ( [id] => 1 [filename] => fibraOpica1152_20230125170804.mp4 [thumbnail] => fibraOpica1152_20230125170804.jpg ) [1] => Array ( [id] => 7 [filename] => 02_20230127001836.mp4 [thumbnail] => 02_20230127001836.jpg ) [2] => Array ( [id] => 8 [filename] => 03_20230127001838.mp4 [thumbnail] => 03_20230127001838.jpg ) )
		$error_copy = false;
		foreach($playlist as $value)
		{

			if(copy($almacen.$value['filename'], $path.$value['filename']))
			{
				//echo "<br>".$almacen.$value['filename']."==>".$path.$value['filename']." ==>Copiado";
			}
			else
			{
				$error_copy = true;
				//echo "<br>".$almacen.$value['filename']."==>".$path.$value['filename']." ==>Error, no se ppudo copiar.";
			}

		}
		
		if($error_copy)
		{
			"Hubo un error en la copia de archivos. No se actualizara la base de datos.";
		}
		else
		{
			$sql_update  = "UPDATE players SET lst_assoc='".$lst_assoc."', res_x='".$res_x."', res_y='".$res_y."', x0='".$x0."', y0='".$y0."', hour_start='".$hora_inicio."', min_start='".$min_inicio."', hour_end='".$hora_fin."', min_end='".$min_fin."' WHERE player_name='".$player_name."'";

			if($result_update = mysqli_query($conexion, $sql_update))
			{
				
				$media = get_plan_json($lst_assoc, $player_name, $conexion);
				
				if(gen_xml($media, $player_name, $lst_assoc))
				{
					echo "Exito Update";
				}
				else
				{
					echo "Error Update Lista vacia";
				}
				
			}
			else
			{
				echo "Error Update Lista vacia";
			}
		}
		
	}
	else
	{
		$sql_update  = "UPDATE players SET lst_assoc='".$lst_assoc."', res_x='".$res_x."', res_y='".$res_y."', x0='".$x0."', y0='".$y0."', hour_start='".$hora_inicio."', min_start='".$min_inicio."', hour_end='".$hora_fin."', min_end='".$min_fin."' WHERE player_name='".$player_name."'";
		//echo "<br>update 02: ".$sql_update;
		if($result_update = mysqli_query($conexion, $sql_update))
		{
			echo "Exito Update";
		}
		else
		{
			echo "Error Update Lista vacia";
		}
	}
}
else
{
	echo "Error : No se especificó el Player. No se registraron los cambios.";
}
?>