<?php
/**
 * @package    Proxim
 * @author     Mike Kivu <mikepaul620@gmail.com | https://webexpert.co.ke>
 * @copyright  2019 Proxim
 * @version    1.5.0
 * @since      File available since Release 1.0.0
 */

namespace Proxim\Api;

use Db;
use Proxim\Database\DbQuery;
use Proxim\Configuration;
use Proxim\Mail;
use Proxim\Tools;
use Proxim\Validate;
use Proxim\Controller;
use Proxim\Order\Order;
use Proxim\User\Employee;
use Proxim\Util\ArrayUtils;
use Proxim\Util\DateUtils;
use Proxim\Crypto\Hashing;

class Updates extends Controller {

    public function getLiveData() {
        $app = $this->app;
        $this->assignVariables();

        $payload = $app->request->post();
        $user = $app->container->user;
        $smarty = $app->container->smarty;

        /* if (last_message || last_notification) not set */
        $last_message = (int) ArrayUtils::get($payload, 'last_message');
        $last_notification = (int) ArrayUtils::get($payload, 'last_notification');

        // initialize the return array
        $return = array();

        // [2] check for new version
        $update = $app->checkVersion();
        if($update) {
            $return['new_version'] = $update['version'];
        }
        
        // [2] check for new notifications
        $notifications = $user->getNotifications(0, $last_notification );

        if(count($notifications) > 0) {
            /* assign variables */
            $smarty->assign('notifications', $notifications);
            /* return */
            $return['notifications_json'] = $notifications;
            $return['notifications_count'] = count($notifications);
            $return['notifications'] = $smarty->fetch("ajax.live.notifications.tpl");
        }

        // [3] check for new messages
        if($last_message != $user->live_messages_lastid) {
            $messages = $user->getMessages(0, $last_message );

            if(count($messages) > 0) {
                /* assign variables */
                $smarty->assign('messages', $messages);
                /* return */
                $return['messages_json'] = $messages;
                $return['messages_count'] = count($messages);
                $return['messages'] = $smarty->fetch("ajax.live.messages.tpl");
            }
        }

        return $this->response($return);
    }

    public function load() {
        $app = $this->app;
        $this->assignVariables();

        $payload = $app->request->post();
        $user = $app->container->user;
        $smarty = $app->container->smarty;

        $get = ArrayUtils::get($payload, 'get');
        $offset = (int) ArrayUtils::get($payload, 'offset');

        // initialize the return array
        $return = $data = array();

        // initialize the attach type
	    $append = true;

        /* get notifications */
        if ( $get == "notifications" ) {
            $data = $user->getNotifications($offset);
        }
        
        // handle data
        if(count($data) > 0) {
            /* assign variables */
            $smarty->assign('offset', $offset );
            $smarty->assign('get', $get );
            $smarty->assign('data', $data);
            /* return */
            $return['append'] = $append;
            $return['data'] = $smarty->fetch("ajax.load_more.tpl");
        }

        return $this->response($return);
    }

    public function reset() {
        $app = $this->app;
        $payload = $app->request->post();
        $user = $app->container->user;

        $counter = ArrayUtils::get($payload, 'reset');

        if ($counter == "notifications") {
            $user->live_notifications_counter = 0;
            $user->update();

            if($user->is_admin) {
                Db::getInstance()->update(
                    'notification', 
                    array('is_seen' => 1), 
                    'to_user_id = ' . (int) $user->id . ' AND to_department = ' . DEPARTMENT_SUPPORT 
                );
            } else {
                Db::getInstance()->update(
                    'notification', 
                    array('is_seen' => 1), 
                    'to_user_id = ' . (int) $user->id . ' AND to_department = ' . DEPARTMENT_WRITER 
                );
            }

        } elseif ( $counter == "messages" ) {
            $user->live_messages_counter = 0;
            $user->update();
        }

        return $this->response([
            "success" => true,
        ]);
    }

}