<?php

namespace Kicken\Chat;


class 
ChatUser {
    
/** @var ChatServer */
    
private $mServer;
    
/** @var int */
    
private $mUid;
    
/** @var string */
    
private $mNick;
    
/** @var int */
    
private $mLastSocketTime 0;
    
/** @var resource */
    
private $mActiveSocket null;
    
/** @var int */
    
private $mLastMessageId null;

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

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

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

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

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

    public function 
setActiveRequest(HTTPClient $r null){
        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(){
        return 
$this->mActiveSocket;
    }

    public function 
getNewMessages(){
        
//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;
    }
}