<?php
	require_once("../system.engine/system.core.php");
	$l->setPrefix("ajax.handle.chat.list");
	
	$mPong = array();
	$a = &$mPong;
	$p = &$_POST;
	
	global $systemHandler;
	$systemExecution = $systemHandler->loadHandler("EXECUTION");
	$systemHash = $systemHandler->loadHandler("HASH");
	/* ### DO YOUR CODES ONLY FROM HERE ### */
	$mUserId = $systemHandler->loadHandler("USER")->mUserId;
	$a["htmlContent"] = null;
	$a["friendsCount"] = 0;
	$mQuery = $systemExecution->runQuery("SELECT id, user_one_id, user_two_id FROM web_friends WHERE user_one_id = '%s' OR user_two_id = '%s'", array($mUserId, $mUserId));
	while($mRow = $mQuery->fetch_assoc()) {
		$mFriendId = $mRow["user_one_id"] == $mUserId ? $mRow["user_two_id"] : $mRow["user_one_id"];
		$mFriend = new systemUSER($mFriendId);
		
		$a["htmlContent"] .= "<li class=\"list-group-item\" data-event=\"initialize-chat\" data-parameter=\"".$mFriendId."\"><img src=\"".$mFriend->mProfilePicture."\" class=\"notificationImage\" /><div class=\"notificationChatName\">".$mFriend->mFullname."</div></li>";
	}
	$a["friendsCount"] = $systemExecution->runQuery("SELECT COUNT(id) AS friendsCount FROM web_friends WHERE user_one_id = '%s' OR user_two_id = '%s'", array($mUserId, $mUserId))->fetch_object()->friendsCount;
	if($a["htmlContent"] == null)
		$a["htmlContent"] = "<center style=\"margin-top: 15px\">".$l->getValue("nofriends")."</center>";
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
?>