<?php
	require_once("../system.engine/system.core.php");
	
	$mPong = array();
	$a = &$mPong;
	$p = &$_POST;
	
	global $systemHandler;
	$systemExecution = $systemHandler->loadHandler("EXECUTION");
	$systemHash = $systemHandler->loadHandler("HASH");
	/* ### DO YOUR CODES ONLY FROM HERE ### */
	
	$mFriendId = $p["friendId"];
	$mUserId = $systemHandler->loadHandler("USER")->mUserId;
	
	if($systemExecution->runQuery("SELECT COUNT(id) AS friendsCount FROM web_friends WHERE (user_one_id = '%s' AND user_two_id = '%s') OR (user_one_id = '%s' AND user_two_id = '%s')", array($mUserId, $mFriendId, $mFriendId, $mUserId))->fetch_object()->friendsCount > 0) {
		$systemExecution->runQuery("DELETE FROM web_friends WHERE (user_one_id = '%s' AND user_two_id = '%s') OR (user_one_id = '%s' AND user_two_id = '%s')", array($mUserId, $mFriendId, $mFriendId, $mUserId));
		$a["isValid"] = true;
	} else if($systemExecution->runQuery("SELECT COUNT(id) AS requestsCount FROM web_friendrequests WHERE (sender_id = '%s' AND receiver_id = '%s') OR (sender_id = '%s' AND receiver_id = '%s')", array($mUserId, $mFriendId, $mFriendId, $mUserId))->fetch_object()->requestsCount > 0) {
		$systemExecution->runQuery("DELETE FROM web_friendrequests WHERE (sender_id = '%s' AND receiver_id = '%s') OR (sender_id = '%s' AND receiver_id = '%s')", array($mUserId, $mFriendId, $mFriendId, $mUserId));
		$a["isValid"] = true;
	} else $a["isValid"] = false;
	
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
?>