<?php

require 'friend.php';
require "appmanager.php";
if (AppManager::checkAppCode($_REQUEST['appcode'])) {
    switch ($_REQUEST['type']) {
        case 'GET_FRIEND_LIST':
            echo json_encode(array("friends" => FriendManager::getFriendsById($_REQUEST['id'])));
            break;
        case 'GET_FRIEND_BY_FID':
            echo json_encode(FriendManager::getFriendByFid($_REQUEST['fid']));
            break;
        case 'FRIEND_REMOVE':
            echo FriendManager::removeFriend($_REQUEST['uid'], $_REQUEST['fid']) + FriendManager::removeFriend($_REQUEST['fid'], $_REQUEST['uid']);
            break;
        case 'SET_SHARE':
            echo FriendManager::setShare($_REQUEST['uid'], $_REQUEST['fid'], $_REQUEST['share']);
            break;
        case 'STOP_SHARE':
            echo FriendManager::stopShare($_REQUEST['uid'], $_REQUEST['fid']);
            break;
        case 'GET_SHARE':
            echo FriendManager::getShare($_REQUEST['uid'], $_REQUEST['fid']);
            break;
        default:
            echo "Unknown type";
    }
}
     echo json_encode(ChatsManager::removeAllChats($_REQUEST['senderid']));
     break;
 case 'GET_CHATS_BY_ID':
     echo json_encode(ChatsManager::getAllChatsById($_REQUEST['id']));
     break;
 case 'SEND_FRIEND_REQUEST':
     echo ChatsManager::send_msg(new Chats(NULL, $_REQUEST['senderid'], $_REQUEST['recipientid'], $_REQUEST['msg']));
     FriendManager::createNewFriend($_REQUEST['senderid'], $_REQUEST['recipientid']);
     FriendManager::setShare($_REQUEST['senderid'], $_REQUEST['recipientid'], 0);
     FriendManager::createNewFriend($_REQUEST['recipientid'], $_REQUEST['senderid']);
     FriendManager::setShare($_REQUEST['recipientid'], $_REQUEST['senderid'], 1);
     break;
 case 'SEND_FRIEND_NOT_ACCEPT':
     echo ChatsManager::send_msg(new Chats(NULL, $_REQUEST['senderid'], $_REQUEST['recipientid'], $_REQUEST['msg']));
     FriendManager::removeFriend($_REQUEST['recipientid'], $_REQUEST['senderid']);
     FriendManager::removeFriend($_REQUEST['senderid'], $_REQUEST['recipientid']);
     break;
 case 'SEND_FRIEND_ACCEPT':
     echo ChatsManager::send_msg(new Chats(NULL, $_REQUEST['senderid'], $_REQUEST['recipientid'], $_REQUEST['msg']));
     FriendManager::setShare($_REQUEST['recipientid'], $_REQUEST['senderid'], 2);
     FriendManager::setShare($_REQUEST['senderid'], $_REQUEST['recipientid'], 2);
     break;
 case 'SEND_SHARE_REQUEST':
     echo ChatsManager::send_msg(new Chats(NULL, $_REQUEST['senderid'], $_REQUEST['recipientid'], $_REQUEST['msg']));
     FriendManager::setShare($_REQUEST['senderid'], $_REQUEST['recipientid'], 3);
     FriendManager::setShare($_REQUEST['recipientid'], $_REQUEST['senderid'], 4);
     break;
 case 'SEND_SHARE_NOT_ACCEPT':
     echo ChatsManager::send_msg(new Chats(NULL, $_REQUEST['senderid'], $_REQUEST['recipientid'], $_REQUEST['msg']));
     FriendManager::setShare($_REQUEST['recipientid'], $_REQUEST['senderid'], 2);
     FriendManager::setShare($_REQUEST['senderid'], $_REQUEST['recipientid'], 2);