<?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 ### */

	$mSenderUser = $p["senderUser"];
	$mFriendAccepted = $p["friendStatus"] == 1;
	$mUserId = $systemHandler->loadHandler("USER")->mUserId;
	$mFriendCount = $systemExecution->runQuery("SELECT COUNT(id) AS friendCount 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, $mSenderUser, $mSenderUser, $mUserId))->fetch_object()->friendCount;
	
	if($mFriendAccepted && $mFriendCount == 0) {
		$systemExecution->runQuery("DELETE FROM web_friendrequests WHERE (sender_id = '%s' AND receiver_id = '%s') OR (sender_id = '%s' AND receiver_id = '%s')", array($mSenderUser, $mUserId, $mUserId, $mSenderUser));
		$systemExecution->runQuery("INSERT INTO web_friends (user_one_id, user_two_id) VALUES ('%s', '%s')", array($mUserId, $mSenderUser));
	} else
		$systemExecution->runQuery("DELETE FROM web_friendrequests WHERE (sender_id = '%s' AND receiver_id = '%s') OR (sender_id = '%s' AND receiver_id = '%s')", array($mSenderUser, $mUserId, $mUserId, $mSenderUser));
	
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
?>