<?php

namespace App\Controllers;
use App\Repositories\ConnectionRepository;
use App\Repositories\MessageRepository;
use App\Repositories\NotificationRepository;
use App\Repositories\PostRepository;
use App\Repositories\UserRepository;

/**
*
*@author: Tiamiyu waliu kola
*@website : www.crea8social.com
*/
class UserUpdateController extends \BaseController
{
    public function __construct(
        NotificationRepository $notificationRepository,
        MessageRepository $messageRepository,
        ConnectionRepository $connectionRepository,
        UserRepository $userRepository,
        PostRepository $postRepository
    )
    {
        parent::__construct();
        $this->notificationRepository = $notificationRepository;
        $this->messageRepository = $messageRepository;
        $this->connectionRepository = $connectionRepository;
        $this->userRepository = $userRepository;
        $this->postRepository = $postRepository;
    }
    public function process()
    {
        $results = [
            'status' => 'login'
        ];

        if (!\Auth::check()) {
            return json_encode($results);
        }

        $results['status'] = true;
        $messageUserId = \Input::get('messageuserid');
        $postType = \Input::get('postType');
        $currentTime = date('Y-m-d H:i:s', time());

        $results['messagelastcheck'] = $currentTime;
        $results['postlastcheck'] = $currentTime;
        $results['onlines'] = $this->userRepository->countFriendsOnline();
        $results['notifications'] = $this->notificationRepository->count();
        $results['requests'] = $this->connectionRepository->countFriendRequest();

        if ($messageUserId) {
            $lastime = \Input::get('messagelastcheck');

            if($lastime) {

                $messages = $this->messageRepository->getList($messageUserId, 10, 0, $lastime);
                $results['messages'] = (string) $this->theme->section('messages.paginate', ['messages' => $messages]);

            }
        }
        $results['unreadmessage'] = $this->messageRepository->countNew();

        if ($postType) {
            $lastime = \Input::get('postlastcheck');
            if ($lastime) {
                $results['posts'] = (String) $this->theme->section('post.paginate', [
                    'posts' => $this->postRepository->lists($postType, 0, \Input::get('postUserid'), $lastime)
                ]);
            }
        }
        return json_encode($results);
    }
}