<?php

namespace MediaAlchemyst\Tests;

use FFMpeg\Exception\ExecutableNotFoundException as FFMpegExecutableNotFound;
use Monolog\Logger;
use Monolog\Handler\NullHandler;
use MediaVorus\MediaVorus;
use PHPExiftool\Reader;
use PHPExiftool\RDFParser;
use PHPExiftool\Writer;
use PHPExiftool\Exiftool;
use FFMpeg\FFProbe;
use MediaVorus\Utils\AudioMimeTypeGuesser;
use MediaVorus\Utils\PostScriptMimeTypeGuesser;
use MediaVorus\Utils\RawImageMimeTypeGuesser;
use MediaVorus\Utils\VideoMimeTypeGuesser;
use Neutron\TemporaryFilesystem\Manager;
use Neutron\TemporaryFilesystem\TemporaryFilesystem;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Mime\MimeTypes;

class AbstractAlchemystTester extends TestCase
{
    public function getMediaVorus()
    {
        static $initialized;

        if (null === $initialized) {
            $customGuessers = [
                new RawImageMimeTypeGuesser(),
                new PostScriptMimeTypeGuesser(),
                new AudioMimeTypeGuesser(),
                new VideoMimeTypeGuesser(),
            ];

            $mimeTypes = new MimeTypes();

            foreach ($customGuessers as $guesser) {
                $mimeTypes->registerGuesser($guesser);
            }

            MimeTypes::setDefault($mimeTypes);

            $initialized = true;
        }

        return new MediaVorus($this->getReader(), $this->getWriter(), $this->getProbe());
    }

    public function getExiftool()
    {
        $logger = new Logger('test');
        $logger->pushHandler(new NullHandler());

        return new Exiftool($logger);
    }

    public function getReader()
    {
        return Reader::create($this->getExiftool(), $this->getRDFParser());
    }

    public function getWriter()
    {
        return Writer::create($this->getExiftool());
    }

    public function getProbe()
    {
        try {
            return FFProbe::create();
        } catch (FFMpegExecutableNotFound $e) {

        }

        return null;
    }

    public function getFsManager()
    {
        $fs = new Filesystem();

        return new Manager(new TemporaryFilesystem($fs), $fs);
    }

    public function getRDFParser()
    {
        $logger = new Logger('test');
        $logger->pushHandler(new NullHandler());
        return new RDFParser("/tmp", $logger);
    }
}
