<?php
error_reporting(0);
require_once "system/constant.php";
if (!empty($_SERVER['HTTPS'])) 
{
  $http = 'https://';
}
else
{
$http = 'http://';
}
$host  = $http.$_SERVER['HTTP_HOST'].str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER["SCRIPT_NAME"]);

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
		<link href="<?=siteUrl?>assets/css/player3.min.css" type="text/css" rel="stylesheet">
		<script src="<?=siteUrl?>assets/js/player3.min.js"></script>
        <!--=====================================CSS LINK PART END=======================================-->
    </head>
<body> 
 <?php 
 
$mysqli =mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(isset($_REQUEST["token"]) && !empty($_REQUEST["token"])){
    //Get all state data
	$token = $_REQUEST["token"];
	$query = $mysqli->query("SELECT * FROM tbl_widget where token = '$token'");
    
    //Count total number of rows
    $rowCount = $query->num_rows;
	if($rowCount > 0){
		while($row = $query->fetch_assoc()){
		   $platform =  $row['platform'];
		   $stream = $row['stream'];
	    if($platform == "sc"){    
		$parsedUrl = parse_url($stream);
        $http = $parsedUrl['scheme'];
        $ip = $parsedUrl['host'];
        $port = (string) $parsedUrl['port'];
        
        $streamlink = $http."://". $ip.":".$port;
        $mount = "stream";
	    }else{
	       $path = parse_url($stream);
                
			  $http = $path['scheme'];
              $ip = $path['host'];
              if($path['port']){
                 $port = ":".(string) $path['port'];
              }else{
                $port = "";   
              }
              if($path['path']){
                  $url = explode('/', $path['path']);
                array_pop($url);
                $psub = implode('/', $url);
              }else{
                $psub = "";   
              }
			  $streamlink = $http."://". $ip.$port.$psub;  
			  $pathFragments = explode('/', $stream);   
              $end = end($pathFragments);
			  $mount = $end;  
	        
	    }
        $background = $row['background'];
        $volume = $row['volume'];
        $history = $row['history'];
        $theme = $row['theme'];
        $title = $row['title'];
        $salogan = $row['salogan'];
        $cover_image = $row['cover_image'];
        $statictitle = $row['statictitle'];
		
         if($row['hidelogo'] == "true"){
           echo '<style>
           .p3-artwork {
                display: none !important;
            }
            .p3-metadata {
                padding: 0 20px 0 105px !important;
            }
            </style>';
        }else{
           echo '<style>.p3-artwork {
                display: block !important;
            }
            .p3-metadata {
                padding: 0 20px 0 190px;
            }
            </style>';
        }
         if($row['hidecover'] == "true"){
             $cimage = "";
             echo '<style>
                .p3-artwork {
                 left: 0px;
                }
                .p3-metadata {
                    padding: 0 20px 0 100px;
                }
             </style>';
          
        }else{
            $cimage = siteUrl.$row['logo'];
           
        }
         if($row['hidevolume'] == "true"){
           echo '<style>
            .p3-volIcon {
                display:none !important;
            }
            .p3-controls input[type="range"]{
                display:none !important;
            }
            .p3-volSliderBar {
                display:none !important;
                }
            </style>';
        }else{
           echo '<style>
           .p3-volIcon {
                display:block ;
            }
            .p3-controls input[type="range"]{
                display:block ;
            }
            .p3-volSliderBar {
                display:block ;
                }
            </style>';
        }
        if($row['hidetitle'] == "true"){
           echo '<style>
            .p3-metadata {
                display: none !important;
            }
            </style>';
        }else{
           echo '<style>
           .p3-metadata {
                display:block !important;
            }
            </style>';
        }
        
        if($row['hidesocial'] == "true"){
           echo '<style>
            .p3-socialShare {
                display: none !important;
            }
            </style>';
        }else{
           echo '<style>
           .p3-socialShare {
                display:flex !important;
            }
            </style>';
        }
		?>
		
        <div class="bottom-player">
           <div id="p2"></div>
        </div>
		<script>
       
		var p2 = new player3({
        target: '#p2',
		url: '<?=$streamlink?>',
		platform: '<?=$platform?>',
		mountPoint: '<?=$mount?>',
		bg: '<?=$background?>',
		volume: '<?=$volume?>',
		stationName: '<?=$title?>',
		staticTitles: <?=$statictitle?>,
		slogan: '<?=$salogan?>',
		theme: '<?=$theme?>',
		songHistory: <?=$history?>,
		historyText: "Recently Played",
		minimize: false,
		socialShare: true,
		logo: '<?=$cover_image?>',
    });
	
			$('.p3-artlogo').css('background-image', 'url("<?=$cimage?>")');
	   
		</script>
	<?php if($row['autoplay'] == "true"){
	    ?>
	    <script>
		 $( window ).on("load", function() {
	        setTimeout(function() {
            var link = document.querySelector('#ppBtn');
    			if(link) {
    				link.click();
    			}
	        }, 1000);
        });
        </script>
<?php }}}} ?>