<?php

namespace App\Addons\Chatbox\Controllers;
use App\Repositories\MessageRepository;
use App\Repositories\UserRepository;

/**
*
*@author: Tiamiyu waliu kola
*@website : www.crea8social.com
*/
class ChatboxController extends \BaseController
{
    public function __construct(MessageRepository $messageRepository, UserRepository $userRepository)
    {
        parent::__construct();
        $this->messageRepository = $messageRepository;
        $this->userRepository = $userRepository;
    }

    public function send()
    {
        $userid = \Input::get('userid');
        $text = \Input::get('text');
        $message = $this->messageRepository->send($userid, $text, \Input::file('image'));

        $content = '';

        if ($message) {
            $content .= $message->present()->text();

            if($message->image) {
                $img= "<img style='display:block' src='".\Image::url($message->image, 100)."'/>";
                $content .= "<a class='preview-image' rel='message-".$message->id."' href='".\Image::url($message->image, 600)."'>".$img.'</a>';
            }
        }

        $this->userRepository->savePrivacy(['typing-'.$userid => 0]);

        return $content;
    }

    public function loadEmoticon()
    {
        return $this->theme->section('chatbox::emoticons');
    }

    public function loadOld()
    {
        $userid = \Input::get('userid');
        $messages = $this->messageRepository->getList($userid, 10);
        $this->messageRepository->markAllByUser($userid);

        return $this->theme->section('chatbox::display-messages', ['messages' => $messages, 'user' => \Auth::user()]);
    }

    public function checkUpdate()
    {
        $boxes = \Input::get('ids');

        $results = [];
        $currentTime = date('Y-m-d H:i:s', time());

        foreach($boxes as $box) {
            $userid = $box[0];
            $lastcheck = $box[1];
            $results[$userid] = [];
            $user = $this->userRepository->findById($userid);
            $results[$userid]['status'] = ($user->present()->isOnline()) ? 1 : 0;
            $results[$userid]['typing'] = $user->present()->privacy('typing-'.\Auth::user()->id , 0);
            $results[$userid]['userid'] = $userid;
            $results[$userid]['lastcheck'] = $currentTime;
            $results[$userid]['messages'] = '';
            if ($lastcheck) {

                $messages = $this->messageRepository->getList($userid, 10, 0, $lastcheck);

                $results[$userid]['messages'] = (string) $this->theme->section('chatbox::display-messages', ['messages' => $messages, 'user' => \Auth::user()]);
            }
        }

        return json_encode($results);

    }

    public function loadOlder()
    {
        $limit = 10 ;
        $userid = \Input::get('userid');
        $offset = \Input::get('offset');
        $offset = (empty($offset)) ? $limit : $offset;
        $newOffset = $offset  +  $limit;


        return json_encode([
            'offset' =>$newOffset,
            'content' => (String)  $this->theme->section('chatbox::display-messages', ['user' => \Auth::user(),'messages' => $this->messageRepository->getList($userid, $limit, $offset)])
        ]);
    }

    public function typing()
    {
        $this->userRepository->savePrivacy(['typing-'.\Input::get('id') => 1]);
    }

    public function offtyping()
    {
        $this->userRepository->savePrivacy(['typing-'.\Input::get('id') => 0]);
    }
}