<?php

namespace Watcher;

class DBTorrent extends \Shared\Torrent
{
    public static $tableName="torrentView";
    public static $removeTableName="torrent";
    private static $colID="id";
    public static $colClientID= "id_client";

    public static $colHash="hash";
	public static $colStatus="status";
	public static $colName="name";
	public static $colSize="size";
	public static $colPercentProgress="percentProgress";
    public static $colDownloadedBytes="downloadedBytes";
	public static $colUploadedBytes="uploadedBytes";
	public static $colRatio="Ratio";
	public static $colUploadSpeed="uploadSpeed";
	public static $colDownloadSpeed="downloadSpeed";
	public static $colETA="ETA";
	public static $colLabel="Label";
    public static $colPeersConnected="peersConnected";
	public static $colPeersInSwarm="peersInSwarm";
	public static $colSeedsConnected="seedsConnected";
	public static $colSeedsInSwarm="seedsInSwarm";
	public static $colAvailability="Availability";
	public static $colQueueOrder="QueueOrder";
    public static $colRemainingBytes="RemainingBytes";	
    
    public static $colLastUpdate="last_update";
    public static $colIsForDistribution="forDistribution";
    public static $colStarted="started";
    public static $colPaused="paused";

    public static $colClientAddress="clientAddress";
    public static $colClientPort="clientPort";
    public static $colClientName="clientTitle";

       
    protected $id=null;
    protected $lastUpdate;

    public $isForDistribution;
    
    
    public function __construct( $tor, $client)
    {   if(is_array($tor))
        {
            parent::__construct($tor,$client);  
        }
        else
        {
            parent::__construct(array_values((array)$tor),$tor->client);  
        }
        $this->Ratio=$this->Ratio/1000;
    }

    public function Delete()
    {
        if($this->id==null) throw new \Exception("Unsupported deleting torrent with ID null.");
        return $sql=\Shared\MySQL::GetMySQL()->where(array(self::$colID=>$this->id))->delete(self::$removeTableName);
    }

 
    public function UpdateOrSave()
    {
        $this->lastUpdate = new \DateTime();
        
        if($this->id ==null )
        { //insert
            \Shared\Debug::Instance()->Activate()->Display("Inserting torrent",$this)->Deactivate();
        $sql=\Shared\MySQL::GetMySQL()->insert(self::$removeTableName, array(  
            self::$colLastUpdate => $this->lastUpdate->format('Y-m-d H:i:s'),
            self::$colClientID=> $this->client->GetID(),
            self::$colHash=>$this->Hash,
            self::$colStatus=>$this->Status,
            self::$colName=>$this->Name,
            self::$colSize=>$this->Size,
            self::$colPercentProgress=>$this->PercentProgress/10,
            self::$colDownloadedBytes=>$this->DownloadedBytes,
            self::$colUploadedBytes=>$this->UploadedBytes,
            self::$colRatio=>$this->Ratio,
            self::$colUploadSpeed=>$this->UploadSpeed,
            self::$colDownloadSpeed=>$this->DownloadSpeed,
            self::$colETA=>$this->ETA,
            self::$colLabel=>$this->Label,
            self::$colPeersConnected=>$this->PeersConnected,
            self::$colPeersInSwarm=>$this->PeersInSwarm,
            self::$colSeedsConnected=>$this->SeedsConnected,
            self::$colSeedsInSwarm=>$this->SeedsInSwarm,
            self::$colAvailability=>$this->Availability,
            self::$colQueueOrder=>$this->QueueOrder,
            self::$colRemainingBytes=>$this->RemainingBytes,
            self::$colIsForDistribution=>$this->isForDistribution?1:0,
            self::$colPaused=>$this->Paused?1:0,
            self::$colStarted=>$this->Started?1:0)
        );

        }
        else
        {//update
            \Shared\Debug::Instance()->Activate()->Display("Updating torrent",$this)->Deactivate();
            $sql=\Shared\MySQL::GetMySQL()->where(self::$colID, $this->id)->update(self::$removeTableName, array(  
                self::$colLastUpdate => $this->lastUpdate->format('Y-m-d H:i:s'),
                self::$colClientID=> $this->client->GetID(),
                self::$colHash=>$this->Hash,
                self::$colStatus=>$this->Status,
                self::$colName=>$this->Name,
                self::$colSize=>$this->Size,
                self::$colPercentProgress=>$this->PercentProgress/10,
                self::$colDownloadedBytes=>$this->DownloadedBytes,
                self::$colUploadedBytes=>$this->UploadedBytes,
                self::$colRatio=>$this->Ratio,
                self::$colUploadSpeed=>$this->UploadSpeed,
                self::$colDownloadSpeed=>$this->DownloadSpeed,
                self::$colETA=>$this->ETA,
                self::$colLabel=>$this->Label,
                self::$colPeersConnected=>$this->PeersConnected,
                self::$colPeersInSwarm=>$this->PeersInSwarm,
                self::$colSeedsConnected=>$this->SeedsConnected,
                self::$colSeedsInSwarm=>$this->SeedsInSwarm,
                self::$colAvailability=>$this->Availability,
                self::$colQueueOrder=>$this->QueueOrder,
                self::$colRemainingBytes=>$this->RemainingBytes,
                self::$colIsForDistribution=>$this->isForDistribution?1:0,
                self::$colPaused=>$this->Paused?1:0,
                self::$colStarted=>$this->Started?1:0));
        }
    }

