<?php

namespace Shared;
class Torrent
{
    public $Hash;
    public $Status;
    public $Name;
    public $Size;
    public $PercentProgress;
    public $DownloadedBytes;
    public $UploadedBytes;
    public $Ratio;
    public $UploadSpeed;
    public $DownloadSpeed;
    public $ETA;
    public $Label;
    public $PeersConnected;
    public $PeersInSwarm;
    public $SeedsConnected;
    public $SeedsInSwarm;
    public $Availability;
    public $QueueOrder;
    public $RemainingBytes;
    public $Started;
    public $Paused;
    public $ClientID;

    protected $lastUpdate;

    protected $client;
    
#region statics

    public static $StatusDescpriptions = array(
            1 => "Started",
            2 => "Checking",
            4 => "Start after check",
            8 => "Checked",
            16 => "Error",
            32 => "Paused",
            64 => "Queued",
            128 => "Loaded"
        );


    public static function GetStatesString($state)
    {
        $states=array();
        foreach(self::$StatusDescpriptions as $key=>$statDesc)
            {   
                if($state & $key)
                    array_push($states,$statDesc);
            }
        return implode(", ",$states);
    }
    public static function IsChecking($state)
    {   
        return (($state & 2)==2);
    }

    public static function IsChecked($state)
    {
        return (($state & 8)==8);
    }

    public static function IsStarted($state)
    {
        return (($state & 1) == 1);
    }
    public static function IsPaused($state)
    {
        return (($state & 32)==32) ;
    }
    public static function IsError($state)
    {
        return (($state & 16) == 16);
    }
#endregion statics

    function __construct($myarray, $cl) 
        {
            if(sizeof($myarray)<19) throw new \InvalidArgumentException("Invalid arguments");            
            $this->Hash=$myarray[0];
            $this->Status=$myarray[1];
            $this->Name=$myarray[2];
            $this->Size=$myarray[3];
            $this->PercentProgress=$myarray[4];
            $this->DownloadedBytes=$myarray[5];
            $this->UploadedBytes=$myarray[6];
            $this->Ratio=$myarray[7];
            $this->UploadSpeed=$myarray[8];
            $this->DownloadSpeed=$myarray[9];
            $this->ETA=$myarray[10];
            $this->Label=$myarray[11];
            $this->PeersConnected=$myarray[12];
            $this->PeersInSwarm=$myarray[13];
            $this->SeedsConnected=$myarray[14];
            $this->SeedsInSwarm=$myarray[15];
            $this->Availability=$myarray[16];
            $this->QueueOrder=$myarray[17];
            $this->RemainingBytes=$myarray[18];
            $this->client=$cl;
            $this->ClientID=$cl->GetID();
            $this->lastUpdate= new \DateTime();
            $this->Started= self::IsStarted($this->Status);
            $this->Paused = self::IsPaused($this->Status);
        }

    public function GetClient()
    {
        return $this->client;
    }
}