<?php

namespace Commander;

class Commander
{

    private $disabled=false;
    private $watcher;
    public $Log;

    public function __construct($watcher) 
        {
            $this->watcher = $watcher;
            $this->Log=$this->watcher->Log;
        }

    
        public function StartCommanding()
        {
            while(!$this->stopped)
            {
                $this->commandingCycle();
                break;
            }
        }

        public function CommandingCycle()
        {
            if($this->disabled)
            {
                $this->Log->LogString("Commander module disabled");
                return;
            }
            else
            {
                $this->Log->LogString("Commmander checking possible optimizations.");

                if(\Shared\Settings::$CommanderAutoResolveMissing)
                    $this->resolveMissingTorrents();

                if(\Shared\Settings::$CommanderAutoRemoveExtras)
                    $this->removeExtras();

                if(\Shared\Settings::$CommanderManageUploadSlots)
                    $this->manageUploadSlots();

                $this->Log->LogString("Commmander finished.");
            }
        }

        private function manageUploadSlots()
        {
            $this->Log->LogString("Commander managing upload slots.");
            $numberSeeds=array();
            //nacteni poctu seedů
            foreach($this->watcher->clients as $client)
            {
                if(!$client->Connected)
                continue;

                foreach($client->Torrents as $torr)
                {
                    //var_dump($torr->PercentProgress);
                    if($torr->PercentProgress>950)
                    { 
                        $numberSeeds[$torr->Hash]=isset($numberSeeds[$torr->Hash])?$numberSeeds[$torr->Hash]+1:1;//var_dump($numberSeeds[$torr->Hash]);
                    }
                }
               
            }
            //var_dump($numberSeeds);

            //nastaveni limitu
            foreach($this->watcher->clients as $client)
            {
                if(!$client->Connected)
                continue;

                foreach($client->Torrents as $torr)
                {
                    if(!isset($numberSeeds[$torr->Hash]))
                        continue;
                    $slots=$numberSeeds[$torr->Hash];
                    $props=$client->Api->getProperties($torr->Hash);
                        if($props[0]["ulslots"]!=$slots)
                        {
                            if($client->Api->setProperties($torr->Hash,"ulslots",$slots))
                                $this->Log->LogString("Set ".$slots." upload slots at ".$client->title." torrent ". $torr->Name);
                            else
                                $this->Log->LogString("Failed to set ".$slots." upload slots at ".$client->title." torrent ". $torr->Name);
                        }
                        else 
                        {
                            $this->Log->LogString($props[0]["ulslots"]." upload slots is fine for ".$client->title." torrent ". $torr->Name);
                        }
                }
            }
        }

        private function removeExtras()
        {
            
            $this->Log->LogString("Commander removing extra torrents.");

            foreach($this->watcher->clients as $client )
            { 
                if(!$client->Connected)
                    continue;

                foreach($client->ExtraTorrents as $extraTorrent)
                {
                    $res=true;
                    if(\Shared\Settings::$CommanderRemoveWithFiles)
                        $res=$client->Api->torrentDataRemove($extraTorrent->Hash);
                        
                    $res&=$client->Api->torrentRemove($extraTorrent->Hash);
                    if(!$res)
                    {
                        $this->Log->LogString("Failed to remove extra". $extraTorrent->Name." from ".$client->IPAddress.":".$client->Port." .");
                    }
                    else
                    {
                        $this->Log->LogString("Extra torrent". $extraTorrent->Name." successfully  removed from ".$client->IPAddress.":".$client->Port);
                    }
                }

            }
        }
        
        private function resolveMissingTorrents()
        {
            
            $this->Log->LogString("Commander resolving missing torrents.");

            foreach($this->watcher->clients as $client )
            { 
                if(!$client->Connected)
                    continue;

                foreach($client->MissingTorrents as $missingTorrent)
                {
                    $fileURI= sprintf(\Shared\Settings::$CommanderTorrentDownloadURI,$missingTorrent->Id);
                    //var_dump($fileURI);
                    $returnString="";
                    $res=$client->Api->torrentAdd($fileURI,$returnString);//var_dump($client->Api);
                    if(!$res)
                    {
                        $this->Log->LogString("Failed to remove ". $missingTorrent->Name." at ".$client->IPAddress.":".$client->Port." due to: ".$returnString);
                    }
                    else
                    {
                        $this->Log->LogString($missingTorrent->Name." successfully  added to ".$client->IPAddress.":".$client->Port);
                    }
                }

            }
        }

    }