<?php

namespace Watcher;

use Shared\DBTorrentToDistribute;


class WatcherREST extends \Shared\AbstractREST
{
    protected function getRespond($data)
    {
        switch($this->class)
        {
            case "status": return $this->statusGetRespond($data);
            case "clients": return $this->clientsGetRespond($data);
            case "distlist": return $this->distListGetRespond($data);
            default: http_response_code(404);die();
        }
    }

    protected function postRespond($data)
    {
        switch($this->class)
        {
            case "status": return $this->statusPostRespond($data);
            default: http_response_code(404);die();
        }
    }
    private function statusGetRespond($data)
    {
        return WatcherInterface::GetWatcherData();
    }

    private function statusPostRespond($data)
    {
        return WatcherInterface::ChangeWatcherStatus();
    }

    private function clientsGetRespond($data)
    {
        return WatcherInterface::GetClients();
    }
    private function distListGetRespond($data)
    {
        if($this->id !=null && $this->subclass=="file")
        {
            $torr=\Shared\DBTorrentToDistribute::GetTorrentDistributionByID($this->id);
            if($torr==null) return null;
            $res=$torr->GetData();//var_dump( $res);die();
            $fileName=$torr->Name .".torrent";
            header("Content-Type: application/x-bittorrent");
            header("Content-Transfer-Encoding: Binary");
            header("Content-disposition: attachment; filename=\"".$fileName."\""); 
            die ($res);
        }

        if($this->id !=null)
        {
            return  \Shared\DBTorrentToDistribute::AppendClientsData(\Shared\DBTorrentToDistribute::GetTorrentDistributionByID($this->id));
        }

        return \Shared\DBTorrentToDistribute::AppendClientsData(\Shared\DBTorrentToDistribute::GetTorrentsDistributionList(false));
    }

}
