<?php 
	include "../../conn.php";
	include "../../functions2.php";
	
	header('Content-Type: application/json; charset=utf-8');
	header('Strict-Transport-Security: max-age=31536000');
	header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
	header('Access-Control-Allow-Credentials: true');
	$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
	header('Access-Control-Allow-Origin: ' . $origin);
	header('vary: Origin');
	
	date_default_timezone_set("Asia/Kolkata");
	$shnunc = date("Y-m-d H:i:s");
	$res = [
		'code' => 11,
		'msg' => 'Method not allowed',
		'msgCode' => 12,
		'serviceNowTime' => $shnunc,
	];
	$shonubody = file_get_contents("php://input");
	$post = json_decode($shonubody, true);
	
	if ($_SERVER['REQUEST_METHOD'] != 'GET') {
		if (isset($post['language']) && isset($post['random']) && isset($post['signature']) && isset($post['timestamp'])) {
			$language = htmlspecialchars(mysqli_real_escape_string($conn, $post['language']));
			$random = htmlspecialchars(mysqli_real_escape_string($conn, $post['random']));
			$signature = htmlspecialchars(mysqli_real_escape_string($conn, $post['signature']));
            $vendorCode = htmlspecialchars(mysqli_real_escape_string($conn, $post['vendorCode']));
			$gameCode = htmlspecialchars(mysqli_real_escape_string($conn, $post['gameCode']));
			$phonetype = htmlspecialchars(mysqli_real_escape_string($conn, $post['phonetype']));
			$shonustr = '{"gameCode":"'.$gameCode.'","language":'.$language.',"phonetype":'.$phonetype.',"random":"'.$random.'","vendorCode":'.$vendorCode.'}';
			$shonusign = strtoupper(md5($shonustr));
          
          
            
            
      
			if (true) {
				$bearer = explode(" ", $_SERVER['HTTP_AUTHORIZATION']);
				$author = $bearer[1];				
				$is_jwt_valid = is_jwt_valid($author);
				$data_auth = json_decode($is_jwt_valid, 1);
				if ($data_auth['status'] === 'Success') {
			
					$sesquery = ($vendorCode == 18) 
						? "SELECT * FROM shonu_subjects WHERE akshinak = '$author'"
						: "SELECT * FROM shonu_subjects WHERE akshinak = '$author'";
					
					$sesresult = $conn->query($sesquery);
					$sesnum = mysqli_num_rows($sesresult);
					$row = $sesresult->fetch_assoc();						
						if ($sesnum == 1) {
						    $uid = $row['id'];
						    $demoQuery = "SELECT 1 FROM demo WHERE balakedara = '$uid'";
                            $demoResult = $conn->query($demoQuery);
                            
                            if ($demoResult->num_rows > 0) {
                                  $res['code'] = 5;
                                    $res['msg'] = 'No operation permission';
                                    $res['msgCode'] = 2;  
                                    http_response_code(200);
                                    echo json_encode($res);
                                    
                                    die();
                            }
                            
                            
                            
                            $code = "mafiyaclubxxnexdeveloperxx021125";
                            $agentid="mafiyaclubxxnexdeveloperxx021125";
                            $agentkey="mafiyaclubxxnexdeveloperxx021125";
                            $callback_url = "https://bluewin.sbs/";
                            $gameLaunchUrl = "https://apisrental.apioperations.xyz/?post&gameId=".$gameCode."&code=$code&mobile=".$row['mobile']."&agentId=$agentid&agentKey=$agentkey&referrerUrl=$callback_url";
                            $ch = curl_init();
                            curl_setopt($ch, CURLOPT_URL, $gameLaunchUrl);
                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
                            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
                            curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
                            $response = curl_exec($ch);
                            if(curl_errno($ch)) {
                                exit;
                            }
                            curl_close($ch);
                            $responseData = json_decode($response, true);
                            if(isset($responseData['gameurl']) && !empty($responseData['gameurl'])) {
                                // The game URL is available
                                $res['code'] = 0;
                                $res['msg'] = 'Game launched successfully';
                                $res['data'] = [
                                    'url' => "$callback_urlapigames.php?game=live&type=api&quality=hd&token=hn0382cn34tpnyt58ny534tn8yt4ny2b7t0xdb1s2br61sdxr6b4d13dx3892cdn9t234nt70325d7tbd23btr34sd12b0rt834sd1280rb67t9324sd&apigames=".$responseData['gameurl']
                                ];
                                http_response_code(200);
                            } else {
                                $res['code'] = 0;
                                $res['msg'] = 'Failed to open game - some error try again later';
                                $res['msgCode'] = 3;
                                $res['et'] = $response;
                                http_response_code(200);
                            }
                            echo json_encode($res);
                        
				
                } else {
                    $res['code'] = 4;
                    $res['msg'] = 'No operation permission';
                    $res['msgCode'] = 2;
                    http_response_code(401);
                    echo json_encode($res);
                }
            } else {
                $res['code'] = 4;
                $res['msg'] = 'No operation permission';
                $res['msgCode'] = 2;
                http_response_code(401);
                echo json_encode($res);
            }
        } else {
            $res['code'] = 5;
            $res['msg'] = 'Wrong signature';
            $res['msgCode'] = 3;
            http_response_code(200);
            echo json_encode($res);
        }
    } else {
        $res['code'] = 7;
        $res['msg'] = 'Param is Invalid';
        $res['msgCode'] = 6;
        http_response_code(200);
        echo json_encode($res);
    }
} else {
    http_response_code(405);
    echo json_encode($res);
}
