<?php
error_reporting(0);

include('db_connect.php');
$response = array(); 

if(isset($_GET['user_id']) && isset($_GET['friend_id'])){
    $user_id=$_GET['user_id'];
     $friend_id=$_GET['friend_id'];
       $dated=$_GET['dated'];
       $token= $_GET['token'];
        $email= $_GET['email'];
       
       //added to shared list
          $query="INSERT INTO `shared_to`( `sender_id`, `receiver_id`, `dated`) VALUES 
     ('$user_id','$friend_id','$dated')";
  if(mysqli_query($conn,$query)){
   $response['error'] = false; 
			$response['message'] = "Added to shared to list \n";
		//	echo json_encode($response);
	

  }else{
   	$response['error'] = true; 
			$response['message'] = "Some error occurred please try again \n";	
		//	echo json_encode($response);
  }

$r=mysqli_query($conn,"Select * from grocery_list where id='$user_id'");
while($row=mysqli_fetch_array($r))
{
$id=$row['item_id'];
if($id!=null){
    		   $query1="INSERT INTO `shared_items`(`sender_id`, `receiver_id`, `item_id`) VALUES
			   ('$user_id','$friend_id','$id')";
  if(mysqli_query($conn,$query1)){
   $response['error'] = false; 
			$response['message'] = "List sent to friend";
		//	echo json_encode($response);
			

  }else{
   	$response['error'] = true; 
			$response['message'] = "Some error occurred please try again \n";	
		//	echo json_encode($response);
  }
  
 
}else{
    $response= "Some error occured. Please Try again!";
    echo json_encode($response);
}
}
}else{
    $response['error']= "error";
    echo json_encode($response);
}
if(strcasecmp($response,'List sent to friend') == 0){
  $action = 'in.aziz.com.shopping.checklist.to.doing.simple.list.TARGETNOTIFICATION';
							$title = 'Grocery Shopping Checklist';
							$message = $email.' sent you shopping list!';
							$id = $token;
									sendGCM($title,$message,$action,$id);
}else{
  echo "error";
}

echo json_encode($response);
mysqli_close($conn);

function sendGCM($title,$message,$action, $id) {
	require_once('Notifications.php');
						$notification = new Notifications();
						
					
						$notification->setTitle($title);
						$notification->setMessage($message);
						$notification->setAction($action);
						
						$firebase_token = $id;
						$firebase_api ="AAAA9R2L9rw:APA91bF8f86e4j3dzxQIDDD9WVag1FSZflcecqASCjK1SMFciV93ywymHE_tMf55y9tsl56TkYc0W3IGGN19B_GA31QiNIKJ6dEhYTRf-RZ27XjbEmBNGBj8xTLmL7NDHhNY5zu3QJhn";
						
					
						
						$requestData = $notification->getNotificatin();
						
					
							
							$fields = array(
								'to' => $firebase_token,
								'data' => $requestData,
							);
						
		
		
						// Set POST variables
						$url = 'https://fcm.googleapis.com/fcm/send';
 
						$headers = array(
							'Authorization: key=' . $firebase_api,
							'Content-Type: application/json'
						);
						
						// Open connection
						$ch = curl_init();
 
						// Set the url, number of POST vars, POST data
						curl_setopt($ch, CURLOPT_URL, $url);
 
						curl_setopt($ch, CURLOPT_POST, true);
						curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
						// Disabling SSL Certificate support temporarily
						curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 
						curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
 
						// Execute post
						$result = curl_exec($ch);
						if($result === FALSE){
							die('Curl failed: ' . curl_error($ch));
						}
 
						// Close connection
						curl_close($ch);
						
					
					//	echo json_encode($fields,JSON_PRETTY_PRINT);
				
						// echo $result;
}
?>