<?php
namespace Watcher;

class WatcherInterface
{
    public static function GetWatcherData()
    {
        $data= Watcher::GetWatcher();
        $dateTime=new \DateTime($data["last_update"]);

        $data["last_update"]=$dateTime->format("j.n.Y h:i:s");
        $data["serviceRunning"]=\Shared\PInfo::IsPIDActive($data["last_pid"]);
        $data["paused"]=$data["paused"]==1;   
        $data["logFile"]=\Shared\Settings::$WatcherServiceLogURL;    
        return $data;
    }

    public static function ChangeWatcherStatus()
    {

        $result=true;
        $newstatus=json_decode($_POST["status"],true);
        if(@$newstatus["paused"]===true)
            $result= $result && self::PauseService();

        if(@$newstatus["paused"]===false){
            $result= $result && self::ResumeService();}

        if(@$newstatus["serviceRunning"]===false)
            $result= $result && self::KillService();
           
        if(@$newstatus["serviceRunning"]===true)
        {
            $result= $result && self::ResumeService();
            self::StartService();
        }
        return $result;
    }

    public static function GetClients()
    {
       
        $clients=DBuTorrentClient::GetClientsData();
        if(!is_array($clients))
            return array();
        foreach($clients as $key=>$client)
        {
            if($client[DBuTorrentClient::$colConnected] && $client[DBuTorrentClient::$colActive])
            {
                self::GetTorrentsForClient($client);
                $clients[$key]=$client;
            }
        }//var_dump($clients);
        
        return $clients;
    }

    public static function GetTorrentsForClient(& $clientRow)
    {   
        $torrs=DBTorrent::GetTorrentsByClientID($clientRow[DBuTorrentClient::$colID]);
        $sumPercentScaled=0.0001;
        $maxPercentScaled=0.0001;
        $sumUpload=0;
        $sumDownload=0;
        $someExtra=false;
        foreach($torrs as $key=>$torrent)
        {//var_dump($torrent);
            $torrs[$key]["asp"]=$torrent[DBTorrent::$colSeedsConnected]."/".$torrent[DBTorrent::$colPeersConnected];
            $torrs[$key]["sp"]=$torrent[DBTorrent::$colSeedsInSwarm]."/".$torrent[DBTorrent::$colPeersInSwarm];
            $torrs[$key]["stats"]=\Shared\Torrent::GetStatesString($torrent[DBTorrent::$colStatus]);
            $torrs[$key]["forDistribution"]=$torrs[$key]["forDistribution"]==1;
            $torrs[$key]["started"]=$torrs[$key]["started"]==1;
            $torrs[$key]["paused"]=$torrs[$key]["paused"]==1;
            $torrs[$key]["checking"]=\Shared\Torrent::IsChecking($torrent[DBTorrent::$colStatus]);
            if($torrent[DBTorrent::$colIsForDistribution]!=1)
                $someExtra=true;

            if(!$torrs[$key]["checking"])
                $sumPercentScaled+=(int)$torrent[DBTorrent::$colPercentProgress]*(int)$torrent[DBTorrent::$colSize];
            
            $maxPercentScaled+=100*(int)$torrent[DBTorrent::$colSize];
            $sumUpload+=$torrent[DBTorrent::$colUploadSpeed];
            $sumDownload+=$torrent[DBTorrent::$colDownloadSpeed];
        }
        $clientRow["sumPercentDownloaded"]=(($sumPercentScaled/$maxPercentScaled)*100);
        $clientRow["torrentCount"]=count($torrs);
        $clientRow["sumUpload"]=$sumUpload;
        $clientRow["sumDownload"]=$sumDownload;
        $clientRow["someExtra"]=$someExtra;
        $clientRow["torrents"]=$torrs;
    }

    public static function GetTorrentDistributionList()
    {

    }

    public static function KillService()
    {
        \Shared\Messages::Error("Sorry, there was no time to implement stopping...");
        
        $watcher= Watcher::GetWatcher();
        if(\Shared\PInfo::IsPIDActive($watcher[Watcher::$colLastPID]))
            return posix_kill($watcher[Watcher::$colLastPID]);

        
        if(!\Shared\Settings::$WatcherServiceScriptPath)// pokud neni aplikace konfigurovana nedovoli toto udelat
            return false;
        return false; 
    }

    public static function StartService()
    {        
        $watcher= Watcher::GetWatcher();
        if(\Shared\PInfo::IsPIDActive($watcher[Watcher::$colLastPID]))
            return true;
        \Shared\Messages::Error("Sorry, there was no time to implement starting...");
        if(!\Shared\Settings::$WatcherServiceScriptPath)// pokud neni aplikace konfigurovana nedovoli toto udelat
            return false;
        return false;
    }

    public static function PauseService()
    {
        Watcher::UpdateWatcherData(array(Watcher::$colPaused=>1));
        \Shared\Messages::Information("Service is now paused.");
        return true;
    }

    public static function ResumeService()
    {
        Watcher::UpdateWatcherData(array(Watcher::$colPaused=>0));
        \Shared\Messages::Information("Service is now resumed.");
        return true;
    }
}