<?php /** @noinspection PhpIllegalPsrClassPathInspection */

namespace Kicken\Chat;


class 
ChatUser {
    private 
ChatServer $mServer;
    private 
string $mUid;
    private 
string $mNick;
    private 
int $mLastSocketTime 0;
    private ?
StreamSocket $mActiveSocket null;
    private ?
string $mLastMessageId null;

    public function 
__construct(string $uidChatServer $server){
        
$this->mUid $uid;
        
$this->mServer $server;
    }

    public function 
setNick(string $n) : void{
        
$this->mNick $n;
    }

    public function 
getNick() : string{
        return 
$this->mNick;
    }

    public function 
getUid() : string{
        return 
$this->mUid;
    }

    public function 
isActive() : bool{
        return 
$this->mActiveSocket !== null && (time() - $this->mLastSocketTime) < 180;
    }

    public function 
setActiveRequest(HTTPClient $r null) : void{
        if (
$this->mActiveSocket !== $r && $this->mActiveSocket !== null){
            
$old $this->mActiveSocket;
            
$this->mActiveSocket null;

            
$old->Close();
        }

        
$this->mActiveSocket $r;
        if (
$r !== null){
            
$this->mLastSocketTime time();
        }
    }

    public function 
getActiveRequest() : ?StreamSocket{
        return 
$this->mActiveSocket;
    }

    public function 
getNewMessages() : array{
        
//echo "Getting messages for ".$this->mNick." that are after ".$this->mLastMessageId."\r\n";
        
$messages $this->mServer->getMessagesAfter($this->mLastMessageId);
        
$this->mLastMessageId $this->mServer->getNewestMessageId();

        return 
$messages;
    }
}