<?php

namespace Commander;

class UTorrentClient
{
    public $IPAddress;
    public $Port;
    
    protected $lastUpdate;
    public $Connected;

    public $Torrents =array();
    
    public $Api;

    function __construct($IPaddr,$port=null) 
        {
            $this->IPAddress=$IPaddr;
            if($port==null)
                $this->Port=\Shared\Settings::$DefaultClientPort;
            else    
                $this->Port=$port; 
        }

    public function ConnectApi()
    {
        $this->Api = new uTorrentApi($this->IPAddress,$this->Port, \Shared\Settings::$DefaultClientLoginName, \Shared\Settings::$DefaultClientLoginPass);
        return $this->Api;
    }

    public function Actualize()
    {
       $this->ConnectApi();
        //var_dump($this->Api);
        $torrs=$this->Api->getTorrents();
        if($torrs ==null ) $torrs =array();
        foreach ($torrs as $torr)
            {            
                $torrent= new \Shared\Torrent($torr,$this);
                $this->Torrents[$torrent->Name]=$torrent;  
            }
        $this->Connected=$this->Api->IsConnected();

    }

    public function GetTorrents()
    {
        return $this->Torrents;
    }

}