<?php

namespace IZON\Thumber\Web\Interceptors;

use IZON\MVC\HttpRequest;
use IZON\MVC\Interceptors\Interceptor;
use IZON\MVC\ModelAndView;
use IZON\Thumber\Services\ThumbnailServiceInterface;
use IZON\Thumber\Thumbnailer;
use IZON\Thumber\Web\Utils\WebPImageUtils;
use IZON\UserAgent\Exceptions\UserAgentParseException;
use IZON\UserAgent\UserAgentParser;

/**
 * nacita informace o ga
 */
class ThumberInterceptor implements Interceptor {
    /**
     *
     * @var ThumbnailServiceInterface
     */
    protected $thumbnailService;

    public function __construct(ThumbnailServiceInterface $thumbnailService) {
        $this->thumbnailService = $thumbnailService;
    }

    public function afterCompletion(HttpRequest $request, $exception) {
        
    }

    public function preHandle(HttpRequest $request) {
        if( WebPImageUtils::hasWebPSupport(getallheaders()) ) {
            // Do something for WebP users
            $this->thumbnailService->setFormat('webp');
        }
    }

    public function postHandle(HttpRequest $request, ModelAndView $model) {
        // adds information about gallery
        $model->putParameter(Thumbnailer::AVAILABLE_AS, $this->thumbnailService->getThumbnailer());
        return $model;
    }

}
