<?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 ### */
	
	$a["htmlContent"] = null;
	$mContent = explode(' ', trim($p["nameContent"]));
	$mQuery = null;
	$mUserId = $systemHandler->loadHandler("USER")->mUserId;
	
	if(count($mContent) >= 2) {
		$mFirstname = $mContent[0];
		$mLastname = $mContent[1];
		$mQuery = $systemHandler->loadHandler("MYSQLI")->mConnection->query("SELECT web_user.id, web_user.firstname, web_user.lastname FROM web_user, web_friends WHERE (web_user.firstname LIKE '%".$mFirstname."%' OR web_user.lastname LIKE '%".$mLastname."%') AND ((web_friends.user_one_id = web_user.id AND web_friends.user_two_id = '".$mUserId."') OR (web_friends.user_one_id = '".$mUserId."' AND web_friends.user_two_id = web_user.id))");
	} else if(count($mContent) == 1) {
		$mOnePart = $mContent[0];
		$mQuery = $systemHandler->loadHandler("MYSQLI")->mConnection->query("SELECT web_user.id, web_user.firstname, web_user.lastname FROM web_user, web_friends WHERE (web_user.firstname LIKE '%".$mOnePart."%' OR web_user.lastname LIKE '%".$mOnePart."%') AND ((web_friends.user_one_id = web_user.id AND web_friends.user_two_id = '".$mUserId."') OR (web_friends.user_one_id = '".$mUserId."' AND web_friends.user_two_id = web_user.id))");
	}
	
	if($mQuery !== null) {
		while($mRow = $mQuery->fetch_array()) {
			$mFriend = new systemUSER($mRow[0]);
			$a["htmlContent"] .= "<li chat-event=\"initialize\" chat-receiver-id=\"".$mFriend->mUserId."\" class=\"list-group-item\"><img src=\"".$mFriend->mProfilePicture."\" class=\"eliteChatUserImage\" /> ".$mFriend->mFullname."</li>";
		}
	}
	
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
?>