<?php

namespace Aweos\Resizer\Tests;

use Aweos\Resizer\Models\Setting;
use Aweos\Resizer\Tests\TestCase;
use Cache;
use Event;
use Illuminate\Http\UploadedFile;
use Storage;

class DeleteTest extends TestCase
{

    public function setUp(): void
    {
        parent::setUp();
        Storage::fake('local');
    }

    public function testItDeletesAllVersionsIfOriginalImageDeleted() 
    {
        Cache::tags('pages/test.jpg')->set('resizer.original.pages/test.jpg', '::cacheContent::');
        Setting::set('folders', ['pages']);
        Setting::set('sizes', []);
        Setting::set('breakpoints', []);

        UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test.jpg', 'local');
        Event::fire('media.file.delete', [null, '/pages/test.jpg', null]);

        $this->assertFileCount(0, 'pages');
        Storage::assertMissing('uploads/public/c/pages');
        $this->assertNull(Cache::get('resizer.original.pages/test.jpg'));
    }

    public function testItDeletesAFileThatDoesntBeginWithASlash() 
    {
        Cache::tags('pages/test.jpg')->set('resizer.original.pages/test.jpg', '::cacheContent::');
        Setting::set('folders', ['pages']);
        Setting::set('sizes', []);
        Setting::set('breakpoints', []);

        UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test.jpg', 'local');
        Event::fire('media.file.delete', [null, 'pages/test.jpg', null]);

        $this->assertFileCount(0, 'pages');
    }

    public function testItPreservesDirectoryWhenThereAreOtherFiles() 
    {
        Setting::set('folders', ['pages']);
        Setting::set('sizes', []);
        Setting::set('breakpoints', []);

        UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test.jpg', 'local');
        UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test2.jpg', 'local');
        Event::fire('media.file.delete', [null, '/pages/test.jpg', null]);

        $this->assertHasFile('pages/test2.jpg');
        $this->assertDoesntHaveFile('pages/test.jpg');
    }

    public function testItDeletesFileVersions() 
    {
        Setting::set('folders', ['pages']);
        Setting::set('sizes', []);
        Setting::set('breakpoints', []);

        UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test.jpg', 'local');
        UploadedFile::fake()->image(300, 500)->storeAs('uploads/public/c/pages', 'test-300x500.jpg', 'local');
        Event::fire('media.file.delete', [null, '/pages/test.jpg', null]);

        $this->assertDoesntHaveFile('pages/test-300x500.jpg');
    }

    public function testItDeletesVersionsOfAPdf() 
    {
        Setting::set('folders', ['pages']);
        Setting::set('sizes', []);
        Setting::set('breakpoints', []);
        UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test-100x100.pdf.jpg', 'local');

        Event::fire('media.file.delete', [null, '/pages/test.pdf', null]);

        $this->assertDoesntHaveFile('pages/test-100x100.pdf.jpg');
    }

}