<?php

/**
 * ajax -> chat -> live
 * 
 * @package Sngine
 * @author Zamblek
 */

// fetch bootstrap
require('../../../bootstrap.php');

// check AJAX Request
is_ajax();

// check user logged in
if (!$user->_logged_in) {
  modal('LOGIN');
}

// initialize opened chat boxes session if not
if (!isset($_SESSION['chat_boxes_opened'])) {
  $_SESSION['chat_boxes_opened'] = [];
}

// valid inputs
if (!isset($_POST['chat_enabled']) || !isset($_POST['chat_boxes_opened_client'])) {
  _error(400);
}
/* if chat_boxes_opened_client not array */
$_POST['chat_boxes_opened_client'] = json_decode($_POST['chat_boxes_opened_client'], true);
if (!is_array($_POST['chat_boxes_opened_client'])) {
  _error(400);
}
/* filter the client opened chat boxes */
$chat_boxes_opened_client = [];
foreach ($_POST['chat_boxes_opened_client'] as $key => $value) {
  if (is_numeric($key) && is_numeric($value)) {
    $chat_boxes_opened_client[$key] = $value;
  }
}
/* if opened_thread isset */
if (isset($_POST['opened_thread'])) {
  $_POST['opened_thread'] = json_decode($_POST['opened_thread'], true);
  if (!is_array($_POST['opened_thread'])) {
    _error(400);
  }
  if (!is_numeric($_POST['opened_thread']['conversation_id']) || !is_numeric($_POST['opened_thread']['last_message_id'])) {
    _error(400);
  }
}

try {

  // initialize the return array
  $return = [];

  // check if chat enabled & not from mobile
  $detect = new Detection\MobileDetect();
  if ($system['chat_enabled'] && $user->_data['user_privacy_chat'] != "me" && !($detect->isMobile() && !$detect->isTablet())) {
    // [1] [update] master chat sidebar (contacts list)
    $sidebar_friends = $user->get_contacts()['data'];
    /* assign variables */
    $smarty->assign('sidebar_friends', $sidebar_friends);
    /* return */
    $return['master']['sidebar'] = $smarty->fetch("ajax.chat.master.sidebar.tpl");

    // [2] [update] master chat sidebar (chat status)
    /* check chat status on both (client side & server side) */
    if ($user->_data['user_chat_enabled']) {
      /* return */
      $return['master']['chat_enabled'] = '1';
    } elseif (!$user->_data['user_chat_enabled'] && $_GET['chat_enabled']) {
      /* return */
      $return['master']['chat_enabled'] = '0';
    }

    /* prepare session */
    if (($key = array_search(NULL, $_SESSION['chat_boxes_opened'])) !== false) {
      unset($_SESSION['chat_boxes_opened'][$key]);
    }

    // [3] & [4] & [5]
    if (!(empty($chat_boxes_opened_client) && empty($_SESSION['chat_boxes_opened']))) {

      // [3] [get] closed chat boxes
      $chat_boxes_closed = array_diff(array_keys($chat_boxes_opened_client), $_SESSION['chat_boxes_opened']);
      if (count($chat_boxes_closed) > 0) {
        $return['chat_boxes_closed'] = $chat_boxes_closed;
      }

      // [4] [get] opened chat boxes
      $chat_boxes_pre_opened = array_diff($_SESSION['chat_boxes_opened'], array_keys($chat_boxes_opened_client));
      $chat_boxes_opened = [];
      foreach ($chat_boxes_pre_opened as $opened_conversation_id) {
        /* get conversation */
        $conversation = $user->get_conversation($opened_conversation_id);
        if ($conversation) {
          $chat_boxes_opened[] = $conversation;
        }
      }
      if (count($chat_boxes_opened) > 0) {
        $return['chat_boxes_opened'] = $chat_boxes_opened;
      }

      // [5] [get] updated chat boxes
      $chat_boxes_pre_updated = array_intersect($_SESSION['chat_boxes_opened'], array_keys($chat_boxes_opened_client));
      $chat_boxes_updated = [];
      foreach ($chat_boxes_pre_updated as $updated_conversation_id) {
        /* get conversation */
        $conversation = $user->get_conversation($updated_conversation_id);
        if ($conversation) {
          $return_this = false;
          /* [1] check for a new messages for this chat box */
          if ($conversation['last_message_id'] != $chat_boxes_opened_client[$conversation['conversation_id']]) {
            $return_this = true;
            /* get new messages */
            $messages = $user->get_conversation_messages($conversation['conversation_id'], 0, $chat_boxes_opened_client[$conversation['conversation_id']])['messages'];
            /* assign variables */
            $smarty->assign('messages', $messages);
            /* return */
            $last_message = end($messages);
            $conversation['is_me'] = ($last_message['user_id'] == $user->_data['user_id']) ? true : false;
            $conversation['messages_count'] = count($messages);
            $conversation['messages'] = $smarty->fetch("ajax.chat.messages.tpl");
          }
          /* [2] check if any recipient typing */
          if ($conversation['typing_name_list']) {
            $return_this = true;
          }
          /* [3] check if any recipient seen */
          if ($conversation['seen_name_list']) {
            $return_this = true;
          }
          /* [4] check single user's chat status (online|offline) */
          if (!$conversation['multiple_recipients']) {
            $return_this = true;
            /* update single user's chat status */
            $conversation['user_online'] = ($user->user_online($conversation['recipients'][0]['user_id'])) ? true : false;
          }
          /* return */
          if ($return_this) {
            $chat_boxes_updated[] = $conversation;
          }
        }
      }
      if (count($chat_boxes_updated) > 0) {
        $return['chat_boxes_updated'] = $chat_boxes_updated;
      }
    }

    // [6] [get] new chat boxes
    $chat_boxes_new = $user->get_conversations_new();
    if (count($chat_boxes_new) > 0) {
      $return['chat_boxes_new'] = $chat_boxes_new;
    }
  }

  // [7] [get] updated thread
  if (isset($_POST['opened_thread'])) {
    /* get conversation */
    $conversation = $user->get_conversation($_POST['opened_thread']['conversation_id']);
    if ($conversation) {
      $return_this = false;
      /* [1] check for a new messages for this converstaion */
      if ($conversation['last_message_id'] != $_POST['opened_thread']['last_message_id']) {
        $return_this = true;
        /* get new messages */
        $messages = $user->get_conversation_messages($conversation['conversation_id'], 0, $_POST['opened_thread']['last_message_id'])['messages'];
        /* assign variables */
        $smarty->assign('messages', $messages);
        /* return */
        $last_message = end($messages);
        $conversation['is_me'] = ($last_message['user_id'] == $user->_data['user_id']) ? true : false;
        $conversation['messages_count'] = count($messages);
        $conversation['messages'] = $smarty->fetch("ajax.chat.messages.tpl");
      }
      /* [2] check if any recipient typing */
      if ($conversation['typing_name_list']) {
        $return_this = true;
      }
      /* [3] check if any recipient seen */
      if ($conversation['seen_name_list']) {
        $return_this = true;
      }
      /* return */
      if ($return_this) {
        $return['thread_updated'] = $conversation;
      }
    }
  }

  // [8] [get] new calls (audio|video)
  $return['has_call'] = false;
  if ($system['audio_call_enabled'] || $system['video_call_enabled']) {
    $call = $user->check_new_calls();
    if ($call) {
      $return['has_call'] = true;
      $return['call'] = $call;
    }
  }

  // return & exit
  return_json($return);
} catch (Exception $e) {
  modal("ERROR", __("Error"), $e->getMessage());
}