    public static function GetTorrentsByClientID($clientID)
    {
        $arr=\Shared\MySQL::GetMySQL()->where(self::$colClientID, $clientID)->get(self::$tableName,self::GetColsForSelect());
        if(!is_array($arr))
            return array();
        return $arr;
    }
    public static function GetTorrentByClientAndHash($clientID,$Hash)
    {
        $arr=\Shared\MySQL::GetMySQL()->where(self::$colClientID, $clientID)->where(self::$colHash,$Hash)->get(self::$tableName,self::GetColsForSelect());
        if(!is_array($arr))
            return array();
        return $arr[0];
    }
    public static function GetTorrentByID($id)
    {
        $arr=\Shared\MySQL::GetMySQL()->where(self::$colID, $id)->get(self::$tableName,self::GetColsForSelect());
        if(!is_array($arr))
            return array();
        return $arr[0];
    }

    public static function GetTorrentsWithClientDataByTorrentName($name)
    {
        $arr=\Shared\MySQL::GetMySQL()->where(self::$colName,$name)->get(self::$tableName,self::GetColsForSelect());
       // var_dump($name);
        if(!is_array($arr))
            return array();
        return $arr;
    }

    public static function LoadTorrentsFromDatabase(\Watcher\DBuTorrentClient $client)
    {
        $torrentRows=self::GetTorrentsByClientID($client->GetID());
        $torrents=array();
        if(!is_array($torrentRows)) return array();
        foreach($torrentRows as $torRow)
        {
            $loadedTorrent= new DBTorrent(array_values((array)$torRow),$client);
            $loadedTorrent->SetId($torRow[self::$colID]);
            
            if(array_key_exists($loadedTorrent->Name,$torrents))
            // pokud je v db nejaky torrent dvakrat z duvodu predchozi chyby, hned ho mazeme 
            {
                $loadedTorrent->Delete();
            }
            $torrents[$loadedTorrent->Name]=$loadedTorrent;

        }
        return $torrents;

    }

    public function SetId($idd)
    {
        $this->id =$idd;
    }

    public function GetID()
    {
    return $this->id;
    }

    public static function GetColsForSelect()
    {
        return array(
            self::$colHash,
            self::$colStatus,
            self::$colName,
            self::$colSize,
            self::$colPercentProgress,
            self::$colDownloadedBytes,
            self::$colUploadedBytes,
            self::$colRatio,
            self::$colUploadSpeed,
            self::$colDownloadSpeed,
            self::$colETA,
            self::$colLabel,
            self::$colPeersConnected,
            self::$colPeersInSwarm,
            self::$colSeedsConnected,
            self::$colSeedsInSwarm,
            self::$colAvailability,
            self::$colQueueOrder,
            self::$colRemainingBytes,
            self::$colID,
            self::$colIsForDistribution,
            self::$colClientName,
            self::$colClientAddress,
            self::$colClientPort,
            self::$colClientID,
            self::$colPaused,
            self::$colStarted
        );
    }
}