<?php
error_reporting(0);
include('db_connect.php');
//Receive JSON
require_once('Notifications.php');
						$notification = new Notifications();
		$response = array(); 				
$gcmRegIds=array();
$email;
$user_id;
     $friend_id;
       $dated;
       $item_id;
$JSON_Received = $_POST["JSON"];
//Decode Json

$obj = json_decode($JSON_Received, true);

 $length = count($obj);
 for ($i = 0; $i < $length; $i++) {
foreach ($obj[$i] as $key => $value) {
//    echo "The value of key '$key' is '$value'", PHP_EOL;    
   //  echo $obj[$i]['friend_token'], PHP_EOL;
   
   /* fetch token*/
   if(strcasecmp($key,'friend_token') == 0){
       $gcmRegIds[]= $value;
}
/*fetch email*/
 if(strcasecmp($key,'user_mail') == 0){
      $email= $value;
}
/*fetch user_id*/
 if(strcasecmp($key,'user_id') == 0){
      $user_id= $value;
}
/*fetch item_id*/
 if(strcasecmp($key,'item_id') == 0){
      $item_id= $value;
}

/*fetch dated*/
 if(strcasecmp($key,'dated') == 0){
      $dated= $value;
}
/*fetch dated*/
 if(strcasecmp($key,'friend_id') == 0){
      $friend_id= $value;
}

    //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);
  }

  
  //added to shared item
  
    		   $query1="INSERT INTO `shared_items`(`sender_id`, `receiver_id`, `item_id`) VALUES
			   ('$user_id','$friend_id','$item_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);
  }

}
}
echo json_encode($response);
mysqli_close($conn);
//sent push notification
 $serverAPIKey = "AAAA9R2L9rw:APA91bF8f86e4j3dzxQIDDD9WVag1FSZflcecqASCjK1SMFciV93ywymHE_tMf55y9tsl56TkYc0W3IGGN19B_GA31QiNIKJ6dEhYTRf-RZ27XjbEmBNGBj8xTLmL7NDHhNY5zu3QJhn";
                   define('FIREBASE_SERVER_KEY', $serverAPIKey);
                                  
                 // echo FIREBASE_SERVER_KEY;exit;
                 //  echo $title.$selection;exit;
	         //first check if the push has an image with it
		 //
		      //if the push don't have an image give null in place of image
		        $serverAPIKey =$serverAPIKey;
		        $title= "Grocery Shopping Checklist";
	 	        $message= $email." sent you shopping list!";
				$radioText= "ass";
				$notification->setTitle($title);
						$notification->setMessage($message);
						$notification->setAction("in.aziz.com.shopping.checklist.to.doing.simple.list.TARGETNOTIFICATION");
	 	       
		// }
		 
	 
	        //firebase server url to send the curl request
	        $url = 'https://fcm.googleapis.com/fcm/send';
	 
	        //building headers for the request
	        $headers = array(
	            'Authorization: key='. FIREBASE_SERVER_KEY,
	            'Content-Type: application/json'
	        );
	
	        //Initializing curl to open a connection
	        $ch = curl_init();
	 
	        //Setting the curl url
	        curl_setopt($ch, CURLOPT_URL, $url);
	        
	        //setting the method as post
	        curl_setopt($ch, CURLOPT_POST, true);
	
	        //adding headers 
	        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	 
	        //disabling ssl support
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	        
	  
	        $messagearray = array();
	 /*       $messagearray['data']['title'] = $title;
	        $messagearray['data']['message'] = $message;
	        $messagearray['data']['image'] = $image;*/
	        $messagearray =   $notification->getNotificatin();
	        //adding the fields in json format 
	         $fields = array(
	             'registration_ids' => $gcmRegIds,
	             'data' => $messagearray,
	        );
	        
	        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
	 
	        //finally executing the curl request 
	        $result = curl_exec($ch);
	        if ($result === FALSE) { 
	           die('Curl failed: ' . curl_error($ch));
	        }
	  
               //  exit();
	        //Now close the connection
	        curl_close($ch);
	 
	        //and return the result 
	      //  echo $result;

?>