<?php

namespace App\Tests\Unit\Service\File;

use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Metadata\Get;
use App\ApiResources\Core\File\DownloadRequest;
use App\Entity\Core\File;
use App\Enum\Core\FileHostEnum;
use App\Service\File\Exception\FileNotFoundException;
use App\Service\File\FileManager;
use App\Service\File\Storage\ApiLegacyStorage;
use App\Service\File\Storage\FileStorageInterface;
use App\Service\File\Storage\LocalStorage;
use PHPUnit\Framework\TestCase;

class FileManagerTest extends TestCase
{
    private LocalStorage $localStorage;
    private ApiLegacyStorage $apiLegacyStorage;
    private IriConverterInterface $iriConverter;

    public function setUp(): void
    {
        $this->localStorage = $this->getMockBuilder(LocalStorage::class)->disableOriginalConstructor()->getMock();
        $this->apiLegacyStorage = $this->getMockBuilder(ApiLegacyStorage::class)->disableOriginalConstructor()->getMock();
        $this->iriConverter = $this->getMockBuilder(IriConverterInterface::class)->disableOriginalConstructor()->getMock();
    }

    public function getFileManagerMockFor(string $methodName) {
        return $this->getMockBuilder(FileManager::class)
            ->setConstructorArgs([$this->localStorage, $this->apiLegacyStorage, $this->iriConverter])
            ->setMethodsExcept([$methodName])
            ->getMock();
    }

    public function testGetStorageFor(): void {
        $fileManager = $this->getFileManagerMockFor('getStorageFor');

        $file1 = $this->getMockBuilder(File::class)->getMock();
        $file1->method('getHost')->willReturn(FileHostEnum::API1()->getValue());

        $file2 = $this->getMockBuilder(File::class)->getMock();
        $file2->method('getHost')->willReturn(FileHostEnum::AP2I()->getValue());

        $this->assertEquals(
            $fileManager->getStorageFor($file1),
            $this->apiLegacyStorage
        );

        $this->assertEquals(
            $fileManager->getStorageFor($file2),
            $this->localStorage
        );
    }

    public function testGetStorageForUnknown(): void {
        $fileManager = $this->getFileManagerMockFor('getStorageFor');

        $file = $this->getMockBuilder(File::class)->getMock();
        $file->method('getHost')->willReturn('unknown');

        $this->expectException(FileNotFoundException::class);

        $fileManager->getStorageFor($file);
    }

    public function testRead(): void {
        $fileManager = $this->getFileManagerMockFor('read');

        $file = $this->getMockBuilder(File::class)->getMock();

        $storage = $this->getMockBuilder(FileStorageInterface::class)->getMock();
        $storage->expects(self::once())->method('read')->with($file)->willReturn('foo');

        $fileManager->expects(self::once())->method('getStorageFor')->with($file)->willReturn($storage);

        $this->assertEquals(
            $fileManager->read($file),
            'foo'
        );
    }

    /**
     * @see LocalStorage::guessMimeTypeFromFilename()
     */
    public function testGuessMimeTypeFromFilename(): void {
        $this->assertEquals('application/pdf', FileManager::guessMimeTypeFromFilename('file.pdf'));
        $this->assertEquals('text/csv', FileManager::guessMimeTypeFromFilename('file.csv'));
        $this->assertEquals('text/plain', FileManager::guessMimeTypeFromFilename('file.txt'));
        $this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', FileManager::guessMimeTypeFromFilename('file.xlsx'));
        $this->assertEquals('application/xml', FileManager::guessMimeTypeFromFilename('file.xml'));

        $this->assertEquals(null, FileManager::guessMimeTypeFromFilename('file'));
        $this->assertEquals(null, FileManager::guessMimeTypeFromFilename('file.invalid'));
    }

    /**
     * @see LocalStorage::getMimeTypeFromExt()
     */
    public function testGuessMimeTypeFromExt(): void {
        $this->assertEquals('application/pdf', FileManager::getMimeTypeFromExt('pdf'));
        $this->assertEquals('text/csv', FileManager::getMimeTypeFromExt('csv'));
        $this->assertEquals('text/plain', FileManager::getMimeTypeFromExt('txt'));
        $this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', FileManager::getMimeTypeFromExt('xlsx'));
        $this->assertEquals('application/xml', FileManager::getMimeTypeFromExt('xml'));

        $this->assertEquals('text/plain', FileManager::getMimeTypeFromExt('.txt'));
        $this->assertEquals(null, FileManager::getMimeTypeFromExt(''));
        $this->assertEquals(null, FileManager::getMimeTypeFromExt('invalid'));
    }

    /**
     * @see LocalStorage::getDownloadIri()
     */
    public function testGetDownloadIri(): void
    {
        $fileManager = $this->getFileManagerMockFor('getDownloadIri');

        $file = $this->getMockBuilder(File::class)->getMock();
        $file->method('getId')->willReturn(1);

        $this->iriConverter
            ->expects(self::once())
            ->method('getIriFromResource')
            ->with(DownloadRequest::class, UrlGeneratorInterface::ABS_PATH, new Get(),['fileId' => 1])
            ->willReturn('/api/download/1');

        $this->assertEquals(
            '/api/download/1',
            $fileManager->getDownloadIri($file)
        );
    }

}
