<?php

namespace App\Soshot;

use App\DataBase\DataBase;
use App\Utils\Hmac;
use App\Utils\Error;
use App\Utils\ResizeToDemande;

class GetThumb {

    protected $params;
    protected $type;
    protected $filePath;
    protected $receiveHmac;
    protected $requestImg;
    protected $queuePath = __DIR__ . '/../../cache/queue/';
    protected $complete;
    protected $conf;
    protected $db;
    protected $demande;

    function __construct($params, $conf) {
        $hmac = new Hmac($conf->key);
        $this->conf = $conf;
        $this->receiveHmac = $params->hmac;

        if ($hmac->checkHmac($this->receiveHmac, $params->url) === false) {
            $message = (object) ['status' => 404, 'message' => 'Wrong Hmac'];
            $error = new Error();
            $error->index($message);
        }

        $this->params = $params;
        $this->type = $params->type;
        $this->filePath = $hmac->makeFilePath($this->receiveHmac);
        if ($this->type === 'pdf') {
            $this->requestImg = $this->filePath . $this->receiveHmac . '_' . $this->type . '.pdf';
        } else {
            $this->requestImg = $this->filePath . $this->receiveHmac . '_' . $this->type . '.png';
        }
        $this->complete = __DIR__ . '/../../cache/img/' . substr($this->params->hmac, 0, 4) . '/' . $this->params->hmac . '_complete.png';

        $this->demande = (object)[
            'url' => $this->params->url,
            'hmac' => $this->params->hmac,
            'filePath' => $this->filePath,
            'requestImg' => $this->requestImg,
            'type' => $this->params->type
        ];

        $this->db = new DataBase($this->demande);

        return $this;
    }

    public function show() {
        if ($this->type === 'pdf' && file_exists($this->requestImg)) {
            header("Content-type:application/pdf");
            header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
            header("Content-Disposition:attachment;filename=\"archive.pdf\"");
            readfile($this->requestImg);
            echo file_get_contents($this->requestImg);
            exit();
        } else if (file_exists($this->requestImg) && in_array($this->type, ['complete', 'full', 'hd', 'thumb'])) {
            header("Content-type: image/png");
            header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
            echo file_get_contents($this->requestImg);
            exit();
        }
        if (file_exists($this->complete) && in_array($this->type, ['complete', 'full', 'hd', 'thumb'])) {
            if (ResizeToDemande::makeDemande((object)[
                'complete' => $this->complete,
                'filePath' => $this->requestImg,
                'type' => $this->type
            ])) {
                $this->db->addUpdate(1);
                header("Content-type: image/png");
                header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
                echo file_get_contents($this->requestImg);
                exit();
            } else {
                // @todo log
            }
            exit();
        } else {
            $json = json_encode([
                'type' => $this->params->type,
                'hmac' => $this->params->hmac,
                'url' => $this->params->url,
                'filePath' => $this->requestImg,
                'complete' => __DIR__ . '/../../cache/img/' . substr($this->params->hmac, 0, 4) . '/' . $this->params->hmac . '_complete.png'
            ]);

            $completeDemande = (object)[
                'url' => $this->params->url,
                'hmac' => $this->params->hmac,
                'filePath' => $this->filePath,
                'requestImg' => $this->requestImg,
                'type' => 'complete'
            ];
            $complete = new DataBase($completeDemande);
            $complete->addUpdate(3);

            if ($this->conf->alwaysMakePdf === true) {
                $complete->addUpdate(3, 'pdf');
            }

            $this->addQueue($json);

            header("Content-type: image/png");
            header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
            if ($this->type === 'fav') {
                echo file_get_contents(__DIR__ . '/../../src/images/error_fav.png');
            } else {
                echo file_get_contents(__DIR__ . '/../../src/images/loadingGen.png');
            }
            exit();
        }
    }

    private function addQueue($json) {
        $file = $this->queuePath . $this->receiveHmac . '.json';
        $this->db->addUpdate(3);
        if (!file_exists($file)) {
            file_put_contents($file, $json);
        }
    }
}
